








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A comprehensive guide to binary search trees (bsts), a fundamental data structure in computer science. It covers the definition, properties, advantages, and implementation of bsts, including searching, insertion, and deletion operations. The document also explores different types of binary trees, such as complete, full, and perfect binary trees. It is a valuable resource for students and professionals seeking to understand and apply bsts in various applications.
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!
Tree Data Structure: A tree is a non-linear data structure consisting of nodes connected by edges. It is used to represent hierarchical structures, such as file systems, computer directories, and organization charts. Trees are a fundamental data structure used in computer science, and they have many applications, such as in search algorithms, decision-making processes, and data compression. The topmost node in a tree is called the root node, which has no parent nodes. Each node in a tree can have one or more child nodes, except for the leaf nodes, which have no children. Nodes that share the same parent node are called siblings.
A tree is a kind of data structure that is used to represent the data in hierarchical form. It is a non- linear data structure as the data in a tree is not stored linearly or sequentially.It can be defined as a collection of objects or entities called as nodes that are linked together to simulate a hierarchy. A Binary Search Tree (BST) is a special type of binary tree in which the left child of a node has a value less than the node’s value and the right child has a value greater than the node’s value. This property is called the BST property and it makes it possible to efficiently search, insert, and delete elements in the tree. The root of a BST is the node that has the smallest value in the left subtree and the largest value in the right subtree. Each left subtree is a BST with nodes that have smaller values than the root and each right subtree is a BST with nodes that have larger values than the root. Binary Search Tree is a node-based binary tree data structure that has the following properties: The left subtree of a node contains only nodes with keys lesser than or equal the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. This means everything to the left of the root is less than the value of the root and everything to the right of the root is greater than the value of the root. Due to this performing, a binary search is very easy. The left and right subtree each must also be a binary search tree. There must be no duplicate nodes(BST may have duplicate values with different handling approaches) // Given Node node struct node { int key; struct node left, right; }; // Function to create a new BST node struct node newNode(int item) { struct node temp= (struct node*)malloc(sizeof(struct node)); temp->key = item;
Commonly performed operations on binary search tree are- Search Operation Insertion Operation Deletion Operation
Search Operation is performed to search a particular element in the Binary Search Tree.
For searching a given key in the BST, Compare the key with the value of root node. If the key is present at the root node, then return the root node. If the key is greater than the root node value, then recur for the root node’s right subtree. If the key is smaller than the root node value, then recur for the root node’s left subtree.
We start our search from the root node 25. As 45 > 25, so we search in 25’s right subtree. As 45 < 50, so we search in 50’s left subtree. As 45 > 35, so we search in 35’s right subtree. As 45 > 44, so we search in 44’s right subtree but 44 has no subtrees. So, we conclude that 45 is not present in the above BST.
Insertion Operation is performed to insert an element in the Binary Search Tree.
The insertion of a new key always takes place as the child of some leaf node. For finding out the suitable leaf node,
Consider the following example where key = 40 is inserted in the given BST-
else if (root->key > val) root->left = insert(root->left,val); /*
Deletion Operation is performed to delete a particular element from the Binary Search Tree. When it comes to deleting a node from the binary search tree, following three cases are possible-
Just remove / disconnect the leaf node that is to deleted from the tree.
Consider the following example where node with value = 20 is deleted from the BST-
Just make the child of the deleting node, the child of its grandparent.
Consider the following example where node with value = 30 is deleted from the BST-
A node with two children may be deleted from the BST in the following two ways-
Visit to the right subtree of the deleting node. Pluck the least value element called as inorder successor. Replace the deleting element with its inorder successor.
Consider the following example where node with value = 15 is deleted from the BST-
temp->left = temp->right = NULL; return temp; } // A utility function to insert // a new node with given key in BST struct node* insert(struct node* node, int key) { // If the tree is empty, return a new node if (node == NULL) return newNode(key); // Otherwise, recur down the tree if (key < node->key) node->left = insert(node->left, key); else if (key > node->key) node->right = insert(node->right, key); // Return the (unchanged) node pointer return node; } // Utility function to search a key in a BST struct node* search(struct node* root, int key) { // Base Cases: root is null or key is present at root if (root == NULL || root->key == key) return root; // Key is greater than root's key if (root->key < key) return search(root->right, key); // Key is smaller than root's key return search(root->left, key); } // Driver Code int main() { struct node* root = NULL; root = insert(root, 50); insert(root, 30); insert(root, 20); insert(root, 40); insert(root, 70); insert(root, 60); insert(root, 80); // Key to be found int key = 6; // Searching in a BST
if (search(root, key) == NULL) printf("%d not found\n", key); else printf("%d found\n", key); key = 60; // Searching in a BST if (search(root, key) == NULL) printf("%d not found\n", key); else printf("%d found\n", key); return 0; }