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.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Outline Scapegoat Trees ( O(log n) amortized time)
CS 171: Introduction to Computer Science II
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Trees, Binary Trees, and Binary Search Trees COMP171.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Marc Smith and Jim Ten Eyck
Binary Trees Chapter 6.
Data Structures Using C++1 Chapter 11 Binary Trees.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Outline Priority Queues Binary Heaps Randomized Mergeable Heaps.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Trees Chapter 15.
Binary Search Tree (BST)
Trees.
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees, Binary Search Trees
Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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 and a right child each of which is a binary tree

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

The depth of a node is its distance from the root 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

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]

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

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.

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

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 ); }

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; }

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 }

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

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 ); }

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 ); }

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

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 For a leaf w, depth(w)=pre(w)-post(w) size(u) = ?

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

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

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

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

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

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 i i j j m m 10

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

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

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.

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]

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]

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]

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

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

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=

Removing in a Binary Search Tree We want to delete a node u containing x from a binary search tree X= 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

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

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? ,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

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 ,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

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

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

quicksort(S): Quicksort 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} – quicksort(S < ) – quicksort(S > )

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 S S

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.

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 ≈ log 2 n Search paths in random binary search trees are nearly 30% shorter than in skiplists Which is better?

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

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

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

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

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

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

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

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

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)

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, , 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

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, 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

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, 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

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

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, ,.51

Insertion into a Treaps 5,.02 2,.318,.15 1,.494,.537,.289,.61 0,.77 3,.72 6, ,.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)

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

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)