CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia AVL Trees v z.
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.
1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
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.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
© 2004 Goodrich, Tamassia Binary Search Trees   
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
Binary Search Trees1 Part-F1 Binary Search Trees   
BST Data Structure A BST node contains: A BST contains
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
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.
CSC 213 – Large Scale Programming. Implementing Map with a Tree  Accessing root much faster than going to leaves  In real-world, should place important.
CSC 213 – Large Scale Programming. Today’s Goal  Review Map & Dictionary implementations  What do they do well? When would they be used?  Why do they.
CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
Search Trees Last Update: Nov 5, 2014 EECS2011: Search Trees1 “Grey Tree”, Piet Mondrian, 1912.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay Trees.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
CSC 213 Lecture 8: (2,4) Trees. Review of Last Lecture Binary Search Tree – plain and tall No balancing, no splaying, no speed AVL Tree – liberté, égalité,
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
Binary Search Trees (BST)
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
CHAPTER 10 SEARCH TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Part-D1 Binary Search Trees
Binary Search Trees < > =
AA Trees.
COMP9024: Data Structures and Algorithms
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Search Trees.
Binary Search Trees < > =
BST Trees
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Binary Search Trees < > = Binary Search Trees
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Binary Search Trees < > = Binary Search Trees
AVL Trees: AVL Trees: Balanced binary search tree
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Binary Search Trees < > =
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Binary Search Trees < > =
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CSC 213 Lecture 7: Binary, AVL, and Splay Trees

Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries): Lower keys are in left subtrees Higher keys are in right subtrees Inorder traversal visits entries in order of their keys

Search (§ 9.1.1) Searches start at root If key we are searching for is lower, we go to left child If key is higher, go to right child If a match, return entry If we reach the end, throw exception Example: TreeSearch(4, root ) Algorithm TreeSearch(k, v) if v.isExternal() /* throw exception */ if k  key(v) return TreeSearch(k, v.left()) else if k  key(v) return v else /* k  key(v) */ return TreeSearch(k, v.right())   

Search (§ 9.1.1) TreeSearch(5, root )    Algorithm TreeSearch(k, v) if v.isExternal() /* throw exception */ if k  key(v) return TreeSearch(k, v.left()) else if k  key(v) return v else /* k  key(v) */ return TreeSearch(k, v.right())

Insertion First, search for k If k is not in tree, add new node where search ends Else, replace value in entry Insert(5)   

Deletion For remove( k ), again start with search for key k If k is not in tree, throw exception Example: remove(4) Node v has only one child; replace v with its child node w v w  

Deletion (cont.) Example: remove(3) Node v has two children Find node w that follows v in an inorder traversal Go to right child of v and then keep taking left children Copy w’s entry into v Replace w with z, w ’s right child v w v 2 z z

Performance Needs one node per entry: Space is O(n) Find, insert, remove take O(h) time, where h is height of tree Worst case: O(n) Best case: O(log n)

AVL Tree Definition (§ 9.2) AVL trees are balanced trees AVL Tree is a BST such that the heights of a node’s children differ by at most 1. Balanced AVL tree where heights are shown in red

Insertion in an AVL Tree First step of insertion is identical to BST insertion Example: insert(54) before insertionafter insertion

Trinode Restructuring Suppose insertion causes tree to become unbalanced Perform rotations so b becomes topmost node Case 1: Single rotation (e.g., left rotation about a b a c T0T0 T1T1 T2T2 T3T3 b a c T0T0 T1T1 T2T2 T3T3

Trinode Restructuring Suppose insertion causes tree to become unbalanced Perform rotations so b becomes topmost node Case 2: Double rotation (right rotation about c, then left rotation about a ) c b a T0T0 T1T1 T2T2 T3T3 b ca T0T0 T1T1 T2T2 T3T3

Removal in an AVL Tree Removal begins just like in a BST, but this may cause an imbalance Remove(32): before deletion of 32after deletion

Rebalancing after a Removal After removal, need to travel up the tree to see if it is balanced Must restructure nodes where balance has been lost Restructuring may unbalance higher nodes, so must continue checking until root is reached c b a

Running Times for AVL Trees Single rebalance/restructure is O(1) Single application does constant amount of work Tree remains balanced – its height stays O(log n) Find takes time O(log n) Insert takes time _____________ Initial find is O(log n) Could do at most _______________________ rebalances Remove takes time _______________ Initial find is O(log n) Could do at most _______________________ restructures

Splay Trees are BSTs (§ 9.3) Splay tree follows rules of a BST: Nodes in the left subtree have smaller keys Nodes in right subtree have larger keys Nodes with equal keys must all be in left or all be in right subtree Inorder traversal returns entries in order of their keys

Searching in a Splay Tree Find starts like any BST search Example: find(11) Not in tree, eventually must throw an exception

Searching in a Splay Tree Example: find(8) Return entry once we are done

Splay Trees Always Restructure Splay trees splay themselves after every find, insert and remove Use rotations to move a node up the tree Stop splaying only when the node becomes tree’s root Splaying keeps the most recently used items near the top of the tree

Left rotation about the rootRight rotation about the root Splay Trees Always Rebalance New operation: splay Splaying uses rotations to move node up to root 3 types of rotations used in splaying Single rotation is used ONLY when parent node is the root node root x T1T1 T2T2 T3T3 x T1T1 T2T2 T3T3 x T1T1 T2T2 T3T3 x T1T1 T2T2 T3T3

Splay Trees Always Rebalance Double rotations normally used Rotation for when node & parent are both left children (similar for both right children) paren t x T1T1 T2T2 T3T3 grandparent T4T4 parent grandparent T4T4 T3T3 T2T2 x T1T1

Splay Trees Always Rebalance Double rotations normally used Rotation for when node & parent are not similar type children parent x T2T2 T3T3 T4T4 grandparent T1T1 parent x T2T2 T3T3 T4T4 grandparent T1T1

Which Node to splay? The node to be splayed depends on the operation we just completed: methodnode to splay find If key found, splay that node If not found, splay last node reached in the search insert Splay the new node which holds the inserted entry remove If key found, splay parent of the node removed If not found, splay last node reached in the search

Performance of Splay Trees Amortized cost of splaying is O(log n) Find frequently-requested entries in O(1) time! Where would you use a splay tree? Where would splay trees be a BAD IDEA ™ ?

Your Turn Insert 61, 98, 95, 48, 89, 63, 24, and 62 into a BST, an AVL tree, & splay tree Search for 61, 24, and 0 within your BST, AVL tree, and splay tree from above. Show the search path and any changes to your tree after each search.

Daily Quiz List 2 real applications where you would use each of the 3 tree data structures from this lecture and explain why your tree data structure makes sense.