AVL tree binary search tree tree height left
AVL tree คอ binary search tree ททกโนดใน tree ม height ของ left และ right subtrees แตกตางกนไมเกน 1 (height ของ 6 6 empty 2 tree ใหเปน -1) 2 8 8 1 4 3 7 1 4 3 Figure 4. 29 Binary search trees 2 5 4
Insert node make AVL unbalance 9
2 Add 6 Rotate 2 4 4 4 1 3 5 2 5 1 6 3 6 Add 7 Rotate 5 6 4 4 5 2 1 3 6 2 6 1 3 5 7 7 14
Left-right double rotation สำหรบกรณ 2 k k k 3 3 2 k 1 k A k D k 2 B C 1 C A D 2 A k k 1 3 B C D B 16
Right-left rotation สำหรบกรณ 3 A k k k 1 1 2 k k D 2 B A 3 C k k k 2 1 3 k B A 3 C B C D D 17
4 4 6 2 1 3 5 Add 14 Double Rotate 7 15 6 7 2 1 15 7 3 6 5 16 14 7 Add 13 Rotate 4 7 2 3 16 14 4 1 15 6 5 2 15 14 15 16 1 6 3 5 14 16 13 13 19
7 7 4 2 1 6 3 Add 12 Rotate 13 14 4 15 14 5 2 16 1 13 6 3 13 5 12 16 14 12 7 4 Add 11 Rotate Add 10 Rotate Add 8 15 13 2 1 6 3 5 11 10 15 12 14 16 8 20
Routine ในโปรแกรม *********PUBLIC OPERATIONS*********** // void insert( x ) --> Insert x // void remove( x ) --> Remove x (unimplemented) // Comparable find( x ) --> Return item that matches x // Comparable find. Min( ) --> Return smallest item // Comparable find. Max( ) --> Return largest item // boolean is. Empty( ) --> Return true if empty; else false // void make. Empty( ) --> Remove all items // void print. Tree( ) --> Print tree in sorted order 23
class Avl. Node { Avl. Node( Comparable the. Element ){ this( the. Element, null ); } Avl. Node( Comparable the. Element, Avl. Node lt, Avl. Node rt ) { element = the. Element; left = lt; right = rt; height = 0; } // Friendly data; accessible by other package routines Comparable element; // The data in the node Avl. Node left; // Left child Avl. Node right; // Right child int height; // Height } Figure 4. 37 การประกาศ node ของ AVL trees 24
public class Avl. Tree { public Avl. Tree( ){ root = null; } public void insert( Comparable x ){root=insert(x, root); } public void remove( Comparable x ){/* detete */}; } public Comparable find. Min( ) { return element. At( find. Min( root ) ); } public Comparable find. Max( ) { return element. At( find. Max( root ) ); } public Comparable find( Comparable x ) { return element. At( find( x, root )); } public void make. Empty( ) { root = null; } public boolean is. Empty( ) { return root == null; } // Other private functions go here /** The tree root. */ private Avl. Node root; 25
/** * Return the height of node t, or -1, if null. */ private static int height( Avl. Node t ) { return t == null ? -1 : t. height; } Figure 4. 38 Method สำหรบคำนวณ height ของ AVL trees 26
private Avl. Node insert( Comparable x, Avl. Node t ) { if ( t == null ) t = new Avl. Node( x, null ); else if(x. compare. To( t. element ) < 0 ) { t. left = insert( x, t. left ); if(height(t. left)-height(t. right)==2 ) if(x. compare. To(t. left. element) < 0 ) t = rotate. With. Left. Child( t ); else t = double. With. Left. Child( t ); } else if(x. compare. To(t. element) > 0 ) { t. right = insert( x, t. right ); if(height(t. right)-height(t. left)==2) if (x. compare. To(t. right. element) >0) t = rotate. With. Right. Child( t ); else t = double. With. Right. Child( t ); } else ; // Duplicate; do nothing t. height = max(height( t. left ), height( t. right )) + 1; return t; } Figure 4. 39 การบรรจคาลงใน trees AVL 27
/* Rotate binary tree node with left child. * For AVL trees, this is a single rotation for case 1. * Update heights, then return new root. */ private static Avl. Node rotate. With. Left. Child( Avl. Node k 2 ) { Avl. Node k 1 = k 2. left; k 2. left = k 1. right; k 1. right = k 2; k 2. height = max( height( k 2. left ), height( k 2. right )) + 1; k 1. height = max( height( k 1. left ), k 2. height ) + 1; return k 1; } Figure 4. 41 Routine สำหรบ single rotation 28
/* Rotate binary tree node with right child. * For AVL trees, this is a single rotation for case 4. * Update heights, then return new root. */ private static Avl. Node rotate. With. Right. Child(Avl. Node k 1 ) { Avl. Node k 2 = k 1. right; k 1. right = k 2. left; k 2. left = k 1; k 1. height = max( height( k 1. left ), height( k 1. right )) + 1; k 2. height = max( height( k 2. right ), k 1. height ) + 1; return k 2; } 29
/* Double rotate binary tree node: * first left child with its right child; * then node k 3 with new left child. * For AVL trees, this is a double rotation for case 2. * Update heights, then return new root. */ private static Avl. Node double. With. Left. Child ( Avl. Node k 3 ) { k 3. left = rotate. With. Right. Child( k 3. left ); return rotate. With. Left. Child ( k 3 ); } Figure 4. 43 Routine สำหรบ double rotation 30
/* Double rotate binary tree node: * first right child with its left child; * then node k 1 with new right child. * For AVL trees, this is a double rotation for case 3. * Update heights, then return new root. */ private static Avl. Node double. With. Right. Child( Avl. Node k 1 ) { k 1. right = rotate. With. Left. Child ( k 1. right ); return rotate. With. Right. Child( k 1 ); } Figure 4. 43 (ตอ ) 31
Node delete. Node(Node root, int key) { // STEP 1: PERFORM STANDARD BST DELETE if (root == null) return root; // If the key to be deleted is smaller than // the root's key, then it lies in left subtree if (key < root. key) root. left = delete. Node(root. left, key); // If the key to be deleted is greater than the // root's key, then it lies in right subtree else if (key > root. key) root. right = delete. Node(root. right, key); // if key is same as root's key, then // this is the node to be deleted else { // node with only one child or no child if ((root. left==null)||(root. right==null)) { Node temp = null; if (temp == root. left) temp = root. right; else temp = root. left; // No child case if (temp == null) { temp = root; root = null; } else // One child case root = temp; // Copy the contents of // the non-empty child } else { // node with two children: Get the inorder // successor (smallest in the right subtree) Node temp = min. Value. Node(root. right); // Copy the inorder successor's data to this node root. key = temp. key; // Delete the inorder successor root. right = delete. Node(root. right, temp. key); } } // If the tree had only one node then return if (root == null) return root; 32
// Node delete. Node(Node root, int key) cont // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE root. height = max(height(root. left), height(root. right)) + 1; // STEP 3: GET THE BALANCE FACTOR OF THIS NODE // (to check whether this node became unbalanced) int balance = get. Balance(root); // If this node becomes unbalanced, // then there are 4 cases // Left Case if (balance > 1 && get. Balance(root. left) >= 0) return right. Rotate(root); // Left Right Case if (balance > 1 && get. Balance(root. left) < 0) { root. left = left. Rotate(root. left); return right. Rotate(root); } // Right Case if (balance < -1 && get. Balance(root. right) <= 0) return left. Rotate(root); // Right Left Case if (balance < -1 && get. Balance(root. right) > 0) { root. right = right. Rotate(root. right); return left. Rotate(root); } return root; } // Get Balance factor of node N int get. Balance(Node N) { if (N == null) return 0; return height(N. left) - height(N. right); } https: //www. geeksforgeeks. org/avl-tree-set-2 -deletion/ 33
k 5 k 4 k 3 k 1 D k 2 A E B F C k 5 k 4 k 1 A E k 3 k 2 B C F D 38
k 5 k 1 k 2 A F k 4 k 3 B C E D k 1 k 5 k 2 A k 4 B k 3 C F E D 39
g x p x A B p D A g B B D C g A C x p x g D C A p B C D 44 Figure 4. 44 Zig-zag : rotate x-p rotate x-g
g x p x A B D p A C B g g x p A B D C p x C g D A B D C 45 Figure 4. 45 Zig-zig : rotate p-g rotate x-p
7 7 6 6 5 5 2 1 6 1 5 2 2 7 4 4 1 1 6 4 4 3 7 3 3 3 Figure 4. 46 ผลการ 50
1 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 Figure 4. 47 ผลการ splay node 1 ของ tree 51
2 1 32 28 24 30 20 26 16 22 12 18 8 14 4 3 10 6 5 9 13 17 21 25 29 31 27 23 19 15 11 7 Figure 4. 48 ผลของการ splay node 2 52
3 2 28 1 32 24 30 20 12 4 16 8 14 6 5 10 7 9 13 22 18 15 17 29 21 31 26 23 25 27 19 11 Figure 4. 49 ผลของการ splay node 3 53
4 28 3 12 2 32 8 1 20 6 5 10 7 9 30 16 11 14 13 24 18 15 17 22 19 21 29 31 26 23 25 27 Figure 4. 50 ผลของการ splay node 4 ของ tree ทแลว 54
5 4 3 12 6 28 2 1 8 20 7 10 9 32 16 11 14 13 24 18 15 17 22 19 21 https: //www. cs. usfca. edu/~galles/visualization/Splay Tree. html 30 26 23 25 29 31 27 Figure 4. 51 ผลของการ splay โนด 5 ของ tree ทแลว 55
Delete( x) X Find(x) L Remove x Join Find max L R R L’ R 57
Delete( 30) 50 2 Find(30) 70 20 30 60 50 20 2 80 35 28 30 28 35 2 Join 28 50 28 35 20 70 60 80 60 Remove 30 Find max 20 70 80 2 50 35 70 60 https: //www. codesdope. com/course/datastructures-splay-trees/ 80 58
public void print. Tree( ) { if ( is. Empty( ) ) System. out. println( "Empty tree" ); else print. Tree( root ); } private void print. Tree( Binary. Node t ) { if ( t != null ) { print. Tree( t. left ); System. out. println( t. element ); print. Tree( t. right ); } } Figure 4. 56 Routine เพอพมพคาใน tree เรยงตามลำดบคา binary search 60
/** Compute height of a subtree * t = the node that roots the tree */ private int height( Binary. Node t ) { if ( t == null) return – 1; else return max(height( t. left ), height( t. right )) + 1; } Figure 4. 57 ฟงกชนเพอคำนวณ height ของ tree 63 โดยการใช postorder traversal
- Slides: 64