C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Binary Search Tree
A Binary search tree is shown in the above figure. As the constraint applied on the BST, we can see that the root node 30 doesn't contain any value greater than or equal to 30 in its left sub-tree and it also doesn't contain any value less than 30 in its right sub-tree. Advantages of using binary search tree
Q. Create the binary search tree using the following data elements.43, 10, 79, 90, 12, 54, 11, 9, 50
The process of creating BST by using the given elements, is shown in the image below. Operations on Binary Search TreeThere are many operations which can be performed on a binary search tree.
Program to implement BST operations#include <iostream> #include <stdlib.h> using namespace std; struct Node { int data; Node *left; Node *right; }; Node* create(int item) { Node* node = new Node; node->data = item; node->left = node->right = NULL; return node; } void inorder(Node *root) { if (root == NULL) return; inorder(root->left); cout<< root->data << " "; inorder(root->right); } Node* findMinimum(Node* cur) { while(cur->left != NULL) { cur = cur->left; } return cur; } Node* insertion(Node* root, int item) { if (root == NULL) return create(item); if (item < root->data) root->left = insertion(root->left, item); else root->right = insertion(root->right, item); return root; } void search(Node* &cur, int item, Node* &parent) { while (cur != NULL && cur->data != item) { parent = cur; if (item < cur->data) cur = cur->left; else cur = cur->right; } } void deletion(Node*& root, int item) { Node* parent = NULL; Node* cur = root; search(cur, item, parent); if (cur == NULL) return; if (cur->left == NULL && cur->right == NULL) { if (cur != root) { if (parent->left == cur) parent->left = NULL; else parent->right = NULL; } else root = NULL; free(cur); } else if (cur->left && cur->right) { Node* succ = findMinimum(cur- >right); int val = succ->data; deletion(root, succ->data); cur->data = val; } else { Node* child = (cur->left)? Cur- >left: cur->right; if (cur != root) { if (cur == parent->left) parent->left = child; else parent->right = child; } else root = child; free(cur); } } int main() { Node* root = NULL; int keys[8]; for(int i=0;i<8;i++) { cout << "Enter value to be inserted"; cin>>keys[i]; root = insertion(root, keys[i]); } inorder(root); cout<<"\n"; deletion(root, 10); inorder(root); return 0; } Output: Enter value to be inserted? 10 Enter value to be inserted? 20 Enter value to be inserted? 30 Enter value to be inserted? 40 Enter value to be inserted? 5 Enter value to be inserted? 25 Enter value to be inserted? 15 Enter value to be inserted? 5 5 5 10 15 20 25 30 40 5 5 15 20 25 30 40
Next TopicAVL Tree
|