Presentation is loading. Please wait.

Presentation is loading. Please wait.

Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree.

Similar presentations


Presentation on theme: "Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree."— Presentation transcript:

1 Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree

2 Traversing a Binary Tree Inorder Traversal

3 The Scenario Imagine we have a binary tree We want to traverse the tree –Its 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

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

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

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

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

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 left At NIL

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 current

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

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 – do right At NIL

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 left At 2 - done

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 current

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

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 – do right At NIL

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 left At 9 – done

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 current

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

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

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 left At NIL

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 current

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

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 – do right At NIL

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 – do right At 42 – done

25 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

26 Outline of In-Order Traversal Three principle steps: –Traverse Left –Do work (Current) –Traverse Right Work can be anything Separate work from traversal

27 Traverse the tree In order: –Visit the trees left sub-tree –Visit the current and do work –Visit right sub-tree

28 In-Order Traversal Procedure procedure In_Order(cur iot in Ptr toa Tree_Node) // Purpose: perform in-order traversal, call // Do_Something for each node // Preconditions: cur points to a binary tree // Postcondition: Do_Something on each tree // node in in-order order if( cur <> NIL ) then In_Order( cur^.left_child ) Do_Something( cur^.data ) In_Order( cur^.right_child ) endif endprocedure // In_Order

29 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R

30 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R

31 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R

32 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R L

33 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R L

34 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R L L

35 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R L L

36 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R L L L

37 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R L L L

38 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R L L L L

39 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 L P R L L L L

40 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 L P R L L L L P

41 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 L P R L L L L P

42 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 L P R L L L L P R

43 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 L P R L L L L P R

44 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 L P R L L L L P R

45 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 L P R L L L L P R P

46 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 L P R L L L L P R P

47 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 L P R L L L P R L P R

48 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 L P R L L L P R L P R

49 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 L P R L L L P R L P R L

50 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 L P R L L L P R L L P R

51 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 L P R L L L P R L L P R P

52 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 L P R L L L P R L L P R P

53 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 L P R L L L P R L P R L P R

54 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 L P R L L L P R L P R L P R

55 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 L P R L L L P R L P R L P R

56 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 L P R L L L P R L P R L P R

57 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 L P R L L P L P R L P R L P R

58 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 L P R L L P L P R L P R L P R

59 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 L P R L L P R L P R L P R L P R

60 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 L P R L L P R L P R L P R L P R

61 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 L P R L L P R L P R L P R L P R L

62 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 L P R L L P R L P R L P R L P R L

63 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 L P R L L P R L P R L P R L P R L P

64 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 L P R L L P R L P R L P R L P R L P

65 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 L P R L L P R L P R L P R L P R L P R

66 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 L P R L L P R L P R L P R L P R L P R

67 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 L P R L L P R L P R L P R L P R L P R

68 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 L P R L L P R L P R L P R L P R L P R

69 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 L P R L P L P R L P R L P R L P R L P R

70 Continue? Yes! Enough Already!

71 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 L P R L P L P R L P R L P R L P R L P R

72 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 L P R L P R L P R L P R L P R L P R L P R

73 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 L P R L P R L P R L P R L P R L P R L P R

74 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 L P R L P R L P R L P R L P R L P R L P R L

75 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 L P R L P R L P R L P R L P R L P R L P R L

76 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 L P R L P R L P R L P R L P R L P R L P R L L

77 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 L P R L P R L P R L P R L P R L P R L P R L L

78 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 L P R L P R L P R L P R L P R L P R L P R L L P

79 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 L P R L P R L P R L P R L P R L P R L P R L L P

80 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 L P R L P R L P R L P R L P R L P R L P R L L P R

81 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 L P R L P R L P R L P R L P R L P R L P R L L P R

82 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 L P R L P R L P R L P R L P R L P R L P R L L P R L

83 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 L P R L P R L P R L P R L P R L P R L P R L L P R L

84 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 L P R L P R L P R L P R L P R L P R L P R L L P R L P

85 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 L P R L P R L P R L P R L P R L P R L P R L L P R L P

86 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 L P R L P R L P R L P R L P R L P R L P R L L P R L P R

87 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 L P R L P R L P R L P R L P R L P R L P R L L P R L P R

88 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 L P R L P R L P R L P R L P R L P R L P R L L P R L P R

89 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 L P R L P R L P R L P R L P R L P R L P R L L P R L P R

90 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 L P R L P R L P R L P R L P R L P R L P R L L P R L P R P

91 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 L P R L P R L P R L P R L P R L P R L P R L L P R L P R P

92 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

93 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

94 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L

95 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L

96 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P

97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P

98 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

99 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

100 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L

101 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L

102 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 97 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P

103 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 97 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P

