Presentation is loading. Please wait.

Presentation is loading. Please wait.

Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.

Similar presentations


Presentation on theme: "Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child."— Presentation transcript:

1 Outline Binary Trees Binary Search Tree Treaps

2 Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child and a right child each of which is a binary tree

3 A node is the parent of its children A node with no parent is called a root A node with no children is called a leaf, non-leaves are called internal nodes. x’sParent x x x’s Children Root Leaves Binary Trees

4 The depth of a node is its distance from the root 0 1 2 3 4 x x X’s depth A level is the set of nodes with the same depth The height of a binary tree is the maximum depth of any node The size of a tree is the number of nodes in the tree Level 2 Height Size = 14

5 Binary Trees A full binary tree is one in which every internal node has 2 children Prove: A full binary tree with n leaves has n - 1 internal nodes A complete binary tree is one in which every level is full, except possibly the last level Prove: A complete binary tree of height k has size in the range [2 k ; 2 k+1 -1]

6 Applications In an expression tree Each leaf represents a value (variable or constant) Each internal node represents a binary operator Expression trees are used inside of compilers. In a binary search tree, each node u has a value u.x For every node u, u.left.x < u.x < u.right.x Binary search trees can implement SortedSets and SortedMaps

7 Traversing Binary Trees How to traverse all the node of the binary tree? If the tree is empty (null) do nothing. Otherwise Traverse the left subtree. Traverse the right subtree.

8 Traversing Binary Trees How can we do it without recursion? Where we go next depends on the previous node 1. If previous node was our parent then go to left child 2. If previous node was left child then go to our right child 3. If previous node was right child then go to our parent Rule 3 is problematic How do we know our parent? We can either use a stack to maintain the current path to the root, or maintain a parent pointer for each node

