SlideShare a Scribd company logo
1 of 115
Traversing a Binary Tree without recursion
In-order Traversal
Pre-order Traversal
Post-order Traversal
The Scenario
• Imagine we have a binary tree
• We want to traverse the tree
– It’s not linear
– We need a way to visit all nodes
• Three things must happen:
– Deal with the entire left sub-tree
– Deal with the current node
– Deal with the entire right sub-tree
In-order Traversal
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – do left
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – do left
At 2 – do left
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – do left
At 2 – do left
At NIL
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – do left
At 2 – do current
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – do left
At 2 – do right
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – do left
At 2 – do right
At NIL
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – do left
At 2 - done
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – do current
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – do right
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – do right
At NIL
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do left
At 9 – done
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do current
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do right
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do right
At 42 – do left
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do right
At 42 – do left
At NIL
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do right
At 42 – do current
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do right
At 42 – do right
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do right
At 42 – do right
At NIL
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – do right
At 42 – done
Use the Activation Stack
• With a recursive module, we
can make use of the
activation stack to visit the
sub-trees and “remember”
where we left off.
13
9
2
42
At 13 – done
Outline of In-Order Traversal
• Three principle steps:
– Traverse Left
– Do work (Current)
– Traverse Right
In-Order Traversal Procedure
typedef struct node
{
char data;
struct node *left;
struct node *right;
}node;
void inorder_non_recursive(node *t)
{
stack s;
while(t!=NULL)
{
s.push(t);
t=t->left;
}
while(s.isempty()!=1)
{
t=s.pop();
cout<<t->data;
t=t->right;
while(t!=NULL)
{
s.push(t);
t=t->left;
}
}
}
Pre-Order Traversal Procedure
void preorder_non_recursive(node *t)
{
stack s;
while(t!=NULL)
{
cout<<t->data;
s.push(t);
t=t->left;
}
while(s.isempty()!=1)
{
t=s.pop();
t=t->right;
while(t!=NULL)
{
cout<<t->data;
s.push(t);
t=t->left;
}
}
}
Post-Order Traversal Procedure
Binary Search Tree
Finding the Correct Location
12
3
7
41
98
Start with
this tree
Finding the Correct Location
12
3
7
41
98
Where would
4 be added?
Finding the Correct Location
12
3
7
41
98
To the top
doesn’t work
4
Finding the Correct Location
12
3
7
41
98
In the middle
doesn’t work
4
Finding the Correct Location
12
3
7
41
98
At the bottom
DOES work!
4
Finding the Correct Location
• Must maintain “search” structure
– Everything to left is less than current
– Everything to right is greater than current
• Adding at the “bottom” guarantees
we keep search structure.
• We’ll recurse to get to the “bottom”
(i.e. when current = nil)
Finding the Correct Location
if (current = nil)
DO “ADD NODE” WORK HERE
elseif (current^.data > value_to_add) then
// recurse left
Insert(current^.left, value_to_add)
else
// recurse right
Insert(current^.right, value_to_add)
endif
Adding the Node
• Current is an in/out pointer
– We need information IN to evaluate current
– We need to send information OUT because
we’re changing the tree (adding a node)
• Once we’ve found the correct location:
– Create a new node
– Fill in the data field
(with the new value to add)
– Make the left and right pointers point to nil
(to cleanly terminate the tree)
Adding the Node
current <- new(Node)
current^.data <- value_to_add
current^.left <- nil
current^.right <- nil
The Entire Module
procedure Insert(cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
Tracing Example
The following example shows a trace of
the BST insert.
• Begin with an empty BST (a pointer)
• Add elements 42, 23, 35, 47 in the
correct positions.
Head iot Ptr toa Node
head <- NIL
Insert(head, 42)
Head iot Ptr toa Node
head <- NIL
Insert(head, 42)
head
Head iot Ptr toa Node
head <- NIL
Insert(head, 42)
head
Head iot Ptr toa Node
head <- NIL
Insert(head, 42)
head
Head iot Ptr toa Node
head <- NIL
Insert(head, 42)
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
Head iot Ptr toa Node
head <- NIL
Insert(head, 42)
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
Head iot Ptr toa Node
head <- NIL
Insert(head, 42)
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
Head iot Ptr toa Node
head <- NIL
Insert(head, 42)
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
Head iot Ptr toa Node
head <- NIL
Insert(head, 42)
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
Head iot Ptr toa Node
head <- NIL
Insert(head, 42)
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 23
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 23
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 23
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
23
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
23
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
23
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
23
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 23
23
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
data_in = 35
35
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
35
Continue?
yes... I’ve had enough!
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
data_in = 47
35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
data_in = 47
35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
data_in = 47
35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
data_in = 47
35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
data_in = 47
35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
data_in = 47
35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
47
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
data_in = 47
35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
47
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
data_in = 47
35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
47
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
data_in = 47
35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
47
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
data_in = 47
35
procedure Insert(
cur iot in/out Ptr toa Node,
data_in iot in num)
if(cur = NIL) then
cur <- new(Node)
cur^.data <- data_in
cur^.left <- NIL
cur^.right <- NIL
elseif(cur^.data > data_in)
Insert(cur^.left, data_in)
else
Insert(cur^.right, data_in)
endif
endprocedure // Insert
47
head
42
.
.
Insert(head, 23)
Insert(head, 35)
Insert(head, 47)
.
.
23
35
47
Summary
• Preserve “search” structure!
• Inserting involves 2 steps:
– Find the correct location
• For a BST insert, always insert at the
“bottom” of the tree
– Do commands to add node
• Create node
• Add data
• Make left and right pointers point to nil
Deleting from a Binary Search Tree
(BST)
The Scenario
• We have a Binary Search Tree and
want to remove some element based
upon a match.
• Must preserve “search” property
• Must not lose any elements (i.e. only
remove the one element)
BST Deletion
• Search for desired item.
• If not found, then return NIL or print error.
• If found, perform steps necessary to
accomplish removal from the tree.
Four Cases for Deletion
• Delete a leaf node
• Delete a node with only one child (left)
• Delete a node with only one child (right)
• Delete a node with two children
Cases 2 and 3 are comparable and only
need slight changes in the conditional
statement used
Delete a Leaf Node
Simply use an “in/out”
pointer and assign it
to “nil”. This will
remove the node from
the tree.
cur <- nil
14
50
94
9 71 116
108
66
13
42
Delete a Leaf Node
Simply use an “in/out”
pointer and assign it
to “nil”. This will
remove the node from
the tree.
cur <- nil
Let’s delete 42.
14
50
94
9 71 116
108
66
13
42
Delete a Leaf Node
Simply use an “in/out”
pointer and assign it
to “nil”. This will
remove the node from
the tree.
cur <- nil
Move the pointer;
now nothing points
to the node.
14
50
94
9 71 116
108
66
13
42
Delete a Leaf Node
Simply use an “in/out”
pointer and assign it
to “nil”. This will
remove the node from
the tree.
cur <- nil
The resulting tree.
14
50
94
9 71 116
108
66
13
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left
or a right child.
Point the current
pointer to the
appropriate child:
cur <- cur^.left_child
or
cur <- cur^.right_child
14
50
94
71 116
108
66
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left
or a right child.
Point the current
pointer to the
appropriate child:
cur <- cur^.left_child
or
cur <- cur^.right_child
Let’s delete 14.
14
50
94
71 116
108
66
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left
or a right child.
Point the current
pointer to the
appropriate child:
cur <- cur^.right_child
Move the pointer; now
nothing points to the
node.
14
50
94
71 116
108
66
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left
or a right child.
Point the current
pointer to the
appropriate child:
cur <- cur^.right_child
The resulting tree.
50
94
71 116
108
66
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left
or a right child.
Point the current
pointer to the
appropriate child:
cur <- cur^.left_child
or
cur <- cur^.right_child
Let’s delete 71.
14
50
94
71 116
108
66
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left
or a right child.
Point the current
pointer to the
appropriate child:
cur <- cur^.left_child
Move the pointer; now
nothing points to the
node.
14
50
94
71 116
108
66
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left
or a right child.
Point the current
pointer to the
appropriate child:
cur <- cur^.left_child
The resulting tree.
14
50
94
116
108
66
42
Delete a Node with Two Children
14
50
94
71 116
108
66
42
Let’s delete 94.
Delete a Node with Two Children
14
50
94
71 116
108
66
42
Look to the right
sub-tree.
Delete a Node with Two Children
14
50
94
71 116
108
66
42
Find and copy the
smallest value
(this will erase the
old value but creates
a duplicate).
108
108
Delete a Node with Two Children
14
50
71 116
108
66
42
The resulting tree
so far.
108
Delete a Node with Two Children
14
50
71 116
108
66
42
Now delete the
duplicate from
the left sub-tree.
Delete a Node with Two Children
14
50
71 116
66
42
The final resulting
tree – still has search
structure.
108
tnode *del(tnode *t,int key)
{
tnode *temp;
if(t==NULL)
{
return NULL;
}
if(key<t->data)
{
t->left=del(t->left,key);
return t;
}
if(key>t->data)
{
t->right=del(t->right,key);
return t;
}
//element found
//no child
if(t->left==NULL&t->right==NULL)
{
temp=t;
delete temp;
return NULL;
}
typedef struct tnode
{
int data;
struct tnode*left;
struct tnode*right;
}tnode;
//one child
if(t->left!=NULL&&t->right==NULL)
{
temp=t;
t=t->left;
delete temp;
return t;
}
if(t->left==NULL&&t->right!=NULL)
{
temp=t;
t=t->right;
delete temp;
return t;
}
//both child present
temp=find_min(t->right);
t->data=temp->data;
t->right=del(t->right,temp->data);
return t;
tnode *find_min(tnode *r)
{
while(r->left!=NULL)
{
r=r->left;
}
return r;
}

More Related Content

Similar to BST.ppt

Similar to BST.ppt (7)

Stack (Sandeep Aravali Udaipur)
Stack (Sandeep Aravali Udaipur)Stack (Sandeep Aravali Udaipur)
Stack (Sandeep Aravali Udaipur)
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
 
Splay tree
Splay treeSplay tree
Splay tree
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heapsort
HeapsortHeapsort
Heapsort
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Splay tree C++
Splay tree C++Splay tree C++
Splay tree C++
 

More from DEEPAK948083

turban_ch07ch07ch07ch07ch07ch07dss9e_ch07.ppt
turban_ch07ch07ch07ch07ch07ch07dss9e_ch07.pptturban_ch07ch07ch07ch07ch07ch07dss9e_ch07.ppt
turban_ch07ch07ch07ch07ch07ch07dss9e_ch07.pptDEEPAK948083
 
introAdhocRoutingRoutingRoutingRouting-new.ppt
introAdhocRoutingRoutingRoutingRouting-new.pptintroAdhocRoutingRoutingRoutingRouting-new.ppt
introAdhocRoutingRoutingRoutingRouting-new.pptDEEPAK948083
 
SensorSensorSensorSensorSensorSensor.ppt
SensorSensorSensorSensorSensorSensor.pptSensorSensorSensorSensorSensorSensor.ppt
SensorSensorSensorSensorSensorSensor.pptDEEPAK948083
 
Chapter1_IntroductionIntroductionIntroduction.ppt
Chapter1_IntroductionIntroductionIntroduction.pptChapter1_IntroductionIntroductionIntroduction.ppt
Chapter1_IntroductionIntroductionIntroduction.pptDEEPAK948083
 
introDMintroDMintroDMintroDMintroDMintroDM.ppt
introDMintroDMintroDMintroDMintroDMintroDM.pptintroDMintroDMintroDMintroDMintroDMintroDM.ppt
introDMintroDMintroDMintroDMintroDMintroDM.pptDEEPAK948083
 
lect1lect1lect1lect1lect1lect1lect1lect1.ppt
lect1lect1lect1lect1lect1lect1lect1lect1.pptlect1lect1lect1lect1lect1lect1lect1lect1.ppt
lect1lect1lect1lect1lect1lect1lect1lect1.pptDEEPAK948083
 
Chchchchchchchchchchchchchchchchc 11.pptx
Chchchchchchchchchchchchchchchchc 11.pptxChchchchchchchchchchchchchchchchc 11.pptx
Chchchchchchchchchchchchchchchchc 11.pptxDEEPAK948083
 
applicationapplicationapplicationapplication.ppt
applicationapplicationapplicationapplication.pptapplicationapplicationapplicationapplication.ppt
applicationapplicationapplicationapplication.pptDEEPAK948083
 
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITYMOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITYDEEPAK948083
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxDEEPAK948083
 
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptxDEEPAK948083
 
Lect no 13 ECC.ppt
Lect no 13 ECC.pptLect no 13 ECC.ppt
Lect no 13 ECC.pptDEEPAK948083
 
block ciphermodes of operation.pptx
block ciphermodes of operation.pptxblock ciphermodes of operation.pptx
block ciphermodes of operation.pptxDEEPAK948083
 
Lect no 13 ECC.ppt
Lect no 13 ECC.pptLect no 13 ECC.ppt
Lect no 13 ECC.pptDEEPAK948083
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptxDEEPAK948083
 
ICS PPT Unit 4.ppt
ICS PPT Unit 4.pptICS PPT Unit 4.ppt
ICS PPT Unit 4.pptDEEPAK948083
 

More from DEEPAK948083 (20)

turban_ch07ch07ch07ch07ch07ch07dss9e_ch07.ppt
turban_ch07ch07ch07ch07ch07ch07dss9e_ch07.pptturban_ch07ch07ch07ch07ch07ch07dss9e_ch07.ppt
turban_ch07ch07ch07ch07ch07ch07dss9e_ch07.ppt
 
introAdhocRoutingRoutingRoutingRouting-new.ppt
introAdhocRoutingRoutingRoutingRouting-new.pptintroAdhocRoutingRoutingRoutingRouting-new.ppt
introAdhocRoutingRoutingRoutingRouting-new.ppt
 
SensorSensorSensorSensorSensorSensor.ppt
SensorSensorSensorSensorSensorSensor.pptSensorSensorSensorSensorSensorSensor.ppt
SensorSensorSensorSensorSensorSensor.ppt
 
Chapter1_IntroductionIntroductionIntroduction.ppt
Chapter1_IntroductionIntroductionIntroduction.pptChapter1_IntroductionIntroductionIntroduction.ppt
Chapter1_IntroductionIntroductionIntroduction.ppt
 
introDMintroDMintroDMintroDMintroDMintroDM.ppt
introDMintroDMintroDMintroDMintroDMintroDM.pptintroDMintroDMintroDMintroDMintroDMintroDM.ppt
introDMintroDMintroDMintroDMintroDMintroDM.ppt
 
lect1lect1lect1lect1lect1lect1lect1lect1.ppt
lect1lect1lect1lect1lect1lect1lect1lect1.pptlect1lect1lect1lect1lect1lect1lect1lect1.ppt
lect1lect1lect1lect1lect1lect1lect1lect1.ppt
 
Chchchchchchchchchchchchchchchchc 11.pptx
Chchchchchchchchchchchchchchchchc 11.pptxChchchchchchchchchchchchchchchchc 11.pptx
Chchchchchchchchchchchchchchchchc 11.pptx
 
applicationapplicationapplicationapplication.ppt
applicationapplicationapplicationapplication.pptapplicationapplicationapplicationapplication.ppt
applicationapplicationapplicationapplication.ppt
 
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITYMOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
 
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
 
Lect no 13 ECC.ppt
Lect no 13 ECC.pptLect no 13 ECC.ppt
Lect no 13 ECC.ppt
 
block ciphermodes of operation.pptx
block ciphermodes of operation.pptxblock ciphermodes of operation.pptx
block ciphermodes of operation.pptx
 
Lect no 13 ECC.ppt
Lect no 13 ECC.pptLect no 13 ECC.ppt
Lect no 13 ECC.ppt
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptx
 
whitman_ch04.ppt
whitman_ch04.pptwhitman_ch04.ppt
whitman_ch04.ppt
 
lesson333.ppt
lesson333.pptlesson333.ppt
lesson333.ppt
 
ICS PPT Unit 4.ppt
ICS PPT Unit 4.pptICS PPT Unit 4.ppt
ICS PPT Unit 4.ppt
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 

Recently uploaded

General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 

Recently uploaded (20)

General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 

BST.ppt

  • 1. Traversing a Binary Tree without recursion In-order Traversal Pre-order Traversal Post-order Traversal
  • 2. The Scenario • Imagine we have a binary tree • We want to traverse the tree – It’s not linear – We need a way to visit all nodes • Three things must happen: – Deal with the entire left sub-tree – Deal with the current node – Deal with the entire right sub-tree In-order Traversal
  • 3. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42
  • 4. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left
  • 5. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – do left
  • 6. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – do left At 2 – do left
  • 7. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – do left At 2 – do left At NIL
  • 8. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – do left At 2 – do current
  • 9. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – do left At 2 – do right
  • 10. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – do left At 2 – do right At NIL
  • 11. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – do left At 2 - done
  • 12. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – do current
  • 13. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – do right
  • 14. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – do right At NIL
  • 15. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do left At 9 – done
  • 16. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do current
  • 17. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do right
  • 18. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do right At 42 – do left
  • 19. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do right At 42 – do left At NIL
  • 20. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do right At 42 – do current
  • 21. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do right At 42 – do right
  • 22. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do right At 42 – do right At NIL
  • 23. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – do right At 42 – done
  • 24. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 2 42 At 13 – done
  • 25. Outline of In-Order Traversal • Three principle steps: – Traverse Left – Do work (Current) – Traverse Right
  • 26. In-Order Traversal Procedure typedef struct node { char data; struct node *left; struct node *right; }node; void inorder_non_recursive(node *t) { stack s; while(t!=NULL) { s.push(t); t=t->left; } while(s.isempty()!=1) { t=s.pop(); cout<<t->data; t=t->right; while(t!=NULL) { s.push(t); t=t->left; } } }
  • 27.
  • 28. Pre-Order Traversal Procedure void preorder_non_recursive(node *t) { stack s; while(t!=NULL) { cout<<t->data; s.push(t); t=t->left; } while(s.isempty()!=1) { t=s.pop(); t=t->right; while(t!=NULL) { cout<<t->data; s.push(t); t=t->left; } } }
  • 29.
  • 32. Finding the Correct Location 12 3 7 41 98 Start with this tree
  • 33. Finding the Correct Location 12 3 7 41 98 Where would 4 be added?
  • 34. Finding the Correct Location 12 3 7 41 98 To the top doesn’t work 4
  • 35. Finding the Correct Location 12 3 7 41 98 In the middle doesn’t work 4
  • 36. Finding the Correct Location 12 3 7 41 98 At the bottom DOES work! 4
  • 37. Finding the Correct Location • Must maintain “search” structure – Everything to left is less than current – Everything to right is greater than current • Adding at the “bottom” guarantees we keep search structure. • We’ll recurse to get to the “bottom” (i.e. when current = nil)
  • 38. Finding the Correct Location if (current = nil) DO “ADD NODE” WORK HERE elseif (current^.data > value_to_add) then // recurse left Insert(current^.left, value_to_add) else // recurse right Insert(current^.right, value_to_add) endif
  • 39. Adding the Node • Current is an in/out pointer – We need information IN to evaluate current – We need to send information OUT because we’re changing the tree (adding a node) • Once we’ve found the correct location: – Create a new node – Fill in the data field (with the new value to add) – Make the left and right pointers point to nil (to cleanly terminate the tree)
  • 40. Adding the Node current <- new(Node) current^.data <- value_to_add current^.left <- nil current^.right <- nil
  • 41. The Entire Module procedure Insert(cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert
  • 42. Tracing Example The following example shows a trace of the BST insert. • Begin with an empty BST (a pointer) • Add elements 42, 23, 35, 47 in the correct positions.
  • 43. Head iot Ptr toa Node head <- NIL Insert(head, 42)
  • 44. Head iot Ptr toa Node head <- NIL Insert(head, 42) head
  • 45. Head iot Ptr toa Node head <- NIL Insert(head, 42) head
  • 46. Head iot Ptr toa Node head <- NIL Insert(head, 42) head
  • 47. Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head
  • 48. Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head
  • 49. Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head 42
  • 50. Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head 42
  • 51. Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head 42
  • 52. Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head 42
  • 54. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23
  • 55. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23
  • 56. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23
  • 57. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert
  • 58. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert
  • 59. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 23
  • 60. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 23
  • 61. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 23
  • 62. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 23
  • 63. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 23
  • 65. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35
  • 66. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35
  • 67. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35
  • 68. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35
  • 69. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35
  • 70. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35
  • 71. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert
  • 72. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert
  • 73. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 35
  • 74. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 35
  • 75. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 35
  • 76. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 35
  • 77. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 35
  • 78. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 35
  • 81. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 data_in = 47 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert
  • 82. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 data_in = 47 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert
  • 83. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 data_in = 47 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert
  • 84. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 data_in = 47 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert
  • 85. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 data_in = 47 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert
  • 86. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 data_in = 47 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 47
  • 87. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 data_in = 47 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 47
  • 88. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 data_in = 47 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 47
  • 89. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 data_in = 47 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 47
  • 90. head 42 . . Insert(head, 23) Insert(head, 35) Insert(head, 47) . . 23 data_in = 47 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 47
  • 92. Summary • Preserve “search” structure! • Inserting involves 2 steps: – Find the correct location • For a BST insert, always insert at the “bottom” of the tree – Do commands to add node • Create node • Add data • Make left and right pointers point to nil
  • 93. Deleting from a Binary Search Tree (BST)
  • 94. The Scenario • We have a Binary Search Tree and want to remove some element based upon a match. • Must preserve “search” property • Must not lose any elements (i.e. only remove the one element)
  • 95. BST Deletion • Search for desired item. • If not found, then return NIL or print error. • If found, perform steps necessary to accomplish removal from the tree.
  • 96. Four Cases for Deletion • Delete a leaf node • Delete a node with only one child (left) • Delete a node with only one child (right) • Delete a node with two children Cases 2 and 3 are comparable and only need slight changes in the conditional statement used
  • 97. Delete a Leaf Node Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree. cur <- nil 14 50 94 9 71 116 108 66 13 42
  • 98. Delete a Leaf Node Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree. cur <- nil Let’s delete 42. 14 50 94 9 71 116 108 66 13 42
  • 99. Delete a Leaf Node Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree. cur <- nil Move the pointer; now nothing points to the node. 14 50 94 9 71 116 108 66 13 42
  • 100. Delete a Leaf Node Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree. cur <- nil The resulting tree. 14 50 94 9 71 116 108 66 13
  • 101. Delete a Node with One Child Use an “in/out” pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.left_child or cur <- cur^.right_child 14 50 94 71 116 108 66 42
  • 102. Delete a Node with One Child Use an “in/out” pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.left_child or cur <- cur^.right_child Let’s delete 14. 14 50 94 71 116 108 66 42
  • 103. Delete a Node with One Child Use an “in/out” pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.right_child Move the pointer; now nothing points to the node. 14 50 94 71 116 108 66 42
  • 104. Delete a Node with One Child Use an “in/out” pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.right_child The resulting tree. 50 94 71 116 108 66 42
  • 105. Delete a Node with One Child Use an “in/out” pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.left_child or cur <- cur^.right_child Let’s delete 71. 14 50 94 71 116 108 66 42
  • 106. Delete a Node with One Child Use an “in/out” pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.left_child Move the pointer; now nothing points to the node. 14 50 94 71 116 108 66 42
  • 107. Delete a Node with One Child Use an “in/out” pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.left_child The resulting tree. 14 50 94 116 108 66 42
  • 108. Delete a Node with Two Children 14 50 94 71 116 108 66 42 Let’s delete 94.
  • 109. Delete a Node with Two Children 14 50 94 71 116 108 66 42 Look to the right sub-tree.
  • 110. Delete a Node with Two Children 14 50 94 71 116 108 66 42 Find and copy the smallest value (this will erase the old value but creates a duplicate). 108
  • 111. 108 Delete a Node with Two Children 14 50 71 116 108 66 42 The resulting tree so far.
  • 112. 108 Delete a Node with Two Children 14 50 71 116 108 66 42 Now delete the duplicate from the left sub-tree.
  • 113. Delete a Node with Two Children 14 50 71 116 66 42 The final resulting tree – still has search structure. 108
  • 114. tnode *del(tnode *t,int key) { tnode *temp; if(t==NULL) { return NULL; } if(key<t->data) { t->left=del(t->left,key); return t; } if(key>t->data) { t->right=del(t->right,key); return t; } //element found //no child if(t->left==NULL&t->right==NULL) { temp=t; delete temp; return NULL; } typedef struct tnode { int data; struct tnode*left; struct tnode*right; }tnode;
  • 115. //one child if(t->left!=NULL&&t->right==NULL) { temp=t; t=t->left; delete temp; return t; } if(t->left==NULL&&t->right!=NULL) { temp=t; t=t->right; delete temp; return t; } //both child present temp=find_min(t->right); t->data=temp->data; t->right=del(t->right,temp->data); return t; tnode *find_min(tnode *r) { while(r->left!=NULL) { r=r->left; } return r; }