104 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 97 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

105 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 97 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

106 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 97 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

107 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 97 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

108 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 97 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

109 Algorithm Example... InOrderPrint(root)... 22 root 6736314447949719 Output: 1 3 7 9 14 22 36 44 67 94 97 L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

110 Summary An In-Order traversal visits every node –Recurse left first –Do something with current –Recurse right last The left, current, right logic is repeated recursively at every node. For a BST, an in-order traversal accesses the elements in ascending order.

111 Questions?

112 Binary Search Tree Insertion

113 Tree Node Defined In general: Node definesa record data isoftype left, right isoftype ptr toa Node endrecord In this example: Node definesa record data isoftype num left, right isoftype ptr toa Node endrecord

114 Scenario We have a Binary Search Tree –It can be empty –Or have some elements in it already We want to add an element to it –Inserting/adding involves 2 steps: Find the correct location Do the steps to add a new node Must maintain search structure

115 Finding the Correct Location 12 3 7 41 98 Start with this tree

116 Finding the Correct Location 12 3 7 41 98 Where would 4 be added?

117 Finding the Correct Location 12 3 7 41 98 To the top doesnt work 4

118 Finding the Correct Location 12 3 7 41 98 In the middle doesnt work 4

119 Finding the Correct Location 12 3 7 41 98 At the bottom DOES work! 4

120 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. Well recurse to get to the bottom (i.e. when current = nil)

121 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

122 Adding the Node Current is an in/out pointer –We need information IN to evaluate current –We need to send information OUT because were changing the tree (adding a node) Once weve 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)

123 Adding the Node current <- new(Node) current^.data <- value_to_add current^.left <- nil current^.right <- nil

124 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

125 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.

126 Head iot Ptr toa Node head <- NIL Insert(head, 42)

127 Head iot Ptr toa Node head <- NIL Insert(head, 42) head

128 Head iot Ptr toa Node head <- NIL Insert(head, 42) head

129 Head iot Ptr toa Node head <- NIL Insert(head, 42) head

130 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

131 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

132 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

133 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

134 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

135 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

136 head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47).

137 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

138 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

139 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

140 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

141 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

142 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

143 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

144 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

145 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

146 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

147 head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23

148 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

149 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

150 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

151 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

152 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

153 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

154 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

155 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

156 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

157 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

158 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

159 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

160 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

161 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

162 head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 35

163 Continue? yes...Ive had enough!

164 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

165 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

166 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

167 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

168 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

169 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

170 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

171 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

172 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

173 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

174 head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 35 47

175 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

176 Questions?

177 Deleting from a Binary Search Tree (BST)

178 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)

179 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.

180 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

181 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 145094971116108661342

182 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 Lets delete 42. 145094971116108661342

183 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. 145094971116108661342

184 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. 1450949711161086613

185 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 145094711161086642

186 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 Lets delete 14. 145094711161086642

187 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. 145094711161086642

188 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. 5094711161086642

189 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 Lets delete 71. 145094711161086642

190 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. 145094711161086642

191 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. 1450941161086642

192 Delete a Node with Two Children Copy a replacement value from a descendant node. - Largest from left - Smallest from right Then delete that descendant node to remove the duplicate value. - We know this will be an easier case. 145094711161086642

193 Delete a Node with Two Children 145094711161086642 Lets delete 50.

194 Delete a Node with Two Children 145094711161086642 Look to the left sub-tree.

195 Delete a Node with Two Children 145094711161086642 Find and copy the largest value (this will erase the old value but creates a duplicate).

196 Delete a Node with Two Children 144294711161086642 The resulting tree so far.

197 Delete a Node with Two Children 144294711161086642 Now delete the duplicate from the left sub-tree.

198 Delete a Node with Two Children 1442947111610866 The final resulting tree – still has search structure.

199 Delete a Node with Two Children 145094711161086642 Lets delete 94.

200 Delete a Node with Two Children 145094711161086642 Look to the right sub-tree.

201 Delete a Node with Two Children 145094711161086642 Find and copy the smallest value (this will erase the old value but creates a duplicate). 108

202 Delete a Node with Two Children 1450711161086642 The resulting tree so far.

203 108 Delete a Node with Two Children 1450711161086642 Now delete the duplicate from the left sub-tree.

204 Delete a Node with Two Children 1450711166642 The final resulting tree – still has search structure. 108

205 Summary Deleting a node from a binary search tree involves two steps: –Search for the element –Then perform the deletion We must preserve the search structure and only delete the element which matches. Four cases: –Deleting a leaf node –Deleting a node with only the left child –Deleting a node with only the right child –Deleting a node with both children

206 Questions?

207


Download ppt "Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree."

Similar presentations


Ads by Google