Traversals
• Traversal is the process of visiting every node once
• Visiting a node entails doing some processing at that node,
but when describing a traversal strategy, we need not
concern ourselves with what that processing is
• Three recursive techniques for binary tree traversal
• In each technique, the left subtree is traversed recursively,
the right subtree is traversed recursively, and the root is
visited
• What distinguishes the techniques from one another is the
order of those 3 tasks
In Preorder, the root is
visited before (pre) the
subtrees traversals
In Inorder, the root is
visited in-between left
and right subtree
traversal
In Preorder, the root is
visited after (Post) the
subtrees traversals
Pre order(info, left, right, root)
A binary tree t is in the memory. An array Stack is used to temporarily hold the addresses of
nodes.
1 Top=0
Stack[0] = NULL
Ptr = root
2 Repeat steps 3 to 5 while Ptr != NULL
3 Apply process to ptr->info Suppose printing
4 If (ptr-> Right != Null) then Right Child ?
Top = Top +1
Stack[Top] = ptr->Right
5 If ( ptr-> left != NULL) then Left Child ?
Ptr =Ptr-> Left
Else
Ptr = Stack[Top]
Top = Top-1
6 Return
In order(info, left, right, root)
A binary tree t is in the memory. An array Stack is used to
temporarily hold the addresses of nodes.
1 Top=0
Stack[0] = NULL
Ptr = root
2 Repeat while Ptr != NULL
Top = Top +1
Stack[Top] = Ptr
Ptr = Ptr->left
3 Ptr = Stack[Top] Suppose printing
Top = Top-1
4 Repeat steps 5 to 7 while Ptr != NULL Backtracking
5 Apply Process to Ptr->info
6 If Ptr->right != NULL then Right Child ?
a) Ptr = Ptr->right
b) Goto step 3;
7 Ptr = stack[Top]
Top = Top-1
8 Exit
PostOrder (info, left, right, root)
A binary tree t is in the memory. An array Stack is used to
temporarily hold the addresses of nodes.
1 Top=0
Stack[0] = NULL
Ptr = root
2 Repeat steps 3 to 5 while Ptr != NULL
3 Top = Top +1
Stack[Top] = Ptr
4 If (Ptr->right != NULL)
Top = Top+1
Stack[Top] = -Ptr->right
5 Ptr = Ptr->left
6 Ptr = stack[Top]
Top = Top-1
7 Repeat while Ptr>0
Process Ptr->info
Ptr = stack[Top]
Top = Top -1
8 If (Ptr < 0)
Ptr = -Ptr
Goto step 2
9 Exit
Recursive Algorithm for Traversals
Preorder (root) Inorder(root) postorder(root)
Ptr = root Ptr = root Ptr = root
If ptr != NULL then If ptr == NULL then If ptr == NULL then
Display ptr->info Display “Empty Display “Empty
Else Tree” Tree”
Display “Empty Return Return
Tree”
return
If (ptr->lptr != NULL) then If (ptr->lptr != null) then If (ptr->lptr != null) then
Preorder(ptrlptr) Inorder(ptr->lptr) postorder(ptr->lptr)
If (ptr->rptr !=NULL) Display ptr->info If (ptr->rptr != NULL) then
Preorder(ptr->rptr) postorder(ptr->rptr)
Return If (ptr->rptr != NULL) then Display ptr->data
Inorder(ptr->rptr)
Return return
Binary Search Tree (BST)
Binary tree with following rules is known as BST
I. The value in the right child or right sub tree is more than or equal to value of the root.
II. The value in left right child or left sub tree is less than or equal to value of the root.
Algorithm for searching in a BST
Case 1 : if Tree is empty
Address of node and it parent is NUL
Loc = Parent= NULL
Case 2:
If Node is found as root node.
Loc = address of root Node
Parent = NULL
Case 3:
If node is either internal node or
external node
Loc <> Null
Parent <> Null
Case 4:
if node is not found
Loc = NULL
Parent <> NULL
Find (Root, Loc, Par, Item)
Loc : Address of Item; Par = Address of Parent of Item; Ptr, save are temporary pointers
1 If (Root == NULL) then Case 1
Loc = Par = NULL
return
2 If (Item == Root->info) then Case 2
Loc = root ; Par = Null; return
3 If (item < root->info) then Case 3
Ptr = root->lptr; Save= root
Else
Ptr = root->rptr ; Save = root
4 While (ptr <> NULL)
If (Item == ptr->nfo) then
Loc = ptr ; Par = save
Return
If (item < ptr->info) then
Save = ptr ; Ptr = ptr->lptr
Else
Save = ptr; Ptr= ptr->rptr
5 Loc = NULL, Case 4
Par = Save
6 return
Algorithm for insertion in BST
Insertion_BST(Root, Loc, Par, Item)
1 Find_BST(Root, Loc, Par, item)
2 If (Loc <> NULL)
return
3 Temp = malloc node
Temp->info = item
Temp->lptr = temp->rptr = NULL
4 If (par == NULL)
Root = temp
Else
If (item < par->info) then
Par->lptr = temp
Else
Par->rptr = temp
5 return
Deletion in BST
Case 1 : node u want to delete, have no
children Let us Delete 5
Case 2: : node u want to delete, have
one child
Let us Delete 40
Case 3: node u want to delete, have two
child
Let us Delete 50
In order : 5,10,20 30, 35,40,50, 70
#include<stdio.h>
#include<stdlib.h>
typedef struct tree
{
int info;
struct tree *left;
struct tree *right;
}node;
node *root = NULL;
node * insert(node *, int);
void preorder(node *);
void postorder(node *);
void inorder(node *);
int count=1;
main()
{
int choice;
int value;
while(1)
{
printf("\n 1 insert ");
printf("\n 2 Pre Order ");
printf("\n 3 In Order ");
printf("\n 4 Post Order ");
printf("\n 5 Quit ");
printf("\n\n Input Choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:printf("\n Stop Populating tree by 0");
printf("\n Enter value for node: ");
scanf("%d",&value);
while(value != 0 )
{
root = insert(root, value);
scanf("%d",&value);
}
break;
case 2:preorder(root);break;
case 3:inorder(root); break;
case 4:postorder(root); break;
case 5:exit(0);
}// switch case
}//while
}// main
node *insert(node *ptr, int value)
{
if (ptr == NULL)
{
ptr = (node *)malloc(sizeof(node));
ptr->left = NULL;
ptr->right = NULL;
ptr->info = value;
count++;
}
else
{
if (count%2 == 0)
ptr->left = insert(ptr->left,value);
else
ptr->right=insert(ptr->right,value);
}
return(ptr);
}
void preorder(node *ptr)
{
if (ptr!=NULL)
{
printf("%d ", ptr->info);
preorder(ptr->left);
preorder(ptr->right);
}
}
void inorder(node *ptr)
{
if (ptr!=NULL)
{
inorder(ptr->left);
printf("%d ", ptr->info);
inorder(ptr->right);
}
}
void postorder(node *ptr)
{
if (ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
printf("%d ", ptr->info);
}
}
Seaching in BST
Find(info, lptr,rptr, root, item, loc, par)
A binary search tree (T)is in memory and ITEM of information is given.
The algo finds the location of item in T. and also location PAR of the
parent of ITEM
If LOC = 0 PAR = 0 will indicate tree is empty
If loc != 0 Par = 0 will indicate that ITEM is the root of tree.
If loc =NULL and PAR !=NULL will ITEM is not in T.
1 If ROOT == NULL then
LOC = NULL
PAR = NULL
Return
2 If( ITEM == ROOT->info) then
LOC = ROOT
PAR = NULL
Return
3 If (ITEM <ROOT->info)
Ptr = ROOT->Lptr
SAVE = ROOT
Else
Ptr = ROOT->Rptr
SAVE = ROOT
4 Repeat steps 5 and 6 while PTR != NULL
5 If (ITEM == PTR->info) then
LOC = PTR
PAR = SAVE
Return
6 If (ITEM <PTR->info) then
SAVE = PTR
PTR = PTR->Lptr
Else
SAVE = PTR
PTR = PTR->Rptr
7 /* When Search unsuccessful */
LOC = NULL
PAR = SAVE
8 Return
Insertion in BST
INSBT(info, LPTR, RPTR, ROOT, ITEM, LOC)
1
Call Find(info, lptr,rptr, root, item, loc,
par)
2
If (LOC != NULL) Then
Return
3
NEW = malloc(sizeof node)
NEW->info = ITEM
LOC = NEW
NEW->LPTR = NEW->RPTR = NULL
4
If (PAR ==NULL)
ROOT = NEW
Else if (ITEM < PAR->info)
PAR->LPTR = NEW
Else
PAR->RPTR = NEW
5
Return
Deletion of node from Binary Search tree
Del(Root, Item)
1 Find(info, lptr,rptr, root, item, loc, par)
loc and par contains the address of itself and
parent.
2 If (loc ==NULL)
Display(“Item not in Tree”);
Exit()
3 If (loc->rptr != NULL && loc->lptr !=NULL)
caseB(Root,loc,par)
else
caseA(Root,loc,par)
4 Free (loc)
5 Return
caseA(root, loc, par)
1 If (loc->lptr == NULL && loc->rptr == NULL)
Child = NULL
Else
If (loc-Lptr != NULL)
Child = loc->lptr
Else
Child = loc->rptr
2 If ( par != NULL)
If ( loc ==par->lptr)
Par->lptr = child
ELSE
PAR->RPTR = CHILD
ELSE
ROOT = CHILD
3 RETURN
caseB(root,loc,par)
1 Ptr = loc->rptr
Save = loc
While( ptr - >lptr != NULL)
Save = ptr
Ptr = ptr->Lptr
Suc = ptr
Parsuc = save
2 CaseA(root, suc, parsuc)
3 If (par != NULL)
If (loc == par->lptr
Par->lptr = suc
Else
Par->rptr = suc
Else
Root =suc
4 Suc->lptr = loc->lptr
Suc->rptr =loc->rptr
5 return