งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

AVL Tree.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "AVL Tree."— ใบสำเนางานนำเสนอ:

1 AVL Tree

2 เป็น binary search tree ที่มี
สำหรับแต่ละโนด ความสูงของ left กับ right subtree ต่างกันได้อย่างมากแค่ 1 ถ้า tree เป็น empty tree เราให้ height เป็น -1 ความสูงของ AVL tree จะเป็น log N ค่าจริงคือ 1.44log(N+2)-.328 เพราะว่าความสูงของ left กับ right subtree ต่างกันได้อย่างมากแค่ 1 จำนวนโนดที่น้อยที่สุดของต้นไม้ที่มีความสูง h, s(h) = s(h-1)+s(h-2)+1 เมื่อ h=0, s(h) =1 เมื่อ h=1, s(h) =2 คล้าย fibonacci

3 AVL Non-AVL 1

4 ฟังก์ชั่นต่างๆ O(log(N)) หมด ยกเว้นการ insert กับ remove ซึ่งอาจทำให้เสียความเป็น AVL ไป ใส่โนดใหม่เข้ามาทำให้เสียความเป็น AVL 1

5 แก้ความไม่เป็น AVL ด้วยการ rotate
จากการ insert โนดที่อยู่บนทางจากจุดที่ใส่ถึง root เท่านั้นที่มีความสูงเปลี่ยน ถ้าเราไล่ดูจากจุดที่ insert ขึ้นไป ก็จะพบโนดที่เสียความเป็น AVL ที่อยู่ลึกสุด เราจะแก้ที่โนดนี้ โนดนี้มีได้สี่แบบเท่านั้น แก้ด้วย single rotation แก้ด้วย double rotation

6 Single rotation หลังจากการ insert
b X Y Z ดึงขึ้น ย้ายที่เกาะ ไปเกาะโนดเจ้าปัญหา แก้ได้แล้วก็ไม่ต้อง rotate ที่ไหนต่ออีก

7 ตัวอย่าง 1 5 2 1 8 4 7 3 6 เสีย AVL จากโนดนี้

8 ตัวอย่าง 2 เริ่มจากไม่มีอะไร เรา insert 3,2,1 เสียความเป็น AVL 3
rotate To be continued

9 3 2 1 4 5 เสียความเป็น AVL To be continued ต่อไปเรา insert 4,5

10 ต่อไปเรา insert 6 3 2 1 4 5 6 เสียความเป็น AVL rotate To be continued

11 ต่อไปเรา insert 7 3 2 1 4 5 6 7 เสียความเป็น AVL rotate

12 Double rotation Single rotation ไม่ได้แล้ว a b Y X Z Y ไม่เปลี่ยนระดับ

13 a b Y X Z c มีการ insert ดังนั้นต้องมีของแน่ๆ Y1 Y2 โนดที่เจ๊ง

14 หมุนครั้งแรก c a b X Z Y1 Y2

15 หมุนครั้งที่สอง c a b X Z Y1 Y2

16 ให้ c ปล่อย Y1,Y2 แล้วกระโดดขึ้นหิ้ว a กับ b แทน
X Z Y1 Y2 ส่วน Y1, Y2 ก็หาที่เกาะข้างๆ ไม่รู้ว่าข้าง Y1 หรือ Y2 กันแน่ที่ทำให้เสีย แต่ก็ไม่เป็นไรเพราะในตอนจบก็ลดระดับกันหมด

17 ตัวอย่าง Insert 16,15 เสียความเป็น AVL ตอน insert 15 4 2 6 เจ๊ง rotate
3 2 1 4 5 6 7 16 15 เจ๊ง rotate

18 Insert 14 3 2 1 4 5 6 15 16 7 14 เจ๊ง ต้องเป็นตัวกระโดด rotate

19 Insert 13 เจ๊ง ขวาสองทีแบบนี้ใช้ single rotation 3 2 1 4 5 6 15 16 7
14 13 rotate Insert 13

20 Insert 12 ก็ทำให้ต้อง single rotate อีก
3 2 1 4 5 6 15 16 7 14 13 7 4 เจ๊ง 15 rotate 2 6 13 16 1 3 5 12 14 12

21 Insert 11 ก็ทำให้ต้อง single rotate อีก
7 เจ๊ง 7 4 4 13 15 rotate 15 2 12 6 6 13 16 2 1 3 1 5 5 14 16 12 14 3 11 11

22 Insert 10 ก็ทำให้ต้อง single rotate อีก
3 2 4 5 6 15 16 7 14 13 12 11 10 เจ๊ง rotate 7 4 13 15 6 2 11 1 3 5 10 12 14 16

23 Insert 8 ไม่มีอะไรเกิดขึ้น Insert 9 ต่อเลย ง่ายๆ
7 4 13 6 15 2 11 เจ๊ง 1 3 5 10 12 14 16 8 9 8 10 9

