struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Tree {
public:
TreeNode* root;
Tree() : root(NULL) {}
void insert(int val) {
if (root == NULL) {
root = new TreeNode(val);
return;
}
TreeNode* cur = root;
while (true) {
if (val < cur->val) {
if (cur->left == NULL) {
cur->left = new TreeNode(val);
return;
} else {
cur = cur->left;
}
} else {
if (cur->right == NULL) {
cur->right = new TreeNode(val);
return;
} else {
cur = cur->right;
}
}
}
}
};
link
a trie is a rooted tree that represents a set of strings. From a computer science perspective, it's a data structure used for efficient string operations, commonly used in applications such as spell checkers, search engines, and network routing algorithms.
structure Node
Children Node[Alphabet-Size]
Is-Terminal Boolean
Value Data-Type
end structure