9 Preorder Traversal Visit u before either of its children. void traverse ( Node u) { if (u == null ) return ; // visit u here traverse (u. left ); traverse (u. right ); } 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 11 12 13

10 Preorder Example ( making a copy of the tree) To copy the subtree rooted at u we First create a copy of u Then copy u.left and u.right recursively protected Node copyTo ( Node u, BinaryTree t) { if (u == null ) return null ; Node w = t. new Node (); w. left = copyTo (u.left, t); w. right = copyTo (u.right, t); return w; }

11 Postorder Traversal Visit u after visiting both of its children. void traverse ( Node u) { if (u == null ) return ; traverse (u. left ); traverse (u. right ); // visit u here } 13 7 7 1 1 0 0 6 6 2 2 5 5 3 3 4 4 12 8 8 11 9 9 10

12 Postorder Example (computing the size) (evaluating expression tree) The size of node u is the size of its children + 1 We need to know the result of u’s children before compute the expression for u Value eval ( Node u) { if (u. isLeaf ()) return u. value ; Value vl = eval (u. left ); Value vr = eval (u. right ); return u.op(u.left, u. right ); + * - * / + / * + x 2 3 5y a b6 (x+2)(3-5y) (a(b+6))

13 Inorder Traversal Visit u after visiting its left child and before visiting its right child. void traverse ( Node u) { if (u == null ) return ; traverse (u. left ); // visit u here traverse (u. right ); } 8 8 2 2 1 1 0 0 4 4 3 3 6 6 5 5 7 7 10 9 9 12 11 13

14 Inorder Example ( printing binary search tree) To keep the order we need to print u after the left child and before the right child void printInOrder ( Node u) { if (u == null ) return ; printInOrder (u. left ); System.out. println (u.x); printInOrder (u. right ); }

15 Complete Example (printing expression tree) Some problems require all three String toString ( Node u) { if (u. isLeaf ) return u. value. toString (); return "(" // pre + toString (u. left ) + u.op // in + toString (u. right ) + ")"; // post} + * - * / + / * + x 2 3 5y a b6 ( ( ( x+2 )( 3 – ( 5 * y ) ) ) / ( a * ( b + 6 ) ) )

16 Traversing Binary Trees Pre-, in-, and post- order numberings allow us to deduce some information about nodes u is an ancestor of w if and only if pre(u) post(w) + 1,8 5,7 7,6 2,2 0,14 10,13 12,12 3,0 4,1 6,3 8,4 9,5 11,9 13,10 14,11 u u w w v v u (1,8), w (9,5) 1 5 v (10,3), w (9,5) 10 > 9 and 13 > 5 Depth w (9,5) = 4 9 - 5 = 4 For a leaf w, depth(w)=pre(w)-post(w) size(u) = ?

17 Higher Degree Tree Non-binary trees are also possible (and sometimes useful)

18 Binary trees are useful for representing lots of things Inorder, Preorder, and Postorder traversal solve a lot of problems – All run in O(n) time on a tree with n nodes Binary Trees Summary

19 23401 78956 i i j j m m 10 Binary search is used to search in a sorted array a of Comparable values for some value x – If a.length == 1 then quit – Compare x with a[a.length/2] If x == a[a.length/2] then we have found x, quit If x < a[a.length/2] then we recursively search – a[0],...,a[a.length/2-1] Else (x > a[a.length/2]) then we recursively search – a[a.length/2]+1,...,a[a.length-1] Binary Search

20 23401 78956 i i j j m m 10 Binary search is used to search in a sorted array a of Comparable values for some value x – If a.length == 1 then quit – Compare x with a[a.length/2] If x == a[a.length/2] then we have found x, quit If x < a[a.length/2] then we recursively search – a[0],...,a[a.length/2-1] Else (x > a[a.length/2]) then we recursively search – a[a.length/2]+1,...,a[a.length-1] Binary Search

21 23401 78956 i i j j m m 10 Binary search is used to search in a sorted array a of Comparable values for some value x – If a.length == 1 then quit – Compare x with a[a.length/2] If x == a[a.length/2] then we have found x, quit If x < a[a.length/2] then we recursively search – a[0],...,a[a.length/2-1] Else (x > a[a.length/2]) then we recursively search – a[a.length/2]+1,...,a[a.length-1] Binary Search

22 Binary search is used to search in a sorted array a of Comparable values for some value x – If a.length == 1 then quit – Compare x with a[a.length/2] If x == a[a.length/2] then we have found x, quit If x < a[a.length/2] then we recursively search – a[0],...,a[a.length/2-1] Else (x > a[a.length/2]) then we recursively search – a[a.length/2]+1,...,a[a.length-1] Binary Search 23401 78956 i i j j m m 10

23 At each iteration of binary search we perform 1 comparison in O(1) time – This comparison either: 1.ends the algorithm (because we find x) or 2.reduces the size of the search range by (at least) a factor of 2 How often can we divide a.length by 2 before we get 1? – log 2 (a.length) On an array of length n, binary search finishes after log 2 n iterations and runs in O(log n) time in the worst case. Analysis of Binary Search

24 O(log n) is much faster than O(n) There are cases in which binary search need log n iterations. – Ex: If we look for an element lower than the minimum. Analysis of Binary Search

25 Theorem: When run on a sorted array a of length n, binary search performs log 2 n + 1 comparisons and runs in O(log n) time in the worst case A sorted array can be used to implement the SortedSet – operations contains(), headSet(), tailSet(), and subSet() in O(log n) time per operation What about the add() and remove() operations? Binary Search Summary Adding/removing an element to/from a sorted array is slow – Takes O(1 + n - i ) time if the rank of the element is i.

26 23401 78956 10 Binary Search and Binary Search Tree Binary search defines an implicit binary tree on the elements of a: a[a.length/2] is the root the left subtree contains a[0]...a[a.length/2-1] the right subtree contains a[a.length/2+1]...a[a.length-1]

27 23401 789 5 6 10 Binary Search and Binary Search Tree Binary search defines an implicit binary tree on the elements of a: a[a.length/2] is the root the left subtree contains a[0]...a[a.length/2-1] the right subtree contains a[a.length/2+1]...a[a.length-1]

28 2 3401 7 8 9 5 6 10 Binary Search and Binary Search Tree Binary search defines an implicit binary tree on the elements of a: a[a.length/2] is the root the left subtree contains a[0]...a[a.length/2-1] the right subtree contains a[a.length/2+1]...a[a.length-1]

29 2 3 4 0 1 7 8 9 5 6 10 Binary Search and Binary Search Tree A binary search tree is a binary tree where each node u stores a distinct value u.x (Comparable) For every node u all values stored in the subtree u.left are smaller than u.x For every node u all values stored in the subtree u.right are larger than u.x

30 2 3 4 0 1 7 8 9 5 6 10 Searching in a Binary Search Tree A binary search tree is a binary tree where each node To find a value x in a binary search tree: If x == root.x then we have found x If x < root.x then recursively search in root.left Else (x > root.x) then recursively search in root.right x=3 3

31 2 3 4 0 1 7 8 9 5 6 10 Inserting into a Binary Search Tree To insert a new value x in a binary search tree: We search for x The last node in the search path becomes the parent of x. X=4.5 x=4.5 4 4.5 2 5

32 2 3 4 0 1 7 8 9 5 6 10 Removing in a Binary Search Tree We want to delete a node u containing x from a binary search tree X=4.5 4.5 This is easy if u is a leaf (x= 4.5) This is easy if u has only one child (x=10) What if u has two children? (x=5) 4.5 x=10 10 x=5

33 2 3 4 0 1 7 8 9 5 6 10 Removing in a Binary Search Tree If x has two children then we want to replace u.x with something that is easy to delete w w 4.5 10 Go to u.right Go left until reaching a node w with no left child (w.left == null) Delete w and set u.x = w.x u u w w w w 6 6

34 How big is d(x) the length of the search path for x? Analysis of Binary Search Trees – It depends on the order in which elements were inserted – Which of the following is more likely? 14121086420 13951 113 7 0 1 13 14 14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 7,3,11,1,5,9,13,0,2,4,6,8,10,12,14

35 Analysis of Binary Search Trees There is only one way to get the left picture There are thousands of ways to get the right picture 14121086420 13951 113 7 0 1 13 14 14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 7,3,11,1,5,9,13,0,2,4,6,8,10,12,14 7,3,11,1,5,9,13,0,2,4,6,8,10,14,12 7,3,11,1,5,9,13,0,2,4,6,12,10,14,8

36 A random binary search tree is obtained by: – Starting with a set S of n distinct elements – Randomly permuting S – Inserting elements of S one at a time into a binary search tree Random Binary Search Trees Theorem: For any particular value x ϵ S, the expected value of d(x) is at most 2 ln n. Theorem: The expected height is at most 4.311 ln n The expected time to search for an element is O(log n) The expected time needed to build a random binary search tree is O(n log n) (this is quicksort)

37 quicksort(S): Quicksort 72158 63049 5 p=5 1. Pick a random element p from S

38 quicksort(S): Quicksort 43021 79658 p=5 S<S< S<S< S=S= S=S= S>S> S>S> – Pick a random element p from S – Compare every element to p to get 3 sets S < = {x ϵ S : x < p} S = = {x ϵ S : x = p} S > = {x ϵ S : x > p} 23401 78956 – quicksort(S < ) – quicksort(S > )

39 quicksort(S): – Pick a random element p from S – Compare every element to p to get 3 sets S < = {x ϵ S : x < p} S = = {x ϵ S : x = p} S > = {x ϵ S : x > p} – quicksort(S < ) – quicksort(S > ) – return S S Quicksort 23401 78956 S S

40 quicksort(S): Quicksort Build a random binary search tree ( buildrbst(S) ): – Pick a random element p from S – Compare every element to p to get 3 sets S < = {x ϵ S : x < p} S = = {x ϵ S : x = p} S > = {x ϵ S : x > p} – quicksort(S ) and return S S – Pick a random element p, make p the root and delete it from – For each element x S if x < p insert x in left subtree if x = p do nothing if x > p insert x in right subtree Quicksort performs exactly the same comparisons as those done when building a random binary search tree O(n log n) expected time.

41 Random BST The expected length of a search path Skiplist 2 ln n = 2 log e n 2 log 2 n Random binary search trees versus skiplists 2 ln n = 2 log e n ≈ 1.3867 log 2 n Search paths in random binary search trees are nearly 30% shorter than in skiplists Which is better?

42 Binary search can search a sorted array of n elements in O(log n) time using at most log2 n + 1 comparisons – But don't support efficient insertion or deletion Summary Binary search trees can store elements for – Searching – Inserting – Deleting – But there is no guarantee (other than O(n)) on the running time Random binary search trees can support search in O(log n) expected time

43 We want to use the properties of about random binary search trees in a dynamic binary search tree ( O(log n) expected value of d(x) ) – Supports insertions and deletions Treap: Randomized Search Trees Problem: after inserting/deleting in a random binary search tree, the resulting binary search tree is no longer random Solution: Make the tree “look like" a random binary search tree after each insertion/deletion

44 A treap is binary search tree where each node u stores Treap: Randomized Search Trees 4,.53 9,.61 6,.69 3,.72 5,.02 8,.15 7,.28 2,.31 0,.77 1,.49 – A key u.x (we search for these) – A priority u.prio (these are assigned randomly) Treap is a binary search tree on the keys Treap is a heap on the priorities

45 A treap is binary search tree where each node u stores Treap: Randomized Search Trees – A key u.x (we search for these) – A priority u.prio (these are assigned randomly) Treap is a binary search tree on the keys Treap is a heap on the priorities – Node u with minimum u.prio is the root – Subtrees are built recursively 4,.53 9,.61 6,.69 3,.72 5,.02 8,.15 7,.28 2,.31 0,.77 1,.49

46 Treap: Randomized Search Trees 4,.53 9,.61 6,.69 3,.72 5,.02 8,.15 7,.28 2,.31 0,.77 1,.49 5,.02 4,.53 3,.72 2,.31 0,.77 1,.49 9,.61 6,.69 8,.15 7,.28

47 Treap: Randomized Search Trees 4,.53 9,.61 6,.28 3,.72 5,.02 8,.15 7,.69 2,.31 0,.77 1,.49 5,.02 4,.53 3,.72 0,.77 1,.49 2,.31 9,.61 6,.28 8,.15 7,.69

48 Treap: Randomized Search Trees 4,.53 9,.61 6,.28 3,.72 5,.02 8,.15 7,.69 2,.31 0,.77 1,.49 5,.02 4,.53 3,.72 0,.77 1,.49 2,.31 9,.61 6,.28 8,.15 7,.69

49 Treap: Randomized Search Trees Alternative (equivalent) definition for treaps: – Sort ( x,prio ) pairs by priority – Insert one at a time into a binary search tree Since a treap is random binary search tree, it inherits all their properties – Expected depth d(x) and height O(log n) How do we support insertions and deletions? We need a way to make the treap “look random again" after an insertion or deletion

50 Treap: Randomized Search Trees u v AB C u v A BC Rotate right Rotate left A < v.x < B < u.x < C is preserved – Left Turn: u moves up in the tree (v moves down) – Right Turn: v moves up in the tree (u moves down)

51 Insertion into a Treaps 5,.02 To insert the value x into a treap : 2,.318,.15 1,.494,.537,.289,.61 0,.773,.726,.69 3.5,.51 1. Make a new node u with u.x=x and a random priority u.prio 2. Insert u as usual (so that u is a leaf) 3.5,.51

52 Insertion into a Treaps 5,.02 To insert the value x into a treap : 2,.318,.15 1,.494,.537,.289,.61 0,.77 3,.72 6,.69 1. Make a new node u with u.x=x and a random priority u.prio 2. Insert u as usual (so that u is a leaf) 3. While u is not the root and u.prio < u.parent.prio I Do a rotation at u.parent to move u upwards 3.5,.51

53 Insertion into a Treaps 5,.02 To insert the value x into a treap : 2,.318,.15 1,.49 4,.53 7,.289,.61 0,.773,.726,.69 1. Make a new node u with u.x=x and a random priority u.prio 2. Insert u as usual (so that u is a leaf) 3. While u is not the root and u.prio < u.parent.prio I Do a rotation at u.parent to move u upwards 3.5,.51

54 Treap: Randomized Search Trees To insert the value x into a treap: 1.Make a new node u with u.x=x and a random priority u.prio 2.Insert u as usual (so that u is a leaf) 3.While u is not the root and u.prio < u.parent.prio I Do a rotation at u.parent to move u upwards Step 1 Takes O(1) time Step 2 Takes O(d(x)) = O(log n) expected time Step 3 Takes at most O(d(x)) = O(log n) expected time Insertion into a treap takes O(log n) expected time

55 Insertion into a Treaps 5,.02 To delete the node u from a treap: 1.While u is not a leaf Do a rotation at u to move u downward left or right rotation depends if – (u.left.prio < u.right.prio) 2,.318,.15 1,.49 4,.53 7,.289,.61 0,.773,.726,.69 3.5,.51

56 Insertion into a Treaps 5,.02 2,.318,.15 1,.494,.537,.289,.61 0,.77 3,.72 6,.69 3.5,.51 To delete the node u from a treap: 1.While u is not a leaf Do a rotation at u to move u downward left or right rotation depends if – (u.left.prio < u.right.prio)

57 Insertion into a Treaps 5,.02 When u is a leaf: 2,.318,.15 1,.494,.537,.289,.61 0,.773,.726,.69 2. Remove u as usual (so that u is a leaf) 3.5,.51 Deletion perform the same rotation that insertion in reverse order

58 Treap: Randomized Search Trees The quicksort recursion tree is a random binary search tree Theorem: A treap storing n elements supports the operations search(x), insert(x), delete(x) in O(log n) expected time per operation Treaps can implement SortedSet/SortedMap interfaces in O(log n) expected time per operation The expected number of comparisons done during a search is at most 2 ln n (better than skiplists) The expected number of rotations done during an insertion/deletion is only O(1) (this is true, but we didn't prove it)


Download ppt "Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child."

Similar presentations


Ads by Google