Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "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):"— Presentation transcript:

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

2 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 6 92 418

3 4 4 2 2 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()) 6 9 1 8   

4 Search (§ 9.1.1) TreeSearch(5, root ) 6 9 2 4 1 8    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())

5 Insertion First, search for k If k is not in tree, add new node where search ends Else, replace value in entry Insert(5) 6 92 418 6 9 2 4 18 5   

6 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 6 9 2 4 18 5 v w 6 9 2 5 18  

7 5 5 8 6 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 3 1 8 6 9 v w 2 5 1 8 6 9 v 2 z z

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

9 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 6 92 41 8 1 1 1 5 2 3 2 4

10 Insertion in an AVL Tree First step of insertion is identical to BST insertion Example: insert(54) 44 17 78 32 50 88 48 62 54 44 1778 325088 4862 before insertionafter insertion

11 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

12 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

13 Removal in an AVL Tree Removal begins just like in a BST, but this may cause an imbalance Remove(32): 44 17 78 32 50 88 48 62 54 44 17 7850 88 48 62 54 before deletion of 32after deletion

14 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 44 17 7850 88 48 62 54 c b a 44 17 78 5088 48 62 54

15 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

16 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 20 3721 14 7 3510 1 2 5 6 8 36 9 40

17 Searching in a Splay Tree Find starts like any BST search Example: find(11) Not in tree, eventually must throw an exception 20 3721 14 7 35 10 1 2 5 6 8 36 9 40

18 Searching in a Splay Tree Example: find(8) Return entry once we are done 20 3721 14 7 35 10 1 2 5 6 8 36 9 40

19 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

20 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

21 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

22 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

23 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

24 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 ™ ?

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

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


Download ppt "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):"

Similar presentations


Ads by Google