Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees II CSC 172 SPRING 2002 LECTURE 15. Binary Trees Every binary tree has two “slots” for children It may have none, either, or both An empty (0-node)

Similar presentations


Presentation on theme: "Trees II CSC 172 SPRING 2002 LECTURE 15. Binary Trees Every binary tree has two “slots” for children It may have none, either, or both An empty (0-node)"— Presentation transcript:

1 Trees II CSC 172 SPRING 2002 LECTURE 15

2 Binary Trees Every binary tree has two “slots” for children It may have none, either, or both An empty (0-node) binary tree is possible As such a node has left and right sub-trees Either or both may be null

3 Data Structure for Binary Trees A node is an object A binary tree is a reference to a node Null references represent empty trees Otherwise, a tree is a reference to its root node Nodes have instance variables leftChild, rightChild null children indicate no sub-tree

4 Binary Tree public class BinTree { Object data; BinTree leftChild; BinTree rightChild; ….// accessor methods }

5 Structural induction on Binary Trees Basis is the empty tree (null reference) rather than a tree of one node; Example: S(T): In a binary tree T represented by leftChild and rightChild references, there is one more null reference than nodes Basis: Empty tree is one null reference, zero nodes

6 Induction Let T not be empty Having left and right children L and R BTIH: S(T) holds for L and R How many “extra” null refs?

7 L n nodes n+1  n+m+1 nodes R m nodes m+1  n+m+2  n+m nodes n+m+2 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

24 Example BST 50 3775 25 15 30 28 32 36 61 5568 59

25 Example BST 50 3775 25 15 30 28 32 36 61 5568 59

26 Example BST 50 3775 25 15 30 28 32 36 61 5568 59

27 Example BST 50 3775 25 15 30 28 32 36 61 5568 59

28 Example BST 50 3775 25 15 30 28 32 36 61 5568 59

29 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15

30 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15

31 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25

32 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25

33 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25

34 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28

35 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28

36 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30

37 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30

38 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32

39 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32

40 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32

41 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32

42 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32

43 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32

44 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32

45 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37

46 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37

47 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50

48 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50

49 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50

50 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50

51 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50, 55

52 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50, 55

53 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50, 55, 59

54 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50, 55, 59

55 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50, 55, 59

56 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50, 55, 59, 61

57 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50, 55, 59, 61

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

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

60 Example BST 50 3775 25 15 30 28 32 36 61 5568 59 15, 25, 28, 30, 32, 37, 50, 55, 59, 61, 68

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

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

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

64 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 log 2 n times Basis: 1. If T is empty, fail; x not found 2. If T has label x at root, then found Induction: Let T have root label y 1. If x < y lookup x on left subtree 2. If x > y lookup x on right subtree

65 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 on right.

66 Deletion 50 3775 25 15 30 28 32 36 61 5568 59

67 Deletion: Leaf 50 3775 25 15 30 28 32 36 61 5568 59 Just remove it

68 Deletion: Leaf 50 3775 25 15 30 28 32 61 5568 59 Just remove it

69 Deletion: One child 50 3775 25 15 30 28 32 61 5568 59

70 Deletion: One child 50 3775 25 15 30 28 32 61 5568 59 Replace with other child

71 Deletion: One child 50 3775 25 15 30 28 32 61 5968 Replace with other child

72 Deletion: One child 50 37 25 15 30 28 32 61 5968 Replace with other child

73 Deletion: Two children 50 37 25 15 30 28 32 61 5968

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

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

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

77 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 hs label y at root, delete x from left|right subtree if x y. Replace left|right reference by returned tree. Return root.

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

79 Example JAVA implementation public class BinSearchTree public BinSearchTree(); public int size(); public Iterator iterator(); // leftmost element public boolean contains (Object o); public boolean add(Object o); public boolean remove (Object elem); // Elements must implement Comparable Entry root; int size;

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

81 Example elementleftrightparentroot size Eric   1

82 Example elementleftrightparentroot size Eric  4 Allen   Soumya  Jack  

83 Constructors & accessors public BinSearchTree() { root = null; } pubic Iterator iterator { return new TreeIterator(); } public int size() { return size; }

84 Contains public boolean contains (Object o) { Entry temp = root; int comp; while (temp != null) { comp = ((Comparable)o).compareTo(temp.element); if (comp == 0) return true; else if (comp < 0) temp = temp.left; else temp = temp.right; } //while return false; }//contains What is the runtime?

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

86 Recursive Contains public boolean contains (Object o) { return containsElement(root,o); } private boolean containsElement(Entry node,Object o){ if (node == null) return false; int comp = ((Comparable)o).compareTo(node.element); if (comp == 0) return true; else if (comp < 0) return containsElement(node.left,o); else return containsElement(node.right,o); return false; } What is the runtime?

87 Adding an element : basis public boolean add(Object o) { if (root == null) { root = new Entry(o,null); size++; return true; }

88 Adding an element : induction else { Entry temp = root; int comp; while (true) { comp = ((Comparable)o).compareTo(temp.element); if (comp == 0) return false; if (comp < 0) if(temp.left != null) temp = temp.left; else { temp.left = new Entry(o,temp); size++; return true; }

89 Adding an element : induction II if(temp.right != null) temp = temp.left; else { temp.right = new Entry(o,temp); size++; return true; } }//while }//root not null }// method add

90 Remove public boolean remove (Object elem) { Entry e = getEntry(elem); if (e != null) return false; deleteEntry(e); return true; }

91 getEntry private Entry getEntry (Object elem) { int comp; Entry e = root; while (e != null) { comp = ((Comparable)o).compareTo(e.element); if (comp == 0) return e; else if (comp < 0) e = e.left; else e = e.right; } return null; }

92 Deletion: Two children 80 15 105 110 90

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

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

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

96 deleteEntry private void deleteEntry (Entry p) { size--; //if p has two children, // replace p’s element with p’s successor’s element // then make p reference that successor if (p.left != null && p.right != null) { Entry s = successor(p); p.element = s.element; p = s; } // p has two children (now we want to delete p)

97 deleteEntry(II) // At this point p has either // no children or one child Entry replacement; if (p.left != null) replacement = p.left; else replacement = p.right;

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

99 deleteEntry(III) // If p has one child, link replacement to parent if (replacement != null) { replacement.parent = p.parent; if (p.parent == null) root = replacement; else if (p == p.parent.left) p.parent.left = replacement; else p.parent.right = replacement; } // p has at least one child

100 deleteEntry(IV) // If p has one child, link replacement to parent else if (p.parent == null) root = null ; else { if (p == p.parent.left) p.parent.left = null; else p.parent.right = null; } // p has parent, but no children }// method deleteEntry

101 successor Private Entry successor(Entry e) { if (e == null) return null; else if (e.right != null) { // successor is leftmost Entry in right subtree Entry p = e.right; while (p.left != null) p = p.left; return p; }

102 Successor II else { //go up the tree to the left as far as possible // then go up to the right Entry p = e.parent; Entry ch = e; while (p != null && ch == p.right) { ch = p; p = p.parent; } return p; }// e has no right child }//method successor

103 TreeIterator Class public class TreeIterator { private Entry lastReturned = null, next; TreeIterator() { next = root; if (next != null) while (next.left != null) next = next.left; }

104 TreeIterator Class public boolean hasNext() { return next != null ; } public Object next() { if (next == null) throw new NoSuchElementException(); lastReturned = next; next = successor(next); return lastReturned.element; }

105 TreeIterator delete() 40 75 5080 20 lastReturned next

106 TreeIterator delete() 40 75 5080 20 lastReturned next

107 TreeIterator delete() 40 75 5080 20 next

108 TreeIterator Class public void remove(){ if (lastReturned.left != null && lastReturned.right != null) next = lastReturned; deleteEntry(lastReturned); lastReturned = null; }


Download ppt "Trees II CSC 172 SPRING 2002 LECTURE 15. Binary Trees Every binary tree has two “slots” for children It may have none, either, or both An empty (0-node)"

Similar presentations


Ads by Google