Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees.

Similar presentations


Presentation on theme: "Binary Search Trees."— Presentation transcript:

1 Binary Search Trees

2 Binary Search Trees Binary Tree labels at nodes
Labels have some order (comparable/comparator) If a node has label x, then every label in the right sub-tree is > x, and every label in the left sub-tree is < x This supports the dictionary ADT Lookup, insert, delete Running time = O(log n) (height of tree)

3 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59

4 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50

5 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 75

6 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75

7 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75 25

8 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75 25 61

9 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75 25 61 55

10 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75 25 61 55 30

11 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75 25 61 55 30 15

12 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75 25 61 55 68 30 15

13 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75 25 61 55 68 30 15 32

14 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75 25 61 55 68 30 15 32 36

15 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75 25 61 55 68 30 15 32 28 36

16 Example BST insertion 50, 75, 37, 25, 61, 55, 30, 15, 68, 32, 36, 28, 59 50 37 75 25 61 55 68 30 15 32 59 28 36

17 BST An inOrder traversal of a BST accesses the elements in increasing order

18 Inorder public void printInOrder() { if (left != null)
left.printInOrder(); System.out.println(element); if (right != null) right.printInOrder(); }

19 Example BST 50 37 75 25 61 55 68 30 15 32 59 28 36

20 Example BST 50 37 75 25 61 55 68 30 15 32 59 28 36

21 Example BST 50 37 75 25 61 55 68 30 15 32 59 28 36

22 Example BST 50 37 75 25 61 55 68 30 15 32 59 28 36

23 Example BST 50 37 75 25 61 55 68 30 15 32 59 28 36

24 Example BST 15 50 37 75 25 61 55 68 30 15 32 59 28 36

25 Example BST 15 50 37 75 25 61 55 68 30 15 32 59 28 36

26 Example BST 15, 25 50 37 75 25 61 55 68 30 15 32 59 28 36

27 Example BST 15, 25 50 37 75 25 61 55 68 30 15 32 59 28 36

28 Example BST 15, 25 50 37 75 25 61 55 68 30 15 32 59 28 36

29 Example BST 15, 25, 28 50 37 75 25 61 55 68 30 15 32 59 28 36

30 Example BST 15, 25, 28 50 37 75 25 61 55 68 30 15 32 59 28 36

31 Example BST 15, 25, 28, 30 50 37 75 25 61 55 68 30 15 32 59 28 36

32 Example BST 15, 25, 28, 30 50 37 75 25 61 55 68 30 15 32 59 28 36

33 Example BST 15, 25, 28, 30, 32 50 37 75 25 61 55 68 30 15 32 59 28 36

34 Example BST 15, 25, 28, 30, 32 50 37 75 25 61 55 68 30 15 32 59 28 36

35 Example BST 15, 25, 28, 30, 32 50 37 75 25 61 55 68 30 15 32 59 28 36

36 Example BST 15, 25, 28, 30, 32 50 37 75 25 61 55 68 30 15 32 59 28 36

37 Example BST 15, 25, 28, 30, 32 50 37 75 25 61 55 68 30 15 32 59 28 36

38 Example BST 15, 25, 28, 30, 32 50 37 75 25 61 55 68 30 15 32 59 28 36

39 Example BST 15, 25, 28, 30, 32 50 37 75 25 61 55 68 30 15 32 59 28 36

40 Example BST 15, 25, 28, 30, 32, 37 50 37 75 25 61 55 68 30 15 32 59 28 36

41 Example BST 15, 25, 28, 30, 32, 37 50 37 75 25 61 55 68 30 15 32 59 28 36

42 Example BST 15, 25, 28, 30, 32, 37, 50 50 37 75 25 61 55 68 30 15 32 59 28 36

43 Example BST 15, 25, 28, 30, 32, 37, 50 50 37 75 25 61 55 68 30 15 32 59 28 36

44 Example BST 15, 25, 28, 30, 32, 37, 50 50 37 75 25 61 55 68 30 15 32 59 28 36

45 Example BST 15, 25, 28, 30, 32, 37, 50 50 37 75 25 61 55 68 30 15 32 59 28 36

46 Example BST 15, 25, 28, 30, 32, 37, 50, 55 50 37 75 25 61 55 68 30 15 32 59 28 36

