It is a easy algorithm to create binary sort,insert one data into the tree,and delete a data from the the tree.I am not a high person,so I hope someone could give me more and better advices to rewrite it.and I do last explain,I write these words and add note in the pragram,not as to my English is so good,Instead,my English is so poor.It is a turth that my cet-4 still doesn't passed.I just feel that we need more practices to prove our Englist especailly in the aspect of writting.
#include <iostream>
using namespace std;
typedef struct node
{
int data;//value
node * left; //left child tree
node * right; //right child tree
}*Node; //node structure
void InsertBST(Node t,int key);
void CreateBST(Node &t,int a[],int n) //Create BinaryTree according to the function named InsertBST()
{
/*
Get the number from arrary named a[],then insert into the tree one by one
*/
Node temp=new node;
temp->data=a[0];
temp->left=temp->right=NULL; //Build a node in a[0],and have it root node of the tree
t=temp; //restore root node
for(int i=1;i<n;i++)
{
InsertBST(t,a[i]); //Call InsertBST()
}
}
void InsertBST(Node t,int key)
{
Node head=t; //restore the head node of the tree in case it be destory
Node parent;
Node temp=new node; //Alloc one node space to resotre the key
temp->data=key;
temp->left=NULL;
temp->right=NULL;
while(head)
{
parent=head;
if(key==head->data) //If the key equals one of the array a[],insert nothing
{
delete temp;
return;
}
else
{
if(key<head->data) //If the key smaller than the current data ,turn to left
{
head=head->left;
}
else
{
head=head->right; //If the key bigger than the current data ,turn to right
}
}
}
if(key<parent->data) //If the key smaller than the parrent data,make the key node being parrent left child
{
parent->left=temp;
}
else //If the key smaller than the parrent data,make the key node being parrent left child
{
parent->right=temp;
}
}
void DeleteBST(Node t,int key)
{
if(t==NULL) return; //If tree is NULL tree ,do nothing
Node parent;
Node temp;
parent=temp=t; //Still restore the root node of the tree
bool flag=false; //false is represent for left child,and true is represent for rihgt child
while(temp)
{
if(key==temp->data) //Find which one you want to del then do next
{
/*
three direction:
I.If the node is the leaf node ,delete if straightly
II.If the node have one child,then alter the parent node,let the child tree get temp->left or temp->right
III.If the node have two child,this case have a little complex,we need find the node temp's backward node,and move backward node to the node that will delete
*/
if((temp->left==NULL)&&(temp->right==NULL))
{
if(temp==t) //If the node is the root head delete straightly
{
delete t;
}
else
{
if(! flag)
{
parent->left=NULL;
delete temp;
}
else
{
parent->right=NULL;
delete temp;
}
}
}
else
if(temp->left==NULL)
{
if(! flag)
{
parent->left=temp->right;
delete temp;
}
else
{
parent->right=temp->right;
delete temp;
}
}
else
if(temp->right==NULL)
{
if(!flag)
{
parent->left=temp->left;
delete temp;
}
else
{
parent->right=temp->left;
delete temp;
}
}
else
{
Node tt=temp->right;
Node ttt=tt;
while(tt)
{
ttt=tt;
tt=tt->left;
}
parent=ttt;
delete temp;
}
temp=NULL;
}
else
if(key<temp->data)
{
parent=temp;
flag=false;
temp=temp->left;
}
else
{
parent=temp;
flag=true;
temp=temp->right;
}
}
}
void InOrder(Node t)
{
if(t)
{
InOrder(t->left);
cout<<t->data<<ends;
InOrder(t->right);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[10];
for(int i=0;i<10;i++)
{
a[i]=rand()%10;
cout<<a[i]<<ends;
}
cout<<endl;
Node t=NULL;
CreateBST(t,a,10);
DeleteBST(t,4);
InOrder(t);
system("pause");
return 0;
}