0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.

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

Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS202 - Fundamental Structures of Computer Science II
Binary Trees, Binary Search Trees COMP171 Fall 2006.
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Trees, Binary Trees, and Binary Search Trees COMP171.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
AVL Trees Data Structures Fall 2006 Evan Korth Adopted from a presentation by Simon Garrett and the Mark Allen Weiss book.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
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 ),
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
David Stotts Computer Science Department UNC Chapel Hill.
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Data Structures AVL Trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Trees (BST)
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CS 201 Data Structures and Algorithms
AVL Tree 27th Mar 2007.
Binary Trees, Binary Search Trees
AVL Trees CENG 213 Data Structures.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
Binary Trees, Binary Search Trees
Presentation transcript:

0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures (Ch. 6) n Balanced Search Trees: general search structures (Ch ) n Union-Find data structure (Ch. 8.1–8.5) n Graphs: Representations and basic algorithms  Topological Sort (Ch )  Minimum spanning trees (Ch. 9.5)  Shortest-path algorithms (Ch ) n B-Trees: External-Memory data structures (Ch. 4.7) n kD-Trees: Multi-Dimensional data structures (Ch. 12.6) n Misc.: Streaming data, randomization

1 Search Trees n Searching over an ordered universe of key values. n Maintaining hierarchical information n Repeated linear search in link list too slow for large data n Hashing useful only for “find key” operations. n Heaps useful only for “find Min” or “Find Max”. n But suppose we needed:  Lookup-by-prefix:  type in first few letters of a name and output all names in the database beginning with those letters.  Find-in-range[key1, key2]: output all keys between key1 and key2  NearLookup(x): returns the closest key to x when x not in database.  Find the kth smallest key.  Rank(x): how many keys are smaller than x.

2 Search Trees n With appropriate balanced binary search trees, we can do most of these operations in worst-case O(log n) time.  searches, inserts, deletes, find successor, find predecessor etc. n A recursive definition of the tree:  either empty, or  a root node r with 0 or subtrees whose roots are children of r  No other structure: arbitrary distribution of keys and depths

3 Search Trees n Some definitions:  Path: a sequence of edges or nodes  Path Length: number of edges  Depth of a node: number of edges from the root  Parent-Child relationship  Ancestor-Descendant

4 An example: directory structure n File directories have a natural tree (hierarchical) structure n Directory traversal and search best visualized by search trees n Tree Traversals:  List all sub-directories: Pre-Order Traversal  Print a node, then recursively traverse subtrees  Compute directory sizes: Post-Order traversal  Compute sizes of all subtrees, then add to get the size of the directory

5 Binary Search Trees (BST) n Each node has at most 2 children (left, right)  Most commonly used form of search tree  Some examples: Heaps, expression trees, branch-bound  BST implements a search ADT  Input: a set of keys  Any set with an ordering, but assume numeric keys for simplicity  For simplicity, also assume all keys are distinct (no duplicates) n A BST is a tree with the following “ordering” property  For any node X:  All keys in the left subtrees < key(X)  All keys in the right subtree > key(X)

6 Binary Search Trees (BST) n A Binary Search Tree is a tree with the following “ordering” property  For any node X:  All keys in the left subtrees < key(X)  All keys in the right subtree > key(X)  Which of the following are valid BSTs?

7 Find (lookup) Operation in BST if (t = null) return null elseif (x < t.key) return find(x, t.left) elseif (x > t.key) return find(x, t.right) else return t;// match

8 FindMin or FindMax in BST FindMax if (t != null) while (t.right != null) t = t.right return t;

9 Insert Operation in BST n Do find(X). If X found, nothing to do. n Otherwise, insert X at the last spot on the path. if (t = null) t = new node x; // insert x here // elseif (x < t.key) Insert(x, t.left) elseif (x > t.key) Insert(x, t.right) else;

10 Delete Operation in BST n Delete is typically the most complex operation. a. if the node X is a leaf, easily removed. b. if node X has only one child, then just bypass it (make the child directly linked to X). Example. Delete 4

11 Delete Operation in BST c. if node X has two children, replace with the smallest node in the right subtree of X; recursively delete that node. the second deletion turns out to be easy; because the second node can't have two children.

12 Example of Delete n Delete 2. n Swap it with Min in Right Subtree (3). Then Delete (3).

13 Analysis of BST n In the worst-case, BST on n nodes can have height n-1. n Each insert/delete takes O(height) time, same as linked list. n For better worst-case, tree needs to be kept balanced  Namely, height O(log n) with n keys n Given a set of keys, easy to build a perfect BST initially:  use the median division rule. n The problem begins when we start doing insert, delete ops. n Suppose we start from an empty tree and do a series of insertions, what does the tree look like.  Of course, the worst-case is a path: linear depth  What about if insert and deletes were random?

14 Analysis of BST n Analysis shows that if insertions are done randomly (keys drawn from [1,n]), then average depth is log n. n The sum of the depths of all the nodes is O(n log n). n Let D(n) be the internal path length. n If i nodes in the left, and (n – i - 1) on the right, then D(n) = D(i) + D(n - i - 1) + (n - 1). n Under random insertions, the value of i varies uniformly between 0 and n-1, and so D(n) = 2/n (  D(j)) + (n-1) n A famous recurrence, which solves to D(n) = O(n log n).

15 randomly generated Alternate insertions and deletions

