Skip to content

Instantly share code, notes, and snippets.

@hoangduy0610
Last active June 13, 2023 08:20
Show Gist options
  • Save hoangduy0610/23b107d4575ccc403fc347f43b5d8733 to your computer and use it in GitHub Desktop.
Save hoangduy0610/23b107d4575ccc403fc347f43b5d8733 to your computer and use it in GitHub Desktop.
Binary Search Tree Example
struct Node
{
int data;
Node *left;
Node *right;
};
typedef Node *Tree;
Node *CreateNode(int init)
{
Node *p = new Node;
p->data = init;
p->left = NULL;
p->right = NULL;
return p;
}
void CreateTree(Tree &root)
{
root = NULL;
}
void DestroyTree(Tree &root)
{
if (root)
{
DestroyTree(root->left);
DestroyTree(root->right);
delete root;
}
}
void AddNode(Tree &root, Node *node)
{
if (root)
{
if (root->data == node->data)
return;
if (node->data < root->data)
AddNode(root->left, node);
else
AddNode(root->right, node);
}
else
{
root = node;
}
}
Node *FindNode(Tree root, int x)
{
if (root)
{
if (root->data == x)
return root;
if (x < root->data)
return FindNode(root->left, x);
return FindNode(root->right, x);
}
return NULL;
}
void PrintTree(Tree root)// print tree using LNR
{
if (root)
{
PrintTree(root->left);
cout << root->data << ' ';
PrintTree(root->right);
}
}
void NLR(Tree root)
{
if (root)
{
// Xử lý nút gốc (root)
NLR(root->left);
NLR(root->right);
}
}
void LNR(Tree root)
{
if (root)
{
LNR(root->left);
// Xử lý nút gốc (root)
LNR(root->right);
}
}
void LRN(Tree root)
{
if (root)
{
LRN(root->left);
LRN(root->right);
// Xử lý nút gốc (root)
}
}
void FindAndReplace1(Tree &p, Tree &tree)
{
if (tree->left)
FindAndReplace1(p, tree->left);
else
{
p->data = tree->data;
p = tree;
tree = tree->right;
}
}
void FindAndReplace2(Tree &p, Tree &tree)
{
if (tree->right)
FindAndReplace2(p, tree->right);
else
{
p->data = tree->data;
p = tree;
tree = tree->left;
}
}
void DeleteNode(Tree &root, int x)
{
if (root)
{
if (x > root->data)
DeleteNode(root->right, x);
else if (x < root->data)
DeleteNode(root->left, x);
else
{
Node *p = root;
if (!root->left)
root = root->right;
else if (!root->right)
root = root->left;
else
FindAndReplace1(p, root->right);
// FindAndReplace2(p, root->left);
delete p;
}
}
else
{
cout << "Not found!\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment