1 / 12

§5 Splay Trees

Target : Any M consecutive tree operations starting from an empty tree take at most O( M log N ) time. Idea : After a node is accessed, it is pushed to the root by a series of AVL tree rotations. §5 Splay Trees. But if one node takes O( N ) time

kaveri
Download Presentation

§5 Splay Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Target : Any M consecutive tree operations starting from an empty tree take at most O(M log N) time. Idea : After a node is accessed, it is pushed to the root by a series of AVL tree rotations. §5 Splay Trees But if one node takes O(N) time to access, we can keep accessing it for M times, can’t we? Does it mean that every operation takes O(log N) time? So a single operation might still take O(N) time? Then what’s the point? Sure we can – that only means that whenever a node is accessed, it must be moved. The bound is weaker. But the effect is the same: There are no bad input sequences. No. It means that the amortized time is O(log N). 1/12

  2. §5 Splay Trees k5 k5 k5 k4 F k4 k5 k4 F F k1 k3 E k3 k1 E F E k1 k2 k5 k1 D k2 k2 D k3 k2 k4 k2 B k4 F C k1 A B D B k3 E k3 B E C A B C A C A A C D D Does NOT work! 2/12

  3. §5 Splay Trees 2 3 N N 1 N 3 2 2 3 3 1 2 1 N 2 2 3 1 1 1 2 3 An even worse case: Insert: 1, 2, 3, … N Find: 1 1 Find: 2 …… Find: N T (N) = O ( N2 ) 3/12

  4. §5 Splay Trees Rotate X and P G X Single rotation Double rotation P D P G X A A B C D B C G X P D P A X C G B A B C D Try again -- For any nonroot node X , denote its parent by P and grandparent by G: Case 1:P is the root Case 2:P is not the root Zig-zag Zig-zig 4/12

  5. §5 Splay Trees k5 k5 k4 k4 F F k1 k1 k3 E E k2 k2 k2 k3 D k4 k1 B B D A k3 k5 C D F A A C E C B Splaying not only moves the accessed node to the root, but also roughly halves the depth of most nodes on the path. 5/12

  6. §5 Splay Trees 7 7 6 7 1 6 5 6 6 5 4 1 7 4 4 3 4 2 5 1 2 5 2 3 2 1 3 3 Insert: 1, 2, 3, 4, 5, 6, 7 Find: 1 Read the 32-node example given in Figures 4.52 – 4.60 6/12

  7. §5 Splay Trees Deletions: X will be at the root.  Step 1: Find X ; Home work: p.136 4.23 Access a splay tree There will be two subtrees TL and TR .  Step 2: Remove X ; The largest element will be the root of TL , and has no right child.  Step 3: FindMax ( TL ) ;  Step 4: Make TR the right child of the root of TL . Are splay trees really better than AVL trees? 7/12

  8.  Other Operations on Binary Search Trees  Sort: List the elements in increasing order Solution: inorder traversal.  Get Height: Compute the heights of the nodes Solution: postorder traversal.  Get Depth: Compute the depths of the nodes Solution: preorder traversal. 8/12

  9. 21 25 31 48 72 41 12 15 59 84 91 1,4,8,11 12,13 15,18,19 21,24 25,26 31,38 41,43,46 48,49,50 59,68 72,78 84,88 91,92,99 < < < < < < < < < < < §6 B-Trees 【Definition】A B-tree of order M is a tree with the following structural properties: (1) The root is either a leaf or has between 2 and M children. (2) All nonleaf nodes (except the root) have between M/2 and M children. (3) All leaves are at the same depth. Assume each nonroot leaf also has between M/2 and M children. Each interior node contains M pointers to the children. And M  1 smallest key values in the subtrees except the 1st one. All the actual data are stored at the leaves. A B-tree of order 4 (2-3-4 tree) 9/12

  10. §6 B-Trees 22: 22: 16: 11:16 41:58 41:58 1, 8 8,11,12 11,12 16,17 16,17,18 22,23,31 22,23,31 41,52 41,52 58,59,61 58,59,61 16:22 11: 18: 41:58 1, 8 11,12 16,17 18,19 22,23,31 41,52 58,59,61 A B-tree of order 3 (2-3 tree)   16,17,18  Find:52  Insert:18  Insert:1  Insert:19  Insert:28 10/12

  11. §6 B-Trees 22: 16: 41: 11: 18: 28: 58: 1, 8 11,12 16,17 18,19 22,23 28,31 41,52 58,59,61  Insert:70 First find a sibling with 2 keys and adjust. Keep more nodes full.  Deletion is similar to insertion except that the root is removed when it loses two children. 11/12

  12. §6 B-Trees T = O(M) For a general B-tree of order M Btree Insert ( ElementType X, Btree T ) { Search from root to leaf for X and find the proper leaf node; Insert X; while ( this node has M+1 keys ) { split it into 2 nodes with (M+1)/2 and (M+1)/2  keys, respectively; if (this node is the root) create a new root with two children; check its parent; } } Home work: p.138 4.36 Access a 2-3 tree T(M, N) = O( (M/log M) log N ) Depth(M, N) = O( logM/2 N  ) Note: The best choice of M is 3 or 4. TFind(M, N) = O( log N ) 12/12

More Related