16 AVL trees n AvL Trees are a form of balanced binary search trees n Named after the initials of their inventors  Adelson-Velskii and Landis  One of the first to achieve provable guarantee of O(log n) worst-case for any sequence of insert and delete operations.  A binary tree cannot do better!

17 AVL trees n How do we ensure that an AVL tree on n nodes has height O(log n) even if an adversary inserts and deletes key to get tree out of balance? n Simply requiring that the left and right children of the root node have the same height is not enough:  They can each have singly branching paths of length n/2

18 Balance Conditions for AVL trees n Demanding that each node should have equal height for both children too restrictive---doesn’t work unless n = 2 k – 1 n AVL Trees aim for the next best thing:  For any node, the heights of its two children can differ by at most 1  Define height of empty subtree as -1  Height of a tree = 1 + max{height-left, height-right}  Which of the following are valid AVL trees?

19 Height of AvL Trees n Theorem: An AVL tree on n nodes has height O(log n) n What is the minimum number of nodes in a height h AvL tree. n What is the most lop-sided tree?

20 Bound for the Height of AvL Trees n Recursive construction n Let S(h) = min number of nodes in AVL tree of height h S(h) = S(h-1) + S(h-2) + 1 Like Fibonacci numbers. Solves to h = 1.44 log (n+2).

21 Keeping AVL Trees Balanced n Theorem: An AVL tree on n nodes has height O(log n) n Building an initially balanced tree on n keys is easy. n The problem is insertions/deletions cause unbalance. n AVL trees perform simple restructuring operations on the tree to regain balance. n These operations, called rotations, change a couple of pointers. n We will show that an insert, delete, or search operations touches O(1) nodes per level of the tree, giving O(log n) bound.

22 Tree Rotations left rotation right rotation E d b CA A b d CE

23 AVL Tree Rebalancing We only discuss inserts; deletes similar though a bit more messy. n How can an insert violate AVL condition? n Before the insert, at some node x the height difference between children was 1, but becomes 2 after the insert. n We will fix this violation from bottom (leaf) up.

24 AVL Tree Rebalancing Suppose A is the first node from bottom that needs rebalancing. n Then, A must have two subtrees whose heights differ by 2. n This imbalance must have been caused by one of two cases: (1) The new insertion occurred into the left subtree of the left child of A; or the right subtree of the right child of A. (2) The new insertion occurred into the right subtree of the left child of A; or the left subtree of the right child of A. (1) and (2) are different: former is an outside (left-left, or right-right) violation, while the latter is an inside (left-right) or (right-left) violation. We fix (1) with a SINGLE ROTATION; and (2) with a DOUBLE ROTATION.

25 Single Rotation n Insert 6. Where is the AVL condition violated? n Case (1): insertion into the left subtree of the left child of A n Fixed by rotation at 7.

26 Single Rotation: Generic Form

27 Single Rotation: Example Perform inserts in the following order: 3, 2, 1, 4, 5, 6, 7.

28 Failure of the Single Rotation n Single rotation can fail in case (2):  Insert into right subtree of left child or vice versa

29 When Single Rotation Fails: Double Rotation n Need to look one level deeper. n Called Double Rotation: generic form n One of B or C is at level D+2.

30 Double Rotation: Example To the example of previous AVL tree (keys 1-7), insert 16, 15, 14, …., 10, followed by insert 8, 9

31 Removing an element from an AVL tree n Similar process:  locate & delete the element  adjust tree height  perform up to O(log n) necessary rotations n Example n Complexity of operations:  O(h) = O(log n) n : number of nodes, h : tree height

32 Deletion Example delete

33 AVL Tree Complexity Bounds n An AVL Tree can perform the following operations in worst-case time O(log n) each:  Insert  Delete  Find  Find Min, Find Max  Find Successor, Find Predecessor  It can report all keys in range [Low, High] in time O(log n + OutputSize)

34 More illustrations… Single Rotation (left-left) Y cannot be at X ’ s level Y cannot be at Z ’ s level either X Y Z 2 1 X YZ 2 1

35 Single Rotation (right-right) X Y Z 2 1 Z X Y 2 1 Y cannot be at Z ’ s level Y cannot be at X ’ s level either

36 Double Rotation (left-right): Single Won ’ t Work n Single rotation does not work because it does not make Y any shorter X Y Z 2 1 X Y Z 2 1

37 Double Rotation (left-right): First Step n First rotation between 2 and 1 A B D 3 1 A D C 2 B C

38 Double Rotation (left-right): Second Step n Second rotation between 2 and 3 B A D C B A D C

39 Double Rotation (left-right): Summary A B D 3 1 A D C 1 B C

40 Double Rotation (right-left): First Step n First Rotation between 2 and 3 A D C B A D C B

41 Double Rotation (right-left): Second Step n Second Rotation between 1 and 2 B A D C A D C B

42 Double Rotation (right-left): Summary A B D 3 1 A D C 1 B C

43 Insertion into an AVL tree left right AvlTree AvlNode element height AvlTree::insert(x, t) if (t = NULL) then t = new AvlNode(x, …); else if (x < t  element) then insert (x, t  left); if (height(t  left) – height(t  right) = 2) thenif (x < t  left  element ) then rotateWithLeftChild (t); else doubleWithLeftChild (t); else if (t  element < x ) then insert (x, t  right); if (height(t  right) – height(t  left) = 2) thenif (t  right  element < x) then rotateWithRightChild (t); else doubleWithRightChild (t); t  height = max{height(t  left), height(t  right)}+1; n Tree height n Search n Insertion:  search to find insertion point  adjust the tree height  rotation if necessary