Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy
Binary Search Trees A binary tree that is built such that every node's left subtree has key values less than the node's key value, and the node’s right subtree has key values greater than the node’s key value. A new node is added as a leaf. Above is NIST’s (National Institute of Standards and Technology) definition We will add a constraint that the key will be unique. 2/22/2019 B.Ramamurthy
Binary Search Tree We will define the operations of binary search tree. We will use the simple binary tree that we designed earlier. The semantics of the operation are loosely based on Chapter 9’s discussion on binary search trees. 2/22/2019 B.Ramamurthy
BST server side 2/22/2019 B.Ramamurthy
BST Client side 2/22/2019 B.Ramamurthy
2/22/2019 B.Ramamurthy
Insert, and Search methods Insert (key, element) (Non_empty tree) Locate position (cursor) to insert. This will be parent of new node (key-element pair) to be inserted. 1.1 If key < cursor’s key, insert as left leaf 1.2 Else if key >cursor’s key, insert as right leaf 1.3 Else replace value of element with new value. (Empty tree) Create a tree with node with an item made up of key,element. 2/22/2019 B.Ramamurthy
Search(key) Locate the node with the given key. 1.1 If one exists return element of the node 1.2 Else return null. Both insert and search method use a utility method boolean locate(Key k, Btree b) That return true, if key is found and sets a cursor to the location where it was found. Returns false if not found, cursor has the location of parent for insert. 2/22/2019 B.Ramamurthy