Outline • Binary Searchtree • Difference between BT and BST • BST Operations • Implementation of BST • Example Of BST • Complete Binary Tree
3.
Binary Search Tree Binarytree property • each node has 2 children • result: • storage is small • operations are simple • average depth is small Search tree property • all keys in left subtree smaller than root’s key • all keys in right subtree larger than root’s key • result: • easy to find any given key • Insert/delete by changing links
Difference between BTand BST A binary tree is simply a tree in which each node can have at most two children. A binary search tree is a binary tree in which the nodes are assigned values, with the following restrictions: 1. No duplicate values. 2. The left subtree of a node can only have values less than the node 3. The right subtree of a node can only have values greater than the node and recursively defined 4. The left subtree of a node is a binary search tree. 5. The right subtree of a node is a binary search tree.
6.
Binary Tree SearchAlgorithm TREE-SEARCH(x,k) If x==NIL or k==x.key return x If k < x.key else return TREE-SEARCH(x.left,k) return TREE-SEARCH(x.right,k)
7.
Implementation of BST: #include<iostream> class TreeNode { public: int data; TreeNode* left; TreeNode* right; TreeNode(int value) { data = value; left = nullptr; right = nullptr; } }; class BinarySearchTree { private: TreeNode* root; public: BinarySearchTree() { root = nullptr; } }; int main() { // Create an empty binary search tree BinarySearchTree bst; // No operations are performed in this example return 0; }
Algorithm for insertionin BST Check, whether value in current node and a new value are equal. If so, duplicate is found. Otherwise, if a new value is less, than the node's value: if a current node has no left child, place for insertion has been found, otherwise, handle the left child with the same algorithm. if a new value is greater, than the node's value: if a current node has no right child, place for insertion has been found; otherwise, handle the right child with the same algorithm.
Binary tree Deletion Case2. Delete a Node with Single Child in BST Deleting a single child node is also simple in BST. Copy the child to the node and delete the node.
36.
Binary tree Deletion Case3. Delete a Node with Both Children in BST Deleting a node with both children is not so simple. we have to delete the node is such a way, that the resulting tree follows the properties of a BST. The trick is to find the inorder successor of the node. Copy contents of the inorder successor to the node, and delete the inorder successor.
37.
General Algorithm forDeleting a node from BST 1. start 2. if a node to be deleted is a leaf nod at left side then simply delete and set null pointer to it's parent's left pointer. 3. If a node to be deleted is a leaf node at right side then simply delete and set null pointer to it's parent's right pointer 4. if a node to be deleted has orf child then connect it's child pointer with it's parent pointer and delete it from the tree 5. if a node to be deleted has two children then replace the node being deleted either by a. right most node of it's left sub-tree or b. left most node of it's right sub-tree. 6. End
Searching-BST Searching Through TheBST Compare the target value with the element in the root node ✓ If the target value is equal, the search is successful. ✔If target value is less, search the left subtree. ✓If target value is greater, search the right subtree. ✔If the subtree is empty, the search is unsuccessful.
Application of BST: 1.Used in many search applications where data is constantly entering/leaving, such as the map and set objects in many languages' libraries. 2. Storing a set of names, and being able to lookup based on a prefix of the name. (Used in internet routers.) 3. Storing a path in a graph, and being able to reverse any subsection of the path in O(log n) time. (Useful in travelling salesman problems). 4. Finding square root of given number 5. allows you to do range searches efficiently.
44.
Example Of BST Ina phonebook, the data consists of names and associated phone numbers. A binary search tree can be used to store this data in an organized manner, where the names are arranged in alphabetical order, and associated phone numbers are stored along with them. This organization allows for efficient searching of a specific contact and also facilitates quick insertion and deletion of contacts.
45.
Complete Binary Tree Acomplete binary tree is a special type of binary tree where all the levels of the tree are filled completely except the lowest level nodes which are filled from as left as possible.