Presentation is loading. Please wait.

Presentation is loading. Please wait.

BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.

Similar presentations


Presentation on theme: "BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND."— Presentation transcript:

1

2

3 BINARY TREES

4 A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND RIGHT SUBTREE OF t.

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25 height(t) = ? TO DISTINGUISH THE HEIGHT OF AN EMPTY TREE FROM THE HEIGHT OF A SINGLE-ITEM TREE, WHAT SHOULD THE HEIGHT OF AN EMPTY TREE BE?

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41 ABCDERSFGL THIS ASSOCIATION SUGGESTS THAT A COMPLETE BINARY TREE CAN BE STORED IN AN ARRAY:

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57 EXTERNAL PATH LENGTH

58

59

60

61

62

63 TRAVERSALS OF A BINARY TREE

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107 80 40 90 60 50 75 HERE ARE THE STEPS IF THE CALL IS find (60) :

108

109 80 40 90 60 50 75 HERE ARE THE STEPS IF THE CALL IS find (70):

110

111

112

113

114

115

116

117 insert (73 ) 80 40 90 60 50 75

118 80 40 90 60 50 75 73 WILL THE INSERTED ITEM ALWAYS BE A LEAF?

119

120

121

122

123

124

125 SUPPOSE link IS POINTING TO THE NODE WITH 50. 80 40 90 60 50 75 73

126 80 40 90 60 75 73

127 WHAT IF link IS POINTING TO THE NODE WITH 40? 80 40 90 60 75 73

128 80 60 90 75 73

129

130 SUPPOSE link IS POINTING TO 80’S NODE: 80 60 110 75 100 150 73 85 105 95

131 THE ITEM 80 HAS TWO CHILDREN, SO WE CANNOT SIMPLY UNLINK 80 FROM THE TREE: THAT WOULD CREATE A HOLE. OF THE ITEMS ALREADY IN THE TREE, WHICH TWO COULD REPLACE 80 WITHOUT DESTROYING THE STRUCTURE OF THE TREE?

132 WE CAN REPLACE 80 WITH EITHER ITS PREDECESSOR, 75, OR ITS SUCCESSOR, 85. WE’LL CHOOSE ITS SUCCESSOR. THE SUCCESSOR OF AN ITEM IS THE LEFTMOST ITEM IN THE RIGHT SUBTREE. REPLACE 80 WITH 85, AND THEN REMOVE 85.

133 HERE IS THE RESULTING TREE: 85 60 110 75 100 150 73 95 105

134 CAN REMOVING THE SUCCESSOR GET COMPLICATED? CAN THE SUCCESSOR HAVE TWO CHILDREN?

135

136

137

138

139 RECALL THAT link IS THE ONLY FIELD IN THE Iterator CLASS. if (link has a right child) // make link point to the leftmost node in link’s right subtree else // go up the tree to the left as far as possible, then go up // to the right. Make link point to that ancestor.

140

141

142

143


Download ppt "BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND."

Similar presentations


Ads by Google