Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.

Similar presentations


Presentation on theme: "Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio."— Presentation transcript:

1 Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio

2 Binary Trees 2 Topics Applications Binary Search Trees  Retrieve  Insert  Delete

3 Binary Trees 3 Ordered Dictionaries Keys are assumed to come from a total order. New operations:  closestBefore(k)  closestAfter(k)

4 Binary Trees 4 Binary Search (§8.3.3) Binary search performs operation find(k) on a dictionary implemented by means of an array-based sequence, sorted by key  similar to the high-low game  at each step, the number of candidate items is halved  terminates after O(log n) steps Example: find(7) 13457 8 91114161819 1 3 457891114161819 134 5 7891114161819 1345 7 891114161819 0 0 0 0 m l h m l h m l h l  m  h

5 Binary Trees 5

6 6 Overview of Binary Search Tree Binary search tree definition: T is a binary search tree if either of these is true  T is empty; or  Root has two subtrees: Each is a binary search tree Value in root > all values of the left subtree Value in root < all values in the right subtree

7 Binary Trees 7

8 8

9 9

10 10 Binary Search Trees

11 Binary Trees 11

12 Binary Trees 12

13 Binary Trees 13

14 Binary Trees 14

15 Binary Trees 15

16 Binary Trees 16

17 Binary Trees 17

18 Binary Trees 18

19 Binary Trees 19

20 Binary Trees 20

21 Binary Trees 21

22 Binary Trees 22

23 Binary Trees 23

24 Binary Trees 24

25 Binary Trees 25 summer spring winter maybe fall always ***

26 Binary Trees 26 Using Binary Search Trees Application: Removing Duplicates

27 Binary Trees 27

28 Binary Trees 28 Binary Tree Nodes

29 Binary Trees 29 Searching a Binary Tree: Algorithm 1. if root is NULL 2. item not in tree: return NULL 3. compare target and root->data 4. if they are equal 5. target is found, return root->data 6. else if target data 7. return search(left subtree) 8. else 9. return search(right subtree) Note that in the STL, trees are not implemented with recursion but with loops

30 Binary Trees 30 - CurrentNodeAction -LOCATING DATA IN A TREE- Root = 50Compare item = 37 and 50 37 < 50, move to the left subtree Node = 30Compare item = 37 and 30 37 > 30, move to the right subtree Node = 35Compare item = 37 and 35 37 > 35, move to the right subtree Node = 37Compare item = 37 and 37. Item found.

31 Binary Trees 31 80 40 90 60 50 75 WHAT ARE THE STEPS IF THE CALL IS find (60)?

32 Binary Trees 32 80 40 90 60 50 75 WHAT ARE THE STEPS IF THE CALL IS find (70)?

33 Binary Trees 33

34 Binary Trees 34 Logic for Insert Insertion is similar to find, but you must keep track of parent to be able to insert a new node Find the spot in the tree that the value would be at if it were already in the tree, When you reach a null link, insert the value there

35 Binary Trees 35 Update Operations: 1 st of 3 steps 1)-The function begins at the root node and compares item 32 with the root value 25. Since 32 > 25, we traverse the right subtree and look at node 35.

36 Binary Trees 36 Update Operations: 2 nd of 3 steps 2)-Considering 35 to be the root of its own subtree, we compare item 32 with 35 and traverse the left subtree of 35.

37 Binary Trees 37 Update Operations: 3 rd of 3 steps 3)-Create a leaf node with data value 32. Insert the new node as the left child of node 35. newNode = getSTNode(item,NULL,NULL,parent); parent->left = newNode;

38 Binary Trees 38 insert (73) 80 40 90 60 50 75

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

40 Binary Trees 40 FOR INSERTING AN ITEM, WHAT IS THE WORST CASE? WHAT IS THE WORST HEIGHT? THE worstTime(n) IS LINEAR IN n. WHAT IS THE AVERAGE HEIGHT? THE averageTime(n) IS LOGARITHMIC IN n.

41 Binary Trees 41

42 Binary Trees 42

43 Binary Trees 43 Deletion There are three possible cases to deletion: Value to be deleted is a leaf node: just adjust parent link and delete node Value to be deleted has one child: child takes parent’s place (value to be deleted’s parent now points to grandchild) and delete node Value to be deleted has two children: swap and delete node

44 Binary Trees 44

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

46 Binary Trees 46 80 40 90 60 75 73

47 Binary Trees 47 Removing an Item From a Binary Tree

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

49 Binary Trees 49 80 60 90 75 73

50 Binary Trees 50 Removing an Item From a Binary Tree

51 Binary Trees 51 Removing an Item From a Binary Tree

52 Binary Trees 52

53 Binary Trees 53 Removing an Item From a Binary Tree

54 Binary Trees 54 Removing a Binary Search Tree Node

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

56 Binary Trees 56 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?

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

58 Binary Trees 58 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.

59 Binary Trees 59 HERE IS THE RESULTING TREE: 85 60 110 75 100 150 73 95 105

60 Binary Trees 60 CAN REMOVING THE SUCCESSOR GET COMPLICATED? CAN THE SUCCESSOR HAVE TWO CHILDREN?

61 Binary Trees 61 Removing an Item From a Binary Tree

62 Binary Trees 62 Removing an Item From a Binary Tree

63 Binary Trees 63 Removing an Item From a Binary Tree

64 Binary Trees 64 Removing from a Binary Search Tree Item not present: do nothing Item present in leaf: remove leaf (change to null) Item in non-leaf with one child: Replace current node with that child Item in non-leaf with two children?  Find largest item in the left subtree  Recursively remove it  Use it as the parent of the two subtrees  (Could use smallest item in right subtree)

65 Binary Trees 65

66 Binary Trees 66 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.

67 Binary Trees 67

68 Binary Trees 68

69 Binary Trees 69

70 Binary Trees 70 Summary Slide 1 §- A binary search tree stores data by value instead of position -It is an example of an associative container. §-The simple rules “== return” “< go left” “> go right” until finding a NULL subtree make it easy to build a binary search tree that does not allow duplicate values.

71 Binary Trees 71

72 Binary Trees 72 Summary Slide 2 §- The insertion algorithm can be used to define the path to locate a data value in the tree. §- The removal of an item from a binary search tree is more difficult and involves finding a replacement node among the remaining values.


Download ppt "Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio."

Similar presentations


Ads by Google