24 เดี๋ยวต้องมีเมธอดที่รีเทิร์น height
class AvlNode { // Constructors AvlNode( Comparable theElement ) this( theElement, null, null ); } AvlNode( Comparable theElement, AvlNode lt, AvlNode rt ) element = theElement; left = lt; right = rt; height = 0; // Friendly data; accessible by other package routines Comparable element; // The data in the node AvlNode left; // Left child AvlNode right; // Right child int height; // Height เดี๋ยวต้องมีเมธอดที่รีเทิร์น height เพื่อเราจะได้ไม่ต้องวุ่นวายถ้าโนดที่ต้องการดูเป็น null

25 ในหนังสือไม่มีการ remove
public class AvlTree { private AvlNode root; /** * Construct the tree. */ public AvlTree( ) { root = null; } * Insert into the tree; duplicates are ignored. x the item to insert. public void insert( Comparable x ) root = insert( x, root ); ในหนังสือไม่มีการ remove

26 /** เมธอดเหมือน binary search Tree เยอะ ข้ามละกัน
* Find the smallest item in the tree. smallest item or null if empty. */ public Comparable findMin( ) { return elementAt( findMin( root ) ); } * Find the largest item in the tree. the largest item of null if empty. public Comparable findMax( ) return elementAt( findMax( root ) ); เมธอดเหมือน binary search Tree เยอะ ข้ามละกัน

27 /** ฟังก์ชั่นช่วย * Return the height of node t, or -1, if null. */
private static int height( AvlNode t ) { return t == null ? -1 : t.height; } * Return maximum of lhs and rhs. private static int max( int lhs, int rhs ) return lhs > rhs ? lhs : rhs; ฟังก์ชั่นช่วย

28 Insert t ถ้า==2เป็นจริง x ต้องอยู่ต่ำกว่านี้
/** * Internal method to insert into a subtree. x the item to insert. t the node that roots the tree. the new root. */ private AvlNode insert( Comparable x, AvlNode t ) { if( t == null ) t = new AvlNode( x, null, null ); else if( x.compareTo( t.element ) < 0 ) t.left = insert( x, t.left ); if( height( t.left ) - height( t.right ) == 2 ) if( x.compareTo( t.left.element ) < 0 ) t = rotateWithLeftChild( t ); else t = doubleWithLeftChild( t ); } Insert t ถ้า==2เป็นจริง x ต้องอยู่ต่ำกว่านี้

29 t else if( x.compareTo( t.element ) > 0 ) {
t.right = insert( x, t.right ); if( height( t.right ) - height( t.left ) == 2 ) if( x.compareTo( t.right.element ) > 0 ) t = rotateWithRightChild( t ); else t = doubleWithRightChild( t ); } ; // Duplicate; do nothing t.height = max( height( t.left ), height( t.right ) ) + 1; return t;

30 /** k2 k1 k1 k2 * 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 AvlNode rotateWithLeftChild( AvlNode k2 ) { AvlNode k1 = k2.left; k2.left = k1.right; k1.right = k2; k2.height = max( height( k2.left ), height( k2.right ) ) + 1; k1.height = max( height( k1.left ), k2.height ) + 1; return k1; } k2 k1 k2 k1

31 k1 k2 k1 k2 /** * 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 AvlNode rotateWithRightChild( AvlNode k1 ) { AvlNode k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = max( height( k1.left ), height( k1.right ) ) + 1; k2.height = max( height( k2.right ), k1.height ) + 1; return k2; } k1 k2 k1 k2

32 k2 k3 k1 X Z Y1 Y2 k2 k1 X Y1 Y2 k3 Z k2 k1 X Y1 Y2 k3 Z /**
* Double rotate binary tree node: first left child * with its right child; then node k3 with new left child. * For AVL trees, this is a double rotation for case 2. * Update heights, then return new root. */ private static AvlNode doubleWithLeftChild( AvlNode k3 ) { k3.left = rotateWithRightChild( k3.left ); return rotateWithLeftChild( k3 ); } k2 k3 k1 X Z Y1 Y2 k2 k1 X Y1 Y2 k3 Z k2 k1 X Y1 Y2 k3 Z

33 k2 k1 k3 X Z Y1 Y2 k2 k3 X Y1 Y2 k1 Z k2 k3 X Y1 Y2 k1 Z /**
* Double rotate binary tree node: first right child * with its left child; then node k1 with new right child. * For AVL trees, this is a double rotation for case 3. * Update heights, then return new root. */ private static AvlNode doubleWithRightChild( AvlNode k1 ) { k1.right = rotateWithLeftChild( k1.right ); return rotateWithRightChild( k1 ); } k2 k1 k3 X Z Y1 Y2 k2 k3 X Y1 Y2 k1 Z k2 k3 X Y1 Y2 k1 Z


ดาวน์โหลด ppt AVL Tree.

งานนำเสนอที่คล้ายกัน


Ads by Google