The value of the left subtree of a node in a binary lookup tree is smaller than it is, and its right subtree is larger than its value.
The primary action to be implemented
Code implementation
#include <iostream>
using namespace Std;
The junction of BST
typedef struct Node
{
int key;
struct node *lchild, *rchild,*parent;
}node, *bst;
BST lvis=null;//is used to save the address of the parent node
void Createbst (BST &p);
void Assignmentparent (BST p);//assigns a value to the parent node of all nodes.
Inserts a node in a given BST, whose data field is element, which is called the new BST
bool Bstinsert (Node *&p, int element)
{
if (p = = NULL)
{
p = new Node;
P->key = element;
P->lchild = P->rchild = NULL;
}
if (element = = P->key)
{
return false;
}
if (element < P->key)
{
Bstinsert (p->lchild, Element);
}
Else
Bstinsert (p->rchild, Element);
return true;
}
void Printbst (BST &p, int depth)
{
BST &p1=p;
int i;
if (p1->rchild)
Printbst (p1->rchild, depth + 1);
for (i = 1; i <= depth; i++)
printf ("");
printf ("%d\n", P1->key);
if (p1->lchild)
Printbst (p1->lchild, depth + 1);
}
BST Search (BST p, int key)
{
if (p = = NULL)
{
printf ("This tree is empty!") \ n ");
return NULL;
}
else if (key = = P->key)
{
printf ("Find the address of the successful!,%d is%p\n", key, p);
return p;
}
else if (Key < P->key)
Search (P->lchild, key);
Else
Search (P->rchild, key);
}
void Createbst (BST &p)
{
int t;
int depth = 0;
p = NULL;
printf ("\ n Please enter the keyword (ending with 123 to build a balanced binary tree):");
scanf ("%d", &t);
while (t! =-123)
{
Bstinsert (P, t);
printf ("\ n Please enter the keyword (ending with 123 to build a balanced binary tree):");
scanf ("%d", &t);
}
Assignmentparent (P);//The purpose of this function is to assign values to the parent nodes of all nodes.
printf ("\n****************************************************\n");
printf ("******************* you create a two-fork tree for *******************\n");
if (p)
Printbst (p, depth);
Else
printf ("This is an empty tree!\n");
printf ("******************** two fork Tree creation completed *********************\n");
}
void Maximum (BST p)
{
BST p1=p;
while (P1->rchild)
{
p1=p1->rchild;
}
printf ("maximum=%d", P1->key);
}
void Minmum (BST p)
{
BST p1=p;
while (P1->lchild)
{
p1=p1->lchild;
}
printf ("minimum=%d", P1->key);
}
bool Insert (BST &p,int Element)
{
if (p = = NULL)
{
p = new Node;
P->key = element;
P->lchild = P->rchild = NULL;
}
if (element = = P->key)
{
return false;
}
if (element < P->key)
{
Insert (p->lchild, Element);
}
Else
Insert (p->rchild, Element);
return true;
}
void Get_parent (BST &p)
{
if (p)
{
printf ("%d->parent=%p", p->key,p->parent);
Get_parent (P->lchild);
Get_parent (P->rchild);
}
}
void Assignmentparent (BST p)
{
if (p)
{
p->parent=lvis;
Lvis=p;
Assignmentparent (P->lchild);
Lvis=p;
Assignmentparent (P->rchild);
}
}
First Order traversal
void Preorderbst (BST p)
{
if (p)
{
cout << p->key << "";
Preorderbst (P->lchild);
Preorderbst (P->rchild);
}
}
Middle sequence traversal (this can be done as a sort algorithm
void Inorderbst (BST p)
{
if (p)
{
Inorderbst (P->lchild);
cout << p->key << "";
Inorderbst (P->rchild);
}
}
Post-post traversal
void Postrderbst (BST p)
{
if (p)
{
Postrderbst (P->lchild);
Postrderbst (P->rchild);
cout << p->key << "";
}
}
void Delete (BST p,int key)
{
BST p1=p;
Delete to be divided into three kinds of cases
while (P1->key!=key)
{
if (Key>p1->key)
p1=p1->rchild;
Else
p1=p1->lchild;
}
The first case: if z doesn't have a child node, it simply removes it and modifies its parent node. Replace Z with Nil as a child (now it's not considered the root node)
if (p1->lchild==null&&p1->rchild==null)
{
if (p1->parent==null)
{
printf ("The root node has been deleted! , when the tree is empty OH \ n ");
P=null;
Return
}
if (P1->key<p1->parent->key)
p1->parent->lchild=null;
Else
p1->parent->rchild=null;
}
Second case: If there is only one child, raise the child to the z position in the tree and modify the parent node of Z to replace Z with the child of Z
{
if (p1->lchild!=null&&p1->rchild==null)
{
if (P1->key<p1->parent->key)
{
p1->parent->lchild=p1->lchild;
p1->lchild->parent=p1->parent;
}
Else
{
p1->parent->rchild=p1->lchild;
p1->lchild->parent=p1->parent;
}
}
if (p1->rchild!=null&&p1->lchild==null)
{
if (P1->key<p1->parent->key)
{
p1->parent->lchild=p1->rchild;
p1->rchild->parent=p1->parent;
}
Else
{
p1->parent->rchild=p1->rchild;
p1->rchild->parent=p1->parent;
}
}
The third case
if (p1->lchild!=null&&p1->rchild!=null)
{
Node *y=p1->rchild->lchild;
while (Y!=null)
y=y->lchild;
Replace Y with Y's right child first
y->rchild->parent=y->parent;
Replace Z with y
y->rchild=p1->rchild;
p1->rchild->parent=y;
y->lchild=p1->lchild;
p1->lchild->parent=y;
y->parent=p1->parent;
if (P1->parent->key>p1->key)
p1->parent->lchild=y;
Else
p1->parent->rchild=y;
}
}
}
int main (void)
{
int ch;
BST T;
printf ("Write all the node values of the BST tree you want to establish!\n");
Createbst (T);
while (1)
{
printf ("\n***************************************\n");
printf ("* * * * * Select the operation you want to do by following the options ******\n");
printf ("*****1 pre-order output. ************************\n");
printf ("*****2. Output ************************\n");
printf ("*****3. Post-Export ************************\n");
printf ("*****4. Find ****************************\n");
printf ("*****5. Max ************************\n");
printf ("*****6. To find the minimum value ************************\n");
printf ("*****7. Insert numeric ************************\n");
printf ("*****8. Get parent node address ******************\n");
printf ("*****9. Delete value ************************\n");
printf ("*****10. Print sort binary tree *****************\n");
printf ("*****11. Exit ***************************\n");
printf ("***************************************\n");
scanf ("%d", &ch);
Switch (CH)
{
Case 1:
Preorderbst (T); Break
Case 2:
Inorderbst (T); Break
Case 3:
Postrderbst (T); break;
Case 4:
{
int m;
printf ("What you're looking for:");
scanf ("%d", &m);
Search (T, M);
}
Break
Case 5:
Maximum (T); Break
Case 6:
Minmum (T); break;
Case 7:
{
printf ("How much you want to insert:");
int key;
scanf ("%d", &key);
Insert (T,key);
}break;
Case 8:
Get_parent (T);
Break
Case 9:
{
printf ("How much you want to delete:");
int key;
scanf ("%d", &key);
Delete (T,key);
}
Break
Case 10:
Printbst (t,0);
Break
Case 11:
return 0; Break
Default
Break
}
}
return 0;
}
Delete operation detailed
"Algorithm design-two-fork search tree" operation and implementation of binary lookup tree