ce 221
play

CE 221 Data Structures and Algorithms Chapter 4: Trees (BST) Text: - PowerPoint PPT Presentation

CE 221 Data Structures and Algorithms Chapter 4: Trees (BST) Text: Read Weiss, 4.3 Izmir University of Economics 1 The Search Tree ADT Binary Search Trees An important application of binary trees is in searching. Let us assume that


  1. CE 221 Data Structures and Algorithms Chapter 4: Trees (BST) Text: Read Weiss, § 4.3 Izmir University of Economics 1

  2. The Search Tree ADT – Binary Search Trees • An important application of binary trees is in searching. Let us assume that each node in the tree stores an item. Assume for simplicity that these are distinct integers (deal with duplicates later). • The property that makes a binary tree into a binary search tree is that for every node, X , in the tree, the values of all the items in the left subtree are smaller than the item in X , and the values of items in the right subtree are larger than the item in X . The tree on the left is a binary search tree, but the tree on the right is not. The tree on the right has a node with key 7 in the left subtree of a node with key 6 (which happens to be the root). Izmir University of Economics 2

  3. Binary Search Trees - Operations  Descriptions and implementations of the operations that are usually performed on binary search trees ( BST ) are given.  Note that because of the recursive definition of trees, it is common to write these routines recursively. Because the average depth of a binary search tree is O (log N ), we generally do not need to worry about running out of stack space.  Since all the elements can be ordered, we will assume that the operators <, >, and = can be applied to them. Izmir University of Economics 3

  4. BST – Implementation - I Izmir University of Economics 4

  5. BST – Implementation - II Previous slide Izmir University of Economics 5

  6. BST – Implementation - III ... 46 } Izmir University of Economics 6

  7. BST – Implementation - IV • contains returns true if element x is in the BST referenced by t , or false if there is no such node. The structure of the tree makes this simple. If t is NULL , then we can just return . Otherwise, we make a recursive call on either the left or the right subtree of the node referenced by t . Izmir University of Economics 7

  8. BST – Implementation - V • To perform a findMin , start at the root and go left as long as there is a left child. The stopping point is the smallest element. • The findMax routine is the same, except that branching is to the right child. • Notice that the degenerate case of an empty tree is carefully handled. • Also notice that it is safe to change t in findMax , since we are only working with a copy. Always be extremely careful, however, because a statement such as t.right=t.right.right will make changes. Izmir University of Economics 8

  9. BST – Implementation – Insertion I The insertion routine is conceptually simple. To insert x into tree t , proceed down the tree as you would with a contains . If x is found, do nothing (or "update" something). Otherwise, insert x at the last spot on the path traversed. Duplicates can be handled by keeping an extra field in the node indicating the frequency of occurrence. If the key is only part of a larger record, then all of the records with the same key might be kept in an auxiliary data structure, such as a list or another search tree. → Insert node 5 Izmir University of Economics 9

  10. BST – Implementation – Insertion II Izmir University of Economics 10

  11. BST – Implementation – Deletion I • Once we have found the node to be deleted, we need to consider 3 possibilities. (1) If the node is a leaf, it can be deleted immediately. (2) If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node. Notice that the deleted node is now unreferenced and can be disposed of only if a pointer to it has been saved. → Delete node 4 Izmir University of Economics 11

  12. BST – Implementation – Deletion II (3) The complicated case deals with a node with two children. The general strategy is to replace the key of this node with the smallest key of the right subtree (easy) and recursively delete that node (which is now empty). Because the smallest node in the right subtree cannot have a left child, the second delete is an easy one. → Delete node 2 Izmir University of Economics 12

  13. BST – Implementation – Deletion III Inefficient, since calls highlighted in yellow result in two passes down the tree to find and delete the smallest node in the right subtree. Izmir University of Economics 13

  14. BST – Implementation – Deletion IV ... • We can use stacks to convert an else if (t.left != null && t.right != null){ expression in standart form (otherwise BinaryNode<AnyType> tmp,prev; /* declare references */ known as infix ) into postfix. tmp = t.right; /* point to smallest in the right */ prev = t.right; /* point to parent of tmp */ • Example : operators = {+, *, (, )}, usual while (tmp.left != null){ /* find smallest of right */ prev = tmp; precedence rules; a + b * c + (d * e + f) * g tmp = tmp.left; } Answer = a b c * + d e * f + g * + t.element = tmp.element; /* replace with smallest */ if (tmp == prev) /* t.right is smallest */ t.right = tmp.right; /* skip over tmp */ else /* connect left of prev to right of tmp */ prev.left = tmp.right; } ... Efficient Version Izmir University of Economics 14

  15. BST – Implementation – Lazy Deletion • If the number of deletions is small, then a popular strategy to use is lazy deletion : When an element is to be deleted, it is left in the tree and merely marked as deleted. This is especially popular if duplicates are present, because then the field that keeps count of the items can be decremented. • If the number of real nodes is the same as the number of "deleted" nodes, then the depth of the tree is only expected to go up by a small constant (why?), so there is a very small time penalty associated with lazy deletion. Also, if an item is reinserted, the overhead of allocating a new cell is avoided. Izmir University of Economics 15

  16. Average-Case Analysis - I • All of the operations of BST, except MakeEmpty , take O ( d ) time where d is the depth of the node containing the accessed key. As a result, they are O (depth of tree). • Why? Because in constant time we descend a level in the tree, thus operating on a tree that is now roughly half as large. • MakeEmpty take O ( N ) time. • Observation : The average depth over all nodes in a BST is O (log N ) assuming all insertion sequences are equally likely. • Proof : The sum of the depths of all nodes in a tree is the internal path length . Let’s calculate the average internal path length over all possible insertion sequences. Izmir University of Economics 16

  17. Average-Case Analysis - II • Let D ( N ) be the internal path length for some BST T of N nodes. D (1) = 0. • D ( N ) = D ( i ) + D ( N - i - 1 ) + N -1 // Subtree nodes are 1 level deeper • All subtree sizes are equally likely for BSTs, since it depends only on the rank of the first element inserted into BST. This does not hold for binary trees though. Let’s, then, average:  N 1           D ( N ) ( 1 / N ) ( D ( i ) D ( N i 1 ) N 1 ), i 0 i N  i 0    N 1     D ( N ) ( 2 / N ) D ( i ) N 1      i 0 • If the recurrence is solved, D ( N ) = O ( N log N ). Thus, the expected depth of any node is O(log N). Izmir University of Economics 17

  18. Derivation of D ( N ) - 1  N 1           D ( N ) ( 1 / N ) ( D ( i ) D ( N i 1 ) N 1 ), i 0 i N  i 0    N 1       D ( N ) ( 2 / N ) D ( i ) N 1    0 i    N 1       ND ( N ) 2 D ( i ) N ( N 1 )......... ..( 1 )    0 i    N 2          ( 1 ) ( 1 ) 2 ( ) ( 1 )( 2 )...( 2 ) N D N D i N N    i 0        .....(subtract (2) from (1)) ND ( N ) ( N 1 ) D ( N 1 ) 2 D ( N 1 ) 2 ( N 1 )      ND ( N ) ( N 1 ) D ( N 1 ) 2 ( N 1 )  2 ( N 1 )     .....(divide by N(N+1)) D ( N ) /( N 1 ) D ( N 1 ) / N  N ( N 1 )  2 ( N 2 )      ( 1 ) / ( 2 ) /( 1 ) D N N D N N  ( N 1 ) N ... 2 * 1   D ( 2 ) / 3 D ( 1 ) / 2 2 * 3  N  1 i    ...(sum the equations side by side) ( ) /( 1 ) ( 1 ) / 2 2 D N N D  ( 1 ) i i  2 i Izmir University of Economics 18

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend