/
12/5/2004(c) 2001-4, University of Washington21-1 12/5/2004(c) 2001-4, University of Washington21-1

12/5/2004(c) 2001-4, University of Washington21-1 - PDF document

olivia-moreira
olivia-moreira . @olivia-moreira
Follow
378 views
Uploaded On 2016-11-08

12/5/2004(c) 2001-4, University of Washington21-1 - PPT Presentation

CSE143 Au04211 CSE 143Binary Search Trees 1252004c 20014 University of Washington212 Costliness of Review in a binary tree is ON worst casecontainsmay be a frequent operation in an appli ID: 486267

CSE143 Au0421-1 CSE 143Binary Search Trees 12/5/2004(c)

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "12/5/2004(c) 2001-4, University of Washi..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

CSE143 Au0421-1 12/5/2004(c) 2001-4, University of Washington21-1 CSE 143Binary Search Trees 12/5/2004(c) 2001-4, University of Washington21-2 Costliness of Review: in a binary tree, is O(N) (worst case)containsmay be a frequent operation in an application 12/5/2004(c) 2001-4, University of Washington21-4 Examples(?)Are these are binary search trees? Why or why not? 368 12/5/2004(c) 2001-4, University of Washington21-5 Implementing a Set with a BSTCan exploit properties of BSTsto have fast, divide-and-conquer implementations of add and containsTreeSet!A TreeSetcan be represented by a pointer to the root node of a binary search tree, or null of no elements yetroot;// root node, or null if none( ) { root = null; } CSE143 Au0421-3 12/5/2004(c) 2001-4, University of Washington21-13 (in TreeSet)/** Ensure that item is in the set. */public void add(Object item) {root = addToSubtree(root, (Comparable) item);// add item to tree/** Add item to tree rooted at r. Return (possibly new) tree containing item. */addToSubtree(BTNoder, Comparable item) { 12/5/2004(c) 2001-4, University of Washington21-14 addToSubtree/** Add item to tree rooted at r. Return (possibly new) tree containing item. */addToSubtree(BTNoder, Comparable item) {if (n == null) { // adding to empty treeint comp = item.compareTo(r.item);if (comp == 0) { return; }// item already in treeif (comp )// add to left subtreer.left= addToSubtree(r.left, item);} else /* comp &#x 0 {;&#x-932;�.20; 0 */ {// add to right subtreer.right= addToSubtree(r.right, item);return r;// this tree has been modified to contain item 12/5/2004(c) 2001-4, University of Washington21-15 Cost of Cost at each node:How many recursive calls?Proportional to height of treeBest case?Worst case? 12/5/2004(c) 2001-4, University of Washington21-16 How to return an iterator that traverses the sorted set in Need to iterate through the items in the BST, from smallest to largestProblem: how to keep track of position in tree where iteration is currently suspendedNeed to be able to implement next( ), which advances to the correct next node in the treeSolution: keep track of a path from the root to the current nodeStill some tricky code to find the correct next node in the tree 12/5/2004(c) 2001-4, University of Washington21-17 Algorithm: find the node contairemoved, and remove that node from the treereplace with an empty tree non-empty subtreeis easy: replace with that subtreeHow to remove a node that has two non-empty subtrees?Need to pick a new element to be the new root node, and adjust at least one of the subtreesE.g., remove the largest element of the left subtree(will be one of the easy cases described above), make that the new root 12/5/2004(c) 2001-4, University of Washington21-18 Analysis of Binary Search Tree OperationsCost of operations is proportional to height of treeBest case: tree is balancedDepth of all leaf nodes is roughly the sameHeight of a balanced tree with nodes is ~log If tree is unbalanced, height can be as bad as the number of nodes in the treeTree becomes just a linear list