给定以三元搜索树表示的一组单词,找到其中最大单词的长度。
例子:
Input : {"Prakriti", "Raghav",
"Rashi", "Sunidhi"}
Output : Length of largest word in
ternary search tree is: 8
Input : {"Boats", "Boat", "But", "Best"}
Output : Length of largest word in
ternary search tree is: 5
先决条件:三元搜索树
想法是递归搜索左子树,右子树和相等树的最大值。
如果当前的字符是相同的,与1根的字符增量。
C
// C program to find the length of largest word
// in ternary search tree
#include
#include
#define MAX 50
// A node of ternary search tree
struct Node
{
char data;
// True if this character is last
// character of one of the words
unsigned isEndOfString: 1;
struct Node *left, *eq, *right;
};
// A utility function to create a new
// ternary search tree node
struct Node* newNode(char data)
{
struct Node* temp =
(struct Node*) malloc(sizeof( struct Node ));
temp->data = data;
temp->isEndOfString = 0;
temp->left = temp->eq = temp->right = NULL;
return temp;
}
// Function to insert a new word in a Ternary
// Search Tree
void insert(struct Node** root, char *word)
{
// Base Case: Tree is empty
if (!(*root))
*root = newNode(*word);
// If current character of word is smaller
// than root's character, then insert this
// word in left subtree of root
if ((*word) < (*root)->data)
insert(&( (*root)->left ), word);
// If current character of word is greater
// than root's character, then insert this
// word in right subtree of root
else if ((*word) > (*root)->data)
insert(&( (*root)->right ), word);
// If current character of word is same as
// root's character,
else
{
if (*(word+1))
insert(&( (*root)->eq ), word+1);
// the last character of the word
else
(*root)->isEndOfString = 1;
}
}
// Function to find max of three numbers
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;
}
// Function to find length of largest word in TST
int maxLengthTST(struct Node *root)
{
if (root == NULL)
return 0;
return max(maxLengthTST(root->left),
maxLengthTST(root->eq)+1,
maxLengthTST(root->right));
}
// Driver program to test above functions
int main()
{
struct Node *root = NULL;
insert(&root, "Prakriti");
insert(&root, "Raghav");
insert(&root, "Rashi");
insert(&root, "Sunidhi");
int value = maxLengthTST(root);
printf("Length of largest word in "
"ternary search tree is: %d\n", value);
return 0;
}
Java
// Java program to find the length of largest word
// in ternary search tree
public class GFG {
static final int MAX = 50;
// A node of ternary search tree
static class Node
{
char data;
// True if this character is last
// character of one of the words
int isEndOfString = 1;
Node left, eq, right;
// constructor
Node(char data)
{
this.data = data;
isEndOfString = 0;
left = null;
eq = null;
right = null;
}
}
// Function to insert a new word in a Ternary
// Search Tree
static Node insert(Node root, String word, int i)
{
// Base Case: Tree is empty
if (root == null)
root = new Node(word.charAt(i));
// If current character of word is smaller
// than root's character, then insert this
// word in left subtree of root
if (word.charAt(i) < root.data)
root.left = insert(root.left, word, i);
// If current character of word is greater
// than root's character, then insert this
// word in right subtree of root
else if (word.charAt(i) > root.data)
root.right = insert(root.right, word, i);
// If current character of word is same as
// root's character,
else
{
if (i + 1 < word.length())
root.eq = insert(root.eq, word, i + 1);
// the last character of the word
else
root.isEndOfString = 1;
}
return root;
}
// Function to find max of three numbers
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;
}
// Function to find length of largest word in TST
static int maxLengthTST(Node root)
{
if (root == null)
return 0;
return max(maxLengthTST(root.left),
maxLengthTST(root.eq)+1,
maxLengthTST(root.right));
}
// Driver program to test above functions
public static void main(String args[])
{
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);
System.out.println("Length of largest word in "+
"ternary search tree is: "+ value);
}
}
// This code is contributed by Sumit Ghosh
C#
// C# program to find the length of largest word
// in ternary search tree
using System;
class GFG
{
static readonly int MAX = 50;
// A node of ternary search tree
public class Node
{
public char data;
// True if this character is last
// character of one of the words
public int isEndOfString = 1;
public Node left, eq, right;
// constructor
public Node(char data)
{
this.data = data;
isEndOfString = 0;
left = null;
eq = null;
right = null;
}
}
// Function to insert a new word in a Ternary
// Search Tree
static Node insert(Node root, String word, int i)
{
// Base Case: Tree is empty
if (root == null)
root = new Node(word[i]);
// If current character of word is smaller
// than root's character, then insert this
// word in left subtree of root
if (word[i] < root.data)
root.left = insert(root.left, word, i);
// If current character of word is greater
// than root's character, then insert this
// word in right subtree of root
else if (word[i] > root.data)
root.right = insert(root.right, word, i);
// If current character of word is same as
// root's character,
else
{
if (i + 1 < word.Length)
root.eq = insert(root.eq, word, i + 1);
// the last character of the word
else
root.isEndOfString = 1;
}
return root;
}
// Function to find max of three numbers
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;
}
// Function to find length of largest word in TST
static int maxLengthTST(Node root)
{
if (root == null)
return 0;
return max(maxLengthTST(root.left),
maxLengthTST(root.eq) + 1,
maxLengthTST(root.right));
}
// Driver code
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);
}
}
/* This code contributed by PrinciRaj1992 */
输出:
Length of largest word in ternary search tree is: 8