1 / 22

§3 The Search Tree ADT -- Binary Search Trees

§3 The Search Tree ADT -- Binary Search Trees. 30. 20. 60. 5. 40. 15. 25. 70. 2. 22. 12. 10. 65. 80. 1. Definition. 【Definition】 A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the following properties:

Télécharger la présentation

§3 The Search Tree ADT -- Binary Search 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. §3 The Search Tree ADT -- Binary Search Trees 30 20 60 5 40 15 25 70 2 22 12 10 65 80 1. Definition 【Definition】A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the following properties: (1) Every node has a key which is an integer, and the keys are distinct. (2) The keys in a nonempty left subtree must be smaller than the key in the root of the subtree. (3) The keys in a nonempty right subtree must be larger than the key in the root of the subtree. (4) The left and right subtrees are also binary search trees. 1/22

  2. §3 Binary Search Trees 2. ADT Objects: A finite ordered list with zero or more elements. Operations:  SearchTree MakeEmpty( SearchTree T );  Position Find( ElementType X, SearchTree T );  Position FindMin( SearchTree T );  Position FindMax( SearchTree T );  SearchTree Insert( ElementType X, SearchTree T );  SearchTree Delete( ElementType X, SearchTree T );  ElementType Retrieve( Position P ); 2/22

  3. §3 Binary Search Trees These are tail recursions. Must this test be performed first? 3. Implementations  Find Position Find( ElementType X, SearchTree T ) { if ( T == NULL ) return NULL; /* not found in an empty tree */ if ( X < T->Element ) /* if smaller than root */ return Find( X, T->Left ); /* search left subtree */ else if ( X > T->Element ) /* if larger than root */ return Find( X, T->Right ); /* search right subtree */ else /* if X == root */ return T; /* found */ } T( N ) = S ( N ) = O( d ) where d is the depth of X 3/22

  4. §3 Binary Search Trees Position Iter_Find( ElementType X, SearchTree T ) { /* iterative version of Find */ while ( T ) { if ( X == T->Element ) return T ; /* found */ if ( X < T->Element ) T = T->Left ; /*move down along left path */ else T = T-> Right ; /* move down along right path */ } /* end while-loop */ return NULL ; /* not found */ } 4/22

  5. §3 Binary Search Trees  FindMin Position FindMin( SearchTree T ) { if ( T == NULL ) return NULL; /* not found in an empty tree */ else if ( T->Left == NULL ) return T; /* found left most */ else return FindMin( T->Left ); /* keep moving to left */ } T( N ) = O ( d )  FindMax Position FindMax( SearchTree T ) { if ( T != NULL ) while ( T->Right != NULL ) T = T->Right; /* keep moving to find right most */ return T; /* return NULL or the right most */ } T( N ) = O ( d ) 5/22

  6. §3 Binary Search Trees 25 35 80 30 5 40 2  Insert Sketch of the idea: Insert 80  check if 80 is already in the tree  80 > 40, so it must be the right child of 40 This is the last node we encounter when search for the key number. It will be the parent of the new node. Insert 35  check if 35 is already in the tree  35 < 40, so it must be the left child of 40 Insert 25  check if 25 is already in the tree  25 > 5, so it must be the right child of 5 6/22

  7. §3 Binary Search Trees SearchTree Insert( ElementType X, SearchTree T ) { if ( T == NULL ) { /* Create and return a one-node tree */ T = malloc( sizeof( struct TreeNode ) ); if ( T == NULL ) FatalError( "Out of space!!!" ); else { T->Element = X; T->Left = T->Right = NULL; } } /* End creating a one-node tree */ else/* If there is a tree */ if ( X < T->Element ) T->Left = Insert( X, T->Left ); else if ( X > T->Element ) T->Right = Insert( X, T->Right ); /* Else X is in the tree already; we'll do nothing */ return T; /* Do not forget this line!! */ } How would you Handle duplicated Keys? T( N ) = O ( d ) 7/22

  8. §3 Binary Search Trees Note: These kinds of nodes have degree at most 1. 40 60 20 50 70 10 45 55 30 52  Delete  Delete a leaf node : Reset its parent link to NULL.  Delete a degree 1 node : Replace the node by its single child.  Delete a degree 2 node :  Replace the node by the largest one in its left subtree or the smallest one in its right subtree.  Delete the replacing node from the subtree. 〖Example〗 Delete 60 55 Solution 1: reset left subtree. Solution 2: reset right subtree. 52 8/22

  9. §3 Binary Search Trees SearchTree Delete( ElementType X, SearchTree T ) { Position TmpCell; if ( T == NULL ) Error( "Element not found" ); else if ( X < T->Element ) /* Go left */ T->Left = Delete( X, T->Left ); else if ( X > T->Element ) /* Go right */ T->Right = Delete( X, T->Right ); else/* Found element to be deleted */ if ( T->Left && T->Right ) { /* Two children */ /* Replace with smallest in right subtree */ TmpCell = FindMin( T->Right ); T->Element = TmpCell->Element; T->Right = Delete( T->Element, T->Right ); } /* End if */ else { /* One or zero child */ TmpCell = T; if ( T->Left == NULL ) /* Also handles 0 child */ T = T->Right; else if ( T->Right == NULL ) T = T->Left; free( TmpCell ); } /* End else 1 or 0 child */ return T; } T( N ) = O ( h ) where h is the height of the tree 9/22

  10. §3 Binary Search Trees Note: If there are not many deletions, then lazy deletion may be employed: add a flag field to each node, to mark if a node is active or is deleted. Therefore we can delete a node without actually freeing the space of that node. If a deleted key is reinserted, we won’t have to call malloc again. While the number of deleted nodes is the same as the number of active nodes in the tree, will it seriously affect the efficiency of the operations? 10/22

  11. §3 Binary Search Trees 1 4 2 6 2 3 5 7 6 1 3 4 7 5 4. Average-Case Analysis Question: Place n elements in a binary search tree. How high can this tree be? Answer: The height depends on the order of insertion. 〖Example〗 Given elements 1, 2, 3, 4, 5, 6, 7. Insert them into a binary search tree in the orders: 4, 2, 1, 3, 6, 5, 7 and 1, 2, 3, 4, 5, 6, 7 h = 6 h = 2 11/22

  12. §3 Binary Search Trees 【Definition】The internal path length for a tree of N nodes is defined to be: where d( i ) is the depth of node i. Since all subtree sizes are equally likely, the average value of both D( i ) and D( N  i  1 ) is root +1 inodes N  i  1nodes p.212-213 = = O(N log N) Home work: p.139 4.42 Isomorphic Trees p.140 4.45 Threaded Trees 【Theorem】 Assume that all binary search trees are equally likely. Then the average D(N) (taken over all possible insertion sequences) is O(N log N). Thus the expected depth of any node is O(log N). Proof:D( N ) = D( i ) + D( N  i  1 ) + N 1 12/22

  13. Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root Problem : Although Tp = O( height ), but the height can be as bad as O( N ). small large §4 AVL Trees 13/22

  14. §4 AVL Trees Mar Oct Aug June Dec Nov Jan Feb Feb Mar Aug Jan July June May Apr Dec July Sept Apr May Oct Nov Sept 〖Example〗 3 binary search trees obtained for the months of the year Average search time = ? 3.5 What if the months are entered in alphabetical order? Entered from Jan to Dec AST = 6.5 Average search time = ? 3.1 A balanced tree 14/22

  15. §4 AVL Trees 5 7 4 2 8 2 8 3 5 1 4 7 1 4 2 6 3 3 5 1 7 Adelson-Velskii-Landis (AVL) Trees (1962) 【Definition】An empty binary tree is height balanced. IfTis a nonempty binary tree withTLandTRas its left and right subtrees, then T is height balanced iff (1)TLandTRare height balanced, and (2) | hLhR |  1 wherehLandhRare the heights ofTLandTR , respectively. The height of an empty tree is defined to be –1. 【Definition】The balance factorBF( node ) = hLhR. In an AVL tree,BF( node ) = 1, 0, or 1. 15/22

  16. §4 AVL Trees 0 0 0 0 0 0 Mar Mar May Nov Nov May 1 2 1 0 Single rotation 0 0 1 2 0 1 0 A B B B A A RR RR BR AL AL Rotation Insertion AL BL BL BR BL BR 〖Example〗 Input the months Mar May Nov 1 2 1  The trouble maker Nov is in the right subtree’s right subtree of the trouble finder Mar. Hence it is called an RR rotation. A is not necessarily the root of the tree In general: 0 16/22

  17. §4 AVL Trees 0 0 0 0 0 0 0 0 0 0 Mar Mar Nov Nov Aug Apr May Aug May Apr 2 1 2 1 1 1 1 1 1 1 0 0 0 1 1 1 2 0 0 A B A A B B LL LL LL BL AR AR Rotation Rotation Insertion AR BR BR BL BR BL Aug Apr 2 2 1 In general: 0 17/22

  18. §4 AVL Trees 0 0 0 0 0 0 0 0 0 0 0 0 May Aug Apr Nov Jan Mar Mar Aug Nov Apr May Jan 1 2 2 1 1 0 1 1 1 1 0 1 BL CL CR AR 0 1 1 0 AR AR 1 1 or 0 0 0 or 1 1 0 2 0 1 A B B A B C A C C LR LR LR OR BL BL Rotation Rotation Insertion CL CR CL CR OR Jan 2 Double Rotation 1 1 In general: 18/22

  19. §4 AVL Trees 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 July Dec Nov Apr Feb Aug July Mar Dec Feb May Jan Apr Mar May Nov Jan Aug 1 2 1 2 2 1 1 1 1 1 1 1 1 1 1 BR CR CL AL 0 1 AL AL 1 or 0 0 or 1 1 0 1 2 0 0 1 1 1 0 A B C A B C C A B RL RL RL OR BR BR Rotation Rotation Insertion CR CL CR CL OR Dec July Feb 2 2 1 1 In general: 19/22

  20. §4 AVL Trees 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 July Dec Aug Jan Feb Dec Apr Feb May June June Nov May Mar Nov Sept Oct Mar Aug Dec July Apr Oct Aug Mar July Nov May June Feb Apr 1 2 2 1 1 1 1 1 1 0 0 0 1 1 2 1 Jan Jan 1 1 2 1 1 1 0 June Oct Sept 1 2 1 1 1 1 1 2 1 1 1 1 Note: Several bf’s might be changed even if we don’t need to reconstruct the tree. Another option is to keep a height field for each node. 20/22

  21. §4 AVL Trees AvlTree Insert( ElementType X, AvlTree T ) { if ( T == NULL ) { /* Create and return a one-node tree */ T = malloc( sizeof( struct AvlNode ) ); if ( T == NULL ) FatalError( "Out of space!!!" ); else { T->Element = X; T->Height = 0; T->Left = T->Right = NULL; } } /* End creating a one-node tree */ else if ( X < T->Element ) { /* handle left insertion */ T->Left = Insert( X, T->Left ); if ( Height( T->Left ) - Height( T->Right ) == 2 ) /* not balanced */ if ( X < T->Left->Element ) /* LL Rotation */ T = SingleRotateWithLeft( T ); else/* LR Rotation */ T = DoubleRotateWithLeft( T ); } /* End left */ else if( X > T->Element ) { /* handle right insertion */ T->Right = Insert( X, T->Right ); if ( Height( T->Right ) - Height( T->Left ) == 2 ) /* not balanced */ if ( X > T->Right->Element ) /* RR Rotation */ T = SingleRotateWithRight( T ); else/* RL Rotation */ T = DoubleRotateWithRight( T ); } /* End right */ /* Else X is in the tree already; we'll do nothing */ T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1; return T; } Home work: p.136 4.16 Create an AVL Tree p.136 4.22 Double Rotation 21/22

  22. §4 AVL Trees OR A A h2 h2 h1 h1 Fibonacci number theory gives that Let nh be the minimum number of nodes in a height balanced tree of height h. Then the tree must look like One last question: Obviously we have Tp = O( h ) where h is the height of the tree. But h = ?  nh = nh1 + nh2 + 1 Fibonacci numbers: F0 = 0, F1 = 1, Fi = Fi1 + Fi2 for i > 1  nh = Fh+2  1, for h  0 22/22

More Related