Binary Search Tree ImplementationThis article explains the many operations of a binary search tree application that has been coded in the C programming language. A binary search tree is a binary tree where each node's left subtree value is less than the node's value, which is less than each value in the right subtree. In this article, we'll define a binary search tree and show you how to use the C programming language to accomplish various binary search tree operations. A specific type of data structure called a tree is used to represent data in a hierarchical format. It can be described as a group of nodes-collections of things or entities-that are connected to one another to create the illusion of a hierarchy. Trees are non-linear data structures because their data is not organised in a linear or sequential manner. The items are arranged in a binary search tree according to a certain order. A Binary search tree requires that the left node's value be less than the parent node and the right node's value be larger than the parent node. The left and right branches of the root are subject to this rule in a recursive fashion. It is advised to first see a quick explanation of the Tree data structure before moving on to the binary search tree. Basics of the Binary Search Tree in CA binary search tree is a type of tree data structure that enables users to sort and store information. Because each node can only have two children, it is known as a binary tree. It is also known as a search tree because we can look up numbers in O(log(n)) time. Typical characteristics of a binary tree are:
Binary Search Tree ExampleThe diagram below shows two binary search trees, one of which complies with all of the requirements for a binary search tree while the other deviates from the standard. The right subtree of node 3 includes a value less than it, hence the binary tree on the right isn't a binary search tree. How to Operate a Binary Search Tree in CA binary search tree can be processed using three fundamental operations: Operation 1: SearchWe need to locate a particular piece in the data structure in Search. Because the elements are stored in sorted order in binary search trees, the searching process is made simpler. The following is the algorithm for seeking an element in a binary tree:
With the aid of an example where we are attempting to search for an item having a value of 20, let's now explore search operation in a binary search tree code written in C: Step 1: Step 2: Step 2: Operation 2: InsertIn a binary search tree, an element is always inserted at the leaf node. If the element to be inserted is smaller than the root value or the root node, we start our search operation from the root node and look for an empty position in the left subtree; otherwise, we look for the empty location in the right subtree. The following is the algorithm for adding an element to a binary tree: C Code: Now that we have a specific instance where we are trying to insert an item with the value 65, let's examine how insertion works in a binary search tree: Operation 3: DeletionA node from the binary search tree must be deleted during the deletion operation without violating any of its attributes. Deletion may happen in one of three situations: 1. Leaf node is the first node to be eliminated. The simplest scenario for removing a node from a binary search tree is this one. Here, we'll swap out the leaf node for NULL and release the space that was allotted. With the aid of an example that shows us to delete the node with a value of 90, we can better comprehend the deletion process in a binary search tree. 2. The deleted node has just one child node. In this instance, the target node will be replaced with its child, which will then be deleted. As a result, the value to be erased will now be present in the child node. Therefore, to release the space allotted, we will simply replace the child node with NULL. In the example below, a node with a value of 79 must be eliminated. Since this node has just one child, 55 will be used in its place. 3. There are two children of the to-be-deleted node. In comparison to the other two cases, this one is more complicated. Here, we do the following actions to remove the node:
In the example below, the root node, node 45, must be deleted. As a result, node 55 will be added in its place as the root node's immediate successor. Node 45 will now be near the tree's leaf, making it simple to delete. A C Program for Implementing a Binary Search TreeNow let's use C programming to implement the formation of a node and traversals in Binary Trees. Here, we'll concentrate on actions that affect the binary search tree, such as insertion and deletion of nodes and searching. C Program: Output 1 5 7 9 12 15 20 25 30 40 42 45 5 7 12 15 20 25 30 42 Array Representation and Traversals in a Binary Search Tree Program Written in CWe will now use an array to create a binary search tree programme in C. A binary tree will be created in C using array representation, and inorder, preorder, and postorder traversals will then be implemented. Consider the Following Array and Try to Create a Tree from It: In the figure above, 0 stands for NULL. For example, node B's right and left children are both NULL, indicating that B is a leaf node. C Program: Output Preorder: D A E G Q B F R V T J L Postorder: G Q E B A V R J L T F D Inorder: G E Q A B D V R F J T L C Program for Binary Search Trees with Traversals and Linked List RepresentationWe will now use a linked list to implement a binary tree in C. A binary tree will be created in C using a linked list representation, and inorder, preorder, and postorder traversals will then be implemented. C Program: Output Preorder: D A E B F Postorder: E B A F D Inorder: E A B D F Binary Search Tree Complexity in CTime ComplexityInsertion
Deletion
Searching
Space Complexity
[The number of tree nodes in this case is n.] Binary Search Tree in C Applications
Conclusion
So that's the end of the article. I sincerely hope you find this post to be educational and useful. |