Download presentation
Presentation is loading. Please wait.
1
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies razdan@asu.edu http://dcst2.east.asu.edu/~razdan/cst230/
2
CST 230 Razdan et al2 BST and Total Order Binary search trees are a subset (generalization) of Binary Trees The set of objects stored in a binary tree must come from a totally ordered set A Total Order has the following mathematical properties: –Equality –Totality –Consistency –Transitivity
3
CST 230 Razdan et al3 BST Definition In a Binary Search Tree, the elements of the nodes can be compared with a total order semantics. These two rules are followed for every node n: –Every element in n’s left subtree is less than or equal to the element in n –Every element in n’s right subtree is greater than or equal to the element in n.
4
CST 230 Razdan et al4 Example 45 9 53 317 20 5354
5
CST 230 Razdan et al5 Typical BST Methods add remove find other variations: –find min –find max –add/combine BSTs
6
CST 230 Razdan et al6 add a new Node Suppose we want to add 23 and 50 to the following BST 45 9 53 317 20 5354
7
CST 230 Razdan et al7 add cont... if tree is empty make the new node the root else insert in the subtree under the current root if val <= root if root has left child insert in subtree under left child else make val the left child else if root has right child insert in subtree under right child else make val the right child
8
CST 230 Razdan et al8 remove Suppose we want to remove 17, 45 from the BST 45 9 53 317 20 5354
9
CST 230 Razdan et al9 remove cont... find node to remove if node is a leaf set parent’s appropriate child to null else if node has no left child set parent’s appropriate child to right child else if node has no right child set parent’s appropriate child to left child else set data in node to max value in left subtree remove max value in left subtree
10
CST 230 Razdan et al10 Complexity of BST methods Best Case –find –add –remove Worst Case –find –add –remove
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.