/
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

liane-varnes
liane-varnes . @liane-varnes
Follow
392 views
Uploaded On 2016-05-24

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

CSE143 Au04211 Binary Search Trees 1252004c 20014 University of Washington212 Costliness of contains Idea order the nodes in the tree so that given that a All nodes in its left subtreecontai ID: 332242

CSE143 Au0421-1 Binary Search Trees 12/5/2004(c) 2001-4

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 Binary Search Trees 12/5/2004(c) 2001-4, University of Washington21-2 Costliness of contains Idea: order the nodes in the tree so that, given that a All nodes in its left subtreecontain values All nodes in its right subtreec&#x 000;ontain values A binary tree with these properties is called a binary (BST) CSE143 Au0421-3 12/5/2004(c) 2001-4, University of Washington21-9 Cost of BST Work done at each node:Number of nodes visited (depth of recursion): 12/5/2004(c) 2001-4, University of Washington21-10 Must preserve BST invariant: insert new element in correct place in BSTTwo base casesTree is empty: create new node which becomes the root of the If node contains the value, found it; suppress duplicate addRecursive caseCompare value to current node’s valueIf value t node's value, add to left subtreerecursivelyOtherwise, add to right subtreerecursively 12/5/2004(c) 2001-4, University of Washington21-11 Add 8, 10, 5, 1, 7, 11 to an initially empty BST, in that 12/5/2004(c) 2001-4, University of Washington21-12 What if we change the order in which the numbers are Add 1, 5, 7, 8, 10, 11 to a BST, in that order (following the algorithm): CSE143 Au0421-5 12/5/2004(c) 2001-4, University of Washington21-17 Algorithm: find the node contairemoved, and remove that node from the treeRemoving a leaf node is easy: replace with an empty treeRemoving a node with only one non-empty subtreeis easy: How 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 Cost of operations is proportional to height of treeBest case: tree is Depth of all leaf nodes is roughly the sameIf tree is unbalanced, height can be as bad as the number of nodes in the treeTree becomes just a linear list 12/5/2004(c) 2001-4, University of Washington21-19 A binary search tree is a good general implementation of a set, if the elements can be orderedBoth contains and add benefit from divide-and-conquer Good properties depend on the tree being roughly balancedNot covered (or, why take a data structures course?)How are other operations implemented (e.g. iterator, remove)?How do you keep the tree balanced as items are added and