1. BINARY SERACH TREE Binary Search Tree, is a node-based binary tree data structure which has the following properties: ▪ The left subtree of a node contains only nodes with keys less than the node’s key. ▪ The right subtree of a node contains only nodes with keys greater than the node’s key. ▪ The left and right subtree each must also be a binary search tree. There must be no duplicate nodes. Example: BINARY SERACH TREE 1.Basics INORDER-TREE-WALK(x) PREORDER-TREE-WALK(x ) POSTORDER-TREE-WALK(x) 2.Querying a Binary search tree Search Minimum Maximum Successor Predecessor 3.Insertion and Deletion i) Basics The binary-search-tree property allows us to print out in 3 ways. Such as
PREORDER-TREE-WALK(x) if x ≠NIL print x.key PREORDER-TREE-WALK(x.left) PREORDER-TREE-WALK(x,right) POSTORDER-TREE-WALK(x) if x ≠NIL POSTORDER-TREE-WALK(x.left) POSTORDER-TREE-WALK(x.right) print x.key Eg: (BST) (a) Inorder (Left, Root, Right) : 4 2 5 1 3 (b) Preorder (Root, Left, Right) : 1 2 4 5 3 (c) Postorder (Left, Right, Root) : 4 5 2 3 1 ii) Querying a Binary search tree We often need to search for a key stored in a binary search tree. It supports,
➢ ➢ ➢ ➢ ➢ SEARCH MINIMUM MAXIMUM SUCCESSOR PREDECESSOR Searching We use the following procedure to search for a node with a given key in a binary search tree. Explanation 1.The procedure begins its search at the root and compare the key need to be searched with the root node. 2.If the key is a root node return x 3. If k is smaller than x.key, the search continues in the left subtree of x and the nodes encountered during the recursion form a simple path downward from the root of the tree until key is found. 4. If k is larger than x.key, the search continues in the right subtree of x and the nodes encountered during the recursion form a simple path downward from the root of the tree until key is found. Minimum and maximum
Successor Eg: Search for the key 13 in the tree, we follow the path 15->6-> 7-> 13 from the root. The minimum key in the tree is 2, which is found by following left pointers from the root. The maximum key 20 is found by following right pointers from the root.