47 Example BST 15, 25, 28, 30, 32, 37, 50, 55 50 37 75 25 61 55 68 30 15 32 59 28 36

48 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59 50 37 75 25 61 55 68 30 15 32 59 28 36

49 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59 50 37 75 25 61 55 68 30 15 32 59 28 36

50 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59 50 37 75 25 61 55 68 30 15 32 59 28 36

51 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59, 61 50 37 75 25 61 55 68 30 15 32 59 28 36

52 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59, 61 50 37 75 25 61 55 68 30 15 32 59 28 36

53 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59, 61, 68 50 37 75 25 61 55 68 30 15 32 59 28 36

54 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59, 61, 68 50 37 75 25 61 55 68 30 15 32 59 28 36

55 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59, 61, 68 50 37 75 25 61 55 68 30 15 32 59 28 36

56 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59, 61, 68, 75 50 37 75 25 61 55 68 30 15 32 59 28 36

57 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59, 61, 68, 75 50 37 75 25 61 55 68 30 15 32 59 28 36

58 Example BST 15, 25, 28, 30, 32, 37, 50, 55, 59, 61, 68, 75 50 37 75 25 61 55 68 30 15 32 59 28 36

59 Lookup The label tells us which half of the tree to search
So, on average, we cut the size of the tree to search almost in half, which we can do log2n times Basis: If T is empty, fail; x not found If T has label x at root, then found Induction: Let T have root label y If x < y lookup x on left subtree If x > y lookup x on right subtree

60 Insertion Insertion function gets tree (reference to node) as an argument and returns a revised tree including inserted element Basis: If T is null create a new node with label x, return reference to the new node Induction: Let root of T have label y. If x < y, insert x into left subtree. The left-subtree refrence of root becomes whatever tree is returned. Same for x > y on right.

61 Deletion 50 37 75 25 61 55 68 30 15 32 59 28 36

62 Deletion: Leaf 50 37 75 25 61 55 68 30 15 32 59 28 36 Just remove it

63 Deletion: Leaf 50 37 75 25 61 55 68 30 15 32 59 28 Just remove it

64 Deletion: One child 50 37 75 25 61 55 68 30 15 32 59 28

65 Deletion: One child 50 37 75 25 61 55 68 30 15 32 59 28 Replace with other child

66 Deletion: One child 50 37 75 25 61 59 68 30 15 32 28 Replace with other child

67 Deletion: One child 50 37 61 25 59 68 30 15 32 28 Replace with other child

68 Deletion: Two children
50 37 61 25 59 68 30 15 32 28

69 Deletion: Two children
50 37 61 25 59 68 30 15 Replace element with leftmost Value in rightmost tree 32 28

70 Deletion: Two children
50 37 61 25 59 68 30 15 Replace element with leftmost Value in rightmost tree 32 28 This element is the “successor” In an inOrder traversal

71 Deletion: Two children
59 37 61 25 68 30 15 Replace element with leftmost Value in rightmost tree 32 28

72 Deletion Basis: If T is empty, just return T.
if x at root, delete root and fix up tree return the fixed up tree Induction: If T has label y at root, delete x from left|right subtree if x<y|x>y. Replace left|right reference by returned tree. Return root.

73 Fixup (Deletemin) If we need to delete the root of T, if it has one null subtree, just return the other subtree Otherwise, find the least element in the right subtree (traverse leftmost path) move it to root.

74 Node class private static class Node { Object element;
Node left = null, right = null, parent; Node (Object element, Node parent) { this.element = element; this.parent = parent; }

75 Example root element left right parent size Eric 1

76 Example root element left right parent size Eric  4 Allen  Soumya 
Jack

77 Runtime depends on the tree
50, 10, 20, 30, 25 25, 20, 30, 10, 50 50 10 20 30 25 25 20 30 50 10 O(log n) O(n)

78 Deletion: Two children
80 15 110 90 105

79 Deletion: Two children
p 80 15 110 90 s 105

80 Deletion: Two children
p 90 15 110 90 s 105

81 Deletion: Two children
p 90 Delete -> 15 110 90 s 105

82 Deletion: Two children
p 90 Delete -> 15 110 90 105 replacement

83 Deletion: Two children
90 15 110 105


Download ppt "Binary Search Trees."

Similar presentations


Ads by Google