using
System;
class
GFG
{
static
readonly
int
MAX = 50;
public
class
Node
{
public
char
data;
public
int
isEndOfString = 1;
public
Node left, eq, right;
public
Node(
char
data)
{
this
.data = data;
isEndOfString = 0;
left =
null
;
eq =
null
;
right =
null
;
}
}
static
Node insert(Node root, String word,
int
i)
{
if
(root ==
null
)
root =
new
Node(word[i]);
if
(word[i] < root.data)
root.left = insert(root.left, word, i);
else
if
(word[i] > root.data)
root.right = insert(root.right, word, i);
else
{
if
(i + 1 < word.Length)
root.eq = insert(root.eq, word, i + 1);
else
root.isEndOfString = 1;
}
return
root;
}
static
int
max(
int
a,
int
b,
int
c)
{
int
max;
if
(a >= b && a >= c)
max = a;
else
if
(b >= a && b >= c)
max = b;
else
max = c;
return
max;
}
static
int
maxLengthTST(Node root)
{
if
(root ==
null
)
return
0;
return
max(maxLengthTST(root.left),
maxLengthTST(root.eq) + 1,
maxLengthTST(root.right));
}
public
static
void
Main()
{
Node root =
null
;
root = insert(root,
"Prakriti"
, 0);
root = insert(root,
"Raghav"
, 0);
root = insert(root,
"Rashi"
, 0);
root = insert(root,
"Sunidhi"
, 0);
int
value = maxLengthTST(root);
Console.WriteLine(
"Length of largest word in "
+
"ternary search tree is: "
+ value);
}
}