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

1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree Krish_ver2
 
Stack (Sandeep Aravali Udaipur)
Stack (Sandeep Aravali Udaipur)Stack (Sandeep Aravali Udaipur)
Stack (Sandeep Aravali Udaipur)sandeeppaliwalit
 
Splay tree
Splay treeSplay tree
Splay treeRajendran
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures LavanyaJ28
 

Similar to BST.ppt (8)

1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
 
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
 
whitman_ch04.ppt
whitman_ch04.pptwhitman_ch04.ppt
whitman_ch04.pptDEEPAK948083
 
lesson333.ppt
lesson333.pptlesson333.ppt
lesson333.pptDEEPAK948083
 
ICS PPT Unit 4.ppt
ICS PPT Unit 4.pptICS PPT Unit 4.ppt
ICS PPT Unit 4.pptDEEPAK948083
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptxDEEPAK948083
 

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

भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)Dr. Mazin Mohamed alkathiri
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 

Recently uploaded (20)

भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 

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; }