The document discusses different methods for traversing binary trees, including recursive and non-recursive approaches. It covers preorder, inorder, and postorder traversal using recursion by following the tree nodes in a certain order. Non-recursive traversal is also described using an explicit stack. Level order traversal is explained which uses a queue to visit each level of the tree from left to right before moving to the next level. The document also covers deleting nodes from a binary search tree in different cases such as when the node is a leaf, has one child, or has two children.
12. Recursive Call
• Recall that a stack is used during function
calls.
• The caller function places the arguments
on the stack and passes control to the
called function.
• Local variables are allocated storage on
the call stack.
• Calling a function itself makes no
difference as far as the call stack is
concerned.
12
13. Stack Layout during a call
• Here is stack layout when function F calls
function F (recursively):
Parameters(F)
Local variables(F)
Return address(F)
Parameters(F)
Parameters(F)
Local variables(F)
Return address(F)
Parameters(F)
Local variables(F)
Return address(F)
Parameters(F)
Local variables(F)
Return address(F)
During execution of F After call
At point of call
sp
sp
sp
13
18. Non Recursive Traversal
• We can implement non-recursive versions
of the preorder, inorder and postorder
traversal by using an explicit stack.
• The stack will be used to store the tree
nodes in the appropriate order.
18
24. Level-order Traversal
• There is yet another way of traversing a
binary tree that is not related to recursive
traversal procedures discussed previously.
• In level-order traversal, we visit the nodes
at each level before proceeding to the next
level.
• At each level, we visit the nodes in a left-
to-right order.
24
26. Level-order Traversal
• How do we do level-order traversal?
• Surprisingly, if we use a queue instead of
a stack, we can visit the nodes in level-
order.
26
39. Deleting a node in BST
• As is common with many data structures,
the hardest operation is deletion.
• Once we have found the node to be
deleted, we need to consider several
possibilities.
• If the node is a leaf, it can be deleted
immediately.
39
40. Deleting a node in BST
• If the node has one child, the node can be
deleted after its parent adjusts a pointer to
bypass the node and connect to inorder
successor.
6
2
4
3
1
8
40
41. Deleting a node in BST
• The inorder traversal order has to be
maintained after the delete.
6
2
4
3
1
8
6
2
4
3
1
8
41
42. Deleting a node in BST
• The inorder traversal order has to be
maintained after the delete.
6
2
4
3
1
8
6
2
4
3
1
8
6
2
3
1
8
42
43. Deleting a node in BST
• The complicated case is when the node to
be deleted has both left and right subtrees.
• The strategy is to replace the data of this
node with the smallest data of the right
subtree and recursively delete that node.
43
44. Deleting a node in BST
Delete(2): locate inorder successor
6
2
5
3
1
8
4
Inorder
successor
44
45. Deleting a node in BST
Delete(2): locate inorder successor
6
2
5
3
1
8
4
Inorder
successor
▪ Inorder successor will be the left-most
node in the right subtree of 2.
▪ The inorder successor will not have a left
child because if it did, that child would be
the left-most node.
45
46. Deleting a node in BST
Delete(2): copy data from inorder successor
6
2
5
3
1
8
4
6
3
5
3
1
8
4
46
47. Deleting a node in BST
Delete(2): remove the inorder successor
6
2
5
3
1
8
4
6
3
5
3
1
8
4
6
3
5
3
1
8
4
47
49. 49
Deletion in BST: Let x be a value to be deleted from the BST and let
N denote the node containing the value x.
Deletion of an element in a BST again uses the BST property in a
critical way.
When we delete the node N containing x, it would
create a "gap" that should be filled by a suitable existing node of the
BST.
There are two possible candidate nodes that can fill this gap, in a
way that the BST property is not violated:
(1). Node containing highest valued element among all descendants
of left child of N.
(2). Node containing the lowest valued element among all the
descendants of the right child of N. There are three possible cases
to consider:
50. 50
Deleting a leaf (node with no children): Deleting a leaf is easy,
as we can simply remove it from the tree.
Deleting a node with one child: Delete it and replace it with
its child.
Deleting a node with two children: Call the node to be
deleted "N". Do not delete N.
Instead, choose its in-order successor node "S". Replace the
value of “N” with the value of “S”. (Note: S itself has up to one
child.)
As with all binary trees, a node's in-order successor is the left-
most child of its right subtree.
This node will have zero or one child. Delete it according to one
of the two simpler cases above.
Figure on next page illustrates several scenarios for deletion in
BSTs.