Data Structure
Dr. Ahmed Hesham Mostafa
Lecture 8 – Binary Search Tree
TextBooks
• s k chang, “data structures and algorithms”
• Kruse and Leung, “Data Structures & Program Design in C”
Online Martials
• CS214: Data Structures by Prof. Dr Waleed A. Yousef
• https://www.youtube.com/playlist?list=PLoK2Lr1miEm-
5zCzKE8siQezj9rvQlnca
• Data Structures Learning Course by Dr Mohammed El-Said
• https://www.youtube.com/playlist?list=PLfay0LLBd0wiNeOR_SGoYfC
3w-NxFwd0D
• Lectures Source code (updated frequently)
• https://github.com/ahmedheshamostafa/DataStructure
Tree
Tree Implementation
root
left A right
left B right left C right
left D right left E right left F right
left G right left H right
Tree Node Model
Tree Implementation
typedef struct nodeType {
EntryType info;
struct nodeType *right;
struct nodeType *left;
} NodeType;
typedef NodeType * TreeType
Tree Implementation
Pre: None.
Post: The tree is initialized to be empty.
void CreateTree(TreeType *t){
*t=NULL;}
Pre: The tree is initialized.
Post: If the tree is empty (1) is returned. Else (0) is returned.
int EmptyTree(TreeType t){
return (!t);}
Pre: The tree is initialized.
Post: If the tree is full (1) is returned. Else (0) is returned.
int FullTree(TreeType t){
return 0;}
Tree Implementation
Pre: The tree is initialized.
Post: The tree has been been traversed in infix order sequence.
void Inorder(TreeType t, void(*pvisit)(EntryType)){
Stack s; NodeType *p=t;
if(p){
CreateStack(&s);
do{
while(p){Push(p, &s); p=p->left;}
Pop(&p, &s);
(*pvisit)(p->info);
p=p->right);
}while(!StackEmpty(&s) || p);
}}}
Tree Implementation
Pre: The tree is initialized.
Post: The tree has been been traversed in infix order sequence.
void Inorder(TreeType t,void(*pvisit)(EntryType)){
if(t){
Inorder(t->left, pvisit);
(*pvisit)(t->info);
Inorder(t->right, pvisit);
}
}
Tree Implementation
Pre: The tree is initialized.
Post: The tree has been been traversed in prefix order sequence.
void Preorder(TreeType t,void(*pvisit)(EntryType)){
if(t){
(*pvisit)(t->info);
Preorder(t->left, pvisit);
Preorder(t->right, pvisit);
}
}
Tree Implementation
Pre: The tree is initialized.
Post: The tree has been been traversed in Postfix order sequence.
void Postorder(TreeType t,void(*pvisit)(EntryType)){
if(t){
Postorder(t->left, pvisit);
Postorder(t->right, pvisit);
(*pvisit)(t->info);
}
}
Tree Implementation
int Size(TreeType t){
if (!t)
return 0;
return (1+Size(t->left)+Size(t->right));
}
Tree Implementation
int Depth(TreeType t){
if (!t)
return 0;
int a=Depth(t->left);
int b=Depth(t->right);
return (a>b)? 1+a : 1+b;
}
return (a>b)? a : b; also true by
defination
Tree Implementation
void ClearTree(TreeType *t){
if (*t){
ClearTree(&(*t)->left);
ClearTree(&(*t)->right);
free(*t);
*t=NULL;
}
}
pointer to pointer (don’t forget that
the definition typedef NodeType *
TreeType
Binary Search Trees
A binary search tree is a binary tree that is
either empty or in which every node has a
key (within its data entry) and satisfies the
following conditions:
1. The key of the root (if it exists) is greater
than the key in any node in the left
subtree of the root.
2. The key of the root (if it exists) is less
than the key in any node in the right
subtree of the root.
3. The left and right subtrees of the root
are again binary search trees.
Binary Search Trees
A binary search tree Not a binary search tree
Tree Implementation
Void Insert(TreeType *t,EntryType item){
NodeType*p=(NodeType*) malloc(sizeof(NodeType));
p->info=item;
p->left=NULL; p->right=NULL;
if (!(*T)) *t= p; //if the tree is empty
else{
NodeType *pre,*cur;
cur=*t;
while(cur){
Tree Implementation
. . .
while(cur){
pre=cur;
if(item < cur->info)
cur=cur->left;
else cur=cur->right;
}
if(item < pre->info) pre->left=p;
else pre->right=p;
}
}
Tree Implementation
int Delete(TreeType *t,EntryType k){
int found=0; NodeType *q=*t;
NodeType *r=NULL;
while(q && !(found=(k==q->info))){
r=q;
if(k < q->info) q=q->left;
else q=q->right;
}
if (found){
19
Tree Implementation
...
if (found){
if(!r)//Case of deleting the root
DeleteNode(t);
else if((k < r->info))
DeleteNode(& r->left);
else
DeleteNode(&r->right);
}
return found;
}
20
Tree Implementation
void DeleteNode(TreeType *pt){
NodeType *q=*pt; remember that pt refer to r->left
if(!(q)->left) *pt=(q)->right; or r->right
else
if(!(q)->right) *pt=(q)->left;
pt
else{ pt
20 q
…… q
} 15 29
free(q); pt 17 26 30
} q 16 28 39
27 21
... Tree Implementation
else{//third case
q=(q)->left; NodeType *r=Null;
while(q->right){
r=q; q=q->right; pt
} 20 q
(*pt)->info=q->info; 15 q 29
28
r
if(r)
17 26 30
r->right=q->left; q
else 28
28 39
(*pt)->left=q->left; 16 27
}
free(q);
22
}