1 / 42

Tree

Tree. 1. 2. 5. 6. 7. 3. 4. ลักษณะทั่วไป. ตัว โนด บนสุดจะเรียกว่าเป็น ราก (root) ของ tree tree ส่วนย่อย (มี โนด 2 เป็น root) เรียกได้ว่าเป็น subtree ของ treeใหญ่

vivek
Télécharger la présentation

Tree

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

  2. 1. 2. 5. 6. 7. 3. 4. ลักษณะทั่วไป

  3. ตัว โนด บนสุดจะเรียกว่าเป็น ราก (root) ของ tree • tree ส่วนย่อย (มี โนด 2 เป็น root) เรียกได้ว่าเป็น subtree ของ treeใหญ่ • โนด 1 จะเป็น บัพแม่(parent) ของ โนด 2,5,6 ส่วน โนด 2 เป็น parent ของ โนด 3,4 หรือเรียกอีกอย่างว่า โนด 3,4 เป็น บัพลูก(child) ของ โนด 2 • โนด 3 เป็นบัพพี่น้อง(sibling) กับ โนด 4 • ถ้ามีความสัมพันธ์แบบ parent ตั้งแต่หนึ่งทอดขึ้นไป เราเรียกว่า บัพบน(ancestor) เช่น โนด 1 เป็น ancestor ของ โนด 7

  4. โนด ที่ไม่มีกิ่งออกไป เราเรียกว่า บัพใบ หรือ ใบ(leaf) เช่นในรูป leaf คือ โนด 3, 4, 5 และ 7 • เราจะสังเกตได้ว่า มีแค่ 1 เส้นทาง(path) จาก root ไปยัง โนด แต่ละ โนด เท่านั้น • ความลึก(depth) ของ โนด n คือความยาวของ path จาก root ถึง n ตัวอย่างเช่น depth ของ โนด เบอร์ 7 คือ 2 • ความสูง(height) ของ โนด n คือความยาวของ path ที่ยาวที่สุดจาก n ถึง leaf ตัวอย่างเช่น height ของ โนด เบอร์ 7 คือ 0 และ height ของ โนด เบอร์ 1 คือ 2

  5. height ของ tree ก็คือ height ของ root นั่นเอง ถ้า tree มีแค่ root เราให้ height เป็น 0 และถ้า tree เป็น empty tree เราให้ height เป็น -1 • height ของต้นไม้ใดๆ จะเท่ากับ height ของ subtree ที่สูงที่สุด บวกด้วย 1 • ระดับ(level) ของ โนด คือ ลำดับชั้นของโนดนั้น โดย child ของ โนด หนึ่งๆจะมีระดับ1 ระดับมากกว่า parent ของมัน (root ถือว่ามีระดับเป็น 0)

  6. Link ไปยัง sibling ถัดไป Link ไปยังลูกกิ่งแรก (first child) 1 6 5 2 7 4 3 ข้อมูล ถ้าให้แต่ละโนด มีกี่กิ่งก็ได้ • เราไม่มีทางกำหนดจำนวนกิ่งที่แน่นอนได้ • การเตรียมกิ่งไว้ล่วงหน้ามากๆก็เป็นการเปลืองหน่วยความจำ • วิธีหนึ่งที่อาจแก้ปัญหานี้ได้คือ ลักษณะของต้นไม้จากรูปแรกที่สร้างด้วยแนวคิดนี้

  7. การเรียงดูสมาชิกภายในต้นไม้ • ก่อนลำดับ (preorder) • ดูของจาก root โนด ที่เราเริ่มพิจารณา แล้วจึงไปดูของใน subtree โดยใน subtree ก็เริ่มดูจาก root โนด เช่นเดียวกัน

  8. 1. 2. 5. 6. 7. 3. 4. ผลการเรียงสมาชิกใน โนด จากรูป คือ 1,2,3,4,5,6,7 อันดับแรกที่ดู อันดับ4 ที่ดู เป็น subtree อันดับสองที่ดู เป็น subtree ซึ่งภายในก็จะเรียงดูแบบ preorder ซึ่งภายในก็จะเรียงดูแบบ preorder อันดับ3 ที่ดู

  9. หลังลำดับ (postorder) • คือการเรียงดู subtree ทั้งหมด แล้วจึงมาดูของใน root ซึ่งภายใน subtree นั้นก็ต้องมีการเรียงดูแบบเดียวกัน • เพราะฉะนั้นถ้าเราเรียงสมาชิกใน tree ในรูปที่แล้วแบบ postorder จะได้ผลลัพธ์เป็น 3,4,2,5,7,6,1 • ตามลำดับ (inorder) • คือการเรียงสมาชิกโดยสมาชิกของ subtree ด้านซ้ายของ root จะถือว่าต้องมาก่อน root ส่วนสมาชิกของ subtree ที่อยู่ด้านขวาของ root จะถือว่ามาทีหลัง root นอกจากนี้ส่วนข้างในตัว subtree ก็ใช้วิธีการเรียงอย่างเดียวกัน • ดังนั้นถ้าดูจากรูป (ไม่นับ subtree ที่มี 5 เพราะไม่รู้จะจัดอยู่ข้างไหนของ root) เราจะเรียงสมาชิกได้เป็น 3,2,4,1,6,7

  10. การค้นหาในแนวกว้าง (breadth-first search) • วิธีการสามแบบแรกที่กล่าวมาข้างต้นนั้น เป็นวิธีการค้นหาในแนวลึก (depth-first search) • ซึ่งถ้ามีการดู subtree ใดแล้วจะต้องตรวจสอบทั้ง subtree นั้นให้เสร็จ จึงจะสำรวจ root หรือ subtree อื่นๆที่เป็น sibling ต่อไปได้ • breadth-first จะตรวจสอบทีละระดับของ tree โดยเริ่มจากระดับของ root ดังนั้น tree ในรูปหน้าก่อนโน้นจะมีการเรียงสมาชิกได้เป็น 1,2,5,6,3,4,7

  11. * + - a * d e b c breadth-first implementation • ตอนที่ดูโนดโนดหนึ่ง เราจะบันทึก child ของโนดนั้นลงในคิวที่สร้างไว้สำหรับเก็บโนดในระดับถัดลงมา (next level queue) เช่น ถ้าเรามี

  12. breadth-first implementation(2) • เมื่อเราดู root เราจะบันทึก child โนด ของ root ไว้บน next level queue ดังนั้น ในขั้นนึ้ next level queue มี + กับ – • update ให้ this level queue = next level queue แล้ว เคลียร์ next level queue • สำรวจ this level queue จะพบ + เป็นตัวแรก ตอนนี้ให้ดูตัวต้นไม้แล้วใส่ลูกของ + ลงใน next level queue นั่นคือ ในตอนนี้ next level queue = a,* • สำรวจ this level queue ต่อ จะพบ – เป็นสมาชิกตัวต่อมา ใส่ลูกของ - ลงใน next level queue นั่นคือ ในตอนนี้ next level queue = a,*,d,e • ไม่มีสมาชิกใน this level queue แล้ว ดังนั้น update ให้ this level queue = next level queue นั่นคือ this level queue = a,*,d,e • เริ่มเยี่ยม this level queue ตั้งแต่ต้นอีกครั้ง และทำซ้ำไปเรื่อยๆ

  13. ต้นไม้แบบทวิภาค (Binary Tree) • แต่ละ โนด มีกิ่งออกไปได้มากที่สุดสองกิ่ง • ดังนั้นอาจมีกิ่งเดียวในทุกระดับก็ได้ ซึ่งก็จะกลายเป็น linked list ไป หรืออาจเรียกอีกอย่างได้ว่า skewed tree

  14. full binary tree หรือ perfectly balanced tree • เป็นรูปสามเหลี่ยมอย่างสมบูรณ์ ไม่ขาด leaf ใดๆในระดับของ leaf ที่สูงที่สุด

  15. A N E T O X ต้นไม้บริบูรณ์ (complete tree) • เต็มถึงระดับก่อน leaf ที่สูงที่สุดแต่ระดับของ leaf ที่สูงที่สุดอาจไม่เต็ม (แต่ยังมี leaf เติมใส่ต้นจากซ้ายไปขวา) ต้นไม้แบบเต็มต้นถือว่าเป็นต้นไม้บริบูรณ์แน่นอน แต่ต้นไม้บริบูรณ์ไม่จำเป็นต้องเต็มต้น

  16. ต้นไม้บริบูรณ์ (2) • ข้อสังเกตอีกอย่างคือ ถ้าเราเอาสมาชิกของต้นไม้บริบูรณ์ใส่ในอาร์เรย์ โดยเรียงใส่แบบตามแนวกว้าง (breadth-first) • สมาชิกที่ตำแหน่ง i จะมีลูกซ้ายอยู่ที่ตำแหน่ง 2i+1 • และลูกขวาอยู่ที่ตำแหน่ง 2i+2 • ส่วนตัว parent ของ i จะอยู่ที่ตำแหน่ง (i-1)/2 (ปัดทศนิยมทิ้งไป) ลูกซ้ายของ E จะต้องมี index =2*2+1 =5 ซึ่งก็คือ X นั่นเอง ส่วน parent ของ E ก็จะมี index = (2-1)/2 = 0 เมื่อปัดลง

  17. two-tree • จะต้องเป็น empty tree หรือไม่ก็มี non-leaf node ที่ต้องมีสองกิ่งเท่านั้น

  18. ทฤษฎีต่างๆของต้นไม้ทวิภาคทฤษฎีต่างๆของต้นไม้ทวิภาค ถ้าเราให้ • leaves(t) แทนฟังก์ชั่นที่บอกจำนวนใบของต้นไม้ t • n(t) เป็นจำนวนโนดของ ต้นไม้ t • height(t) เป็นความสูงของต้นไม้ t • leftsubtree(t) เป็นต้นไม้ย่อยด้านซ้ายของ t • rightsubtree(t) เป็นต้นไม้ย่อยด้านขวาของ t • max(a,b) เป็นค่าที่มากที่สุดเมื่อเทียบ a กับ b

  19. สำหรับต้นไม้ทวิภาคที่ไม่ใช่ต้นไม้ว่าง จะมีความสัมพันธ์ระหว่างฟังก์ชั่นต่างๆดังนี้ ถ้า t เป็น two-tree แล้วละก็ แล้วละก็ t เป็น two-tree แน่นอน ถ้า ถ้า t เป็นต้นไม้แบบเต็มต้น แล้วละก็

  20. ถ้า แล้วละก็ t เป็นต้นไม้แบบเต็มต้น อย่าลืมว่าเราใช้ 2.0 เพราะฉะนั้นค่าจากสูตรเหล่านี้ผมจะถือว่าเป็นทศนิยมนะ เพราะจำนวนเต็มจะใช้กับกฎข้อ 4 ไม่ได้

  21. พิสูจน์ base case คือเมื่อ t เป็น ต้นไม้ที่มีแต่ราก ซึ่งเห็นชัดว่าทำให้สมการเป็นจริง inductive case คือ เมื่อ t เป็นต้นไม้ที่สูง h ให้ สิ่งที่ต้องพิสูจน์ คือ เมื่อ t เป็นต้นไม้ที่สูง h+1 แล้ว

  22. เรารู้ว่า เพราะฉะนั้นเราสามารถแทนค่า n(t) ลงไปในอสมการได้ ผลคือได้

  23. External Path length • ให้ t เป็นต้นไม้ทวิภาคที่มีราก จะได้ว่า External Path length, E(t) คือผลบวกของความลึกของทุกใบ • ถ้า t เป็นต้นไม้ทวิภาคที่มีจำนวนใบเป็น k (ซึ่งมากกว่า 0) จะได้ว่า

  24. ต้นไม้ค้นหาแบบทวิภาค (Binary Search Tree) เป็น empty tree หรือเป็นต้นไม้ซึ่ง • ทุกสมาชิกใน leftsubtree(t) มีค่าน้อยกว่าสมาชิกที่รากของ t • ทุกสมาชิกใน rightsubtree(t) มีค่ามากกว่าสมาชิกที่รากของ t • ทั้ง leftsubtree(t) และ rightsubtree(t) ต่างก็เป็น binary search tree

  25. 7 5 11 2 6 14 4 สืบค้นได้ง่าย เช่นถ้าเราต้องการหาว่า 4 อยู่ในต้นไม้หรือไม่ เมื่อเราดูโนดแต่ละโนด เราสามารถรู้ได้ทันทีว่าจะไปสำรวจต้นไม้ข้างใดต่อไป ทำให้เวลาในการค้นหาขึ้นอยู่กับความสูงของต้นไม้เท่านั้น ซึ่งก็แปรตาม log ของจำนวน โนด อีกทีหนึ่ง

  26. class BinaryNode • { • // Constructors • BinaryNode( Comparable theElement ) • { • this( theElement, null, null ); • } • BinaryNode( Comparable theElement, BinaryNode lt, BinaryNode rt ) • { • element = theElement; • left = lt; • right = rt; • } • // Friendly data; accessible by other package routines • Comparable element; // The data in the node • BinaryNode left; // Left child • BinaryNode right; // Right child • }

  27. // BinarySearchTree class // // CONSTRUCTION: with no initializer // // ******************PUBLIC OPERATIONS********************* // void insert( x ) --> Insert x // void remove( x ) --> Remove x // Comparable find( x ) --> Return item that matches x // Comparable findMin( ) --> Return smallest item // Comparable findMax( ) --> Return largest item // boolean isEmpty( ) --> Return true if empty; else false // void makeEmpty( ) --> Remove all items // void printTree( ) --> Print tree in sorted order private BinaryNode root;

  28. public class BinarySearchTree • { • private BinaryNode root; • /** • * Construct the tree. • */ • public BinarySearchTree( ) • { • root = null; • } • /** • * Insert into the tree; duplicates are ignored. • * @param x the item to insert. • */ • public void insert( Comparable x ) • { • root = insert( x, root ); • }

  29. /** • * Remove from the tree. Nothing is done if x is not found. • * @param x the item to remove. • */ • public void remove( Comparable x ) • { • root = remove( x, root ); • } • /** • * Find the smallest item in the tree. • * @return smallest item or null if empty. • */ • public Comparable findMin( ) • { • return elementAt( findMin( root ) ); • }

  30. /** • * Find the largest item in the tree. • * @return the largest item of null if empty. • */ • public Comparable findMax( ) • { • return elementAt( findMax( root ) ); • } • /** • * Find an item in the tree. • * @param x the item to search for. • * @return the matching item or null if not found. • */ • public Comparable find( Comparable x ) • { • return elementAt( find( x, root ) ); • }

  31. /** • * Make the tree logically empty. • */ • public void makeEmpty( ) • { • root = null; • } • /** • * Test if the tree is logically empty. • * @return true if empty, false otherwise. • */ • public boolean isEmpty( ) • { • return root == null; • }

  32. /** • * Print the tree contents in sorted order. • */ • public void printTree( ) • { • if( isEmpty( ) ) • System.out.println( "Empty tree" ); • else • printTree( root ); • } • /** • * Internal method to get element field. • * @param t the node. • * @return the element field or null if t is null. • */ • private Comparable elementAt( BinaryNode t ) • { • return t == null ? null : t.element; • }

  33. /** • * Internal method to insert into a subtree. • * @param x the item to insert. • * @param t the node that roots the tree. • * @return the new root. • */ • private BinaryNode insert( Comparable x, BinaryNode t ) • { • /* 1*/ if( t == null ) • /* 2*/ t = new BinaryNode( x, null, null ); • /* 3*/ else if( x.compareTo( t.element ) < 0 ) • /* 4*/ t.left = insert( x, t.left ); • /* 5*/ else if( x.compareTo( t.element ) > 0 ) • /* 6*/ t.right = insert( x, t.right ); • /* 7*/ else • /* 8*/ ; // Duplicate; do nothing • /* 9*/ return t; • }

  34. /** • * Internal method to remove from a subtree. • * @param x the item to remove. • * @param t the node that roots the tree. • * @return the new root. • */ • private BinaryNode remove( Comparable x, BinaryNode t ) • { • if( t == null ) • return t; // Item not found; do nothing • if( x.compareTo( t.element ) < 0 ) • t.left = remove( x, t.left ); • else if( x.compareTo( t.element ) > 0 ) • t.right = remove( x, t.right ); • else if( t.left != null && t.right != null ) // Two children • { • t.element = findMin( t.right ).element; • t.right = remove( t.element, t.right ); • } • else • t = ( t.left != null ) ? t.left : t.right; • return t; • }

  35. X y y y การเอาสมาชิกค่าน้อยที่สุดของกิ่งขวาขึ้นมาแทนสมาชิกตัวที่ถูกลบ

  36. x y y การเอา subtree ขึ้นมาแทนที่โนดที่ถูกลบ

  37. /** • * Internal method to find the smallest item in a subtree. • * @param t the node that roots the tree. • * @return node containing the smallest item. • */ • private BinaryNode findMin( BinaryNode t ) • { • if( t == null ) • return null; • else if( t.left == null ) • return t; • return findMin( t.left ); • }

  38. /** • * Internal method to find the largest item in a subtree. • * @param t the node that roots the tree. • * @return node containing the largest item. • */ • private BinaryNode findMax( BinaryNode t ) • { • if( t != null ) • while( t.right != null ) • t = t.right; • return t; • }

  39. /** • * Internal method to find an item in a subtree. • * @param x is item to search for. • * @param t the node that roots the tree. • * @return node containing the matched item. • */ • private BinaryNode find( Comparable x, BinaryNode t ) • { • if( t == null ) • return null; • if( x.compareTo( t.element ) < 0 ) • return find( x, t.left ); • else if( x.compareTo( t.element ) > 0 ) • return find( x, t.right ); • else • return t; // Match • }

  40. /** • * Internal method to print a subtree in sorted order. • * @param t the node that roots the tree. • */ • private void printTree( BinaryNode t ) • { • if( t != null ) • { • printTree( t.left ); • System.out.println( t.element ); • printTree( t.right ); • } • }

  41. // Test program • public static void main( String [ ] args ) • { • BinarySearchTree t = new BinarySearchTree( ); • final int NUMS = 4000; • final int GAP = 37; • System.out.println( "Checking... (no more output means success)" ); • for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS ) • t.insert( new MyInteger( i ) ); • for( int i = 1; i < NUMS; i+= 2 ) • t.remove( new MyInteger( i ) );

  42. if( NUMS < 40 ) • t.printTree( ); • if( ((MyInteger)(t.findMin( ))).intValue( ) != 2 || • ((MyInteger)(t.findMax( ))).intValue( ) != NUMS - 2 ) • System.out.println( "FindMin or FindMax error!" ); • for( int i = 2; i < NUMS; i+=2 ) • if( ((MyInteger)(t.find( new MyInteger( i ) ))).intValue( ) != i ) • System.out.println( "Find error1!" ); • for( int i = 1; i < NUMS; i+=2 ) • { • if( t.find( new MyInteger( i ) ) != null ) • System.out.println( "Find error2!" ); • } • } • }

More Related