What Data Structure Does a Binary Tree Degenerate to
Definition of Binary Tree in Data Structure
Information construction provides the dissimilar types of trees to the user and we can utilize them as per the user requirement. The binary tree is one tree type in the data structure; information technology is a special type of tree. In a binary tree, every node or every vertex has ii kid nodes or single child nodes, or no child node. Basically, a binary tree is a very important class in a information structure in which nodes of a binary tree have at most ii children nodes. In the binary tree, the left side is called the left kid node and the right side of the binary tree is chosen the right child node.
Syntax:
struct b_node
{
int item;
struct b_node *left_node;
struct b_node *right_node;
};
Explanation
In the above syntax we represent a binary tree, here we use structure to represent the binary tree that contains two pointers with two items, 1 for the left child node and one for the right child node as shown in the above syntax.
How Binary tree works in information structure?
Now let's come across how a binary tree works in a data structure as follows.
In to a higher place show the binary tree; see hither above tree contains at almost two child nodes equally shown in the to a higher place figure. Some important terms are used in binary trees every bit follows.
Path: Path is used to stand for the sequence of nodes either the left side or right side.
Root: The topmost node of the tree we call a root node and each tree has just one root node for example A is a root node.
Parent: We can consider any node as a parent node except the root node for case B as a parent node.
Child: Below parent node tree contains the child node for example D and Due east is a kid node of B.
Leafage: In a binary tree the node does non have any child node that node we called a leaf node for instance D.
Subtree: binary we contain the subtree for instance in the above binary we show the subtree by using the foursquare.
Keys: It is used to correspond the value of a node and that is used to perform the search and insert operation.
Now allow'due south see the basic performance of the Binary tree equally follows.
Insert Operation:
Binary tree representation starts with the insertion operation. After that, we insert the node on its proper location and the node location is based on the primal value of the node.
Search Operation:
When nosotros demand to search an element in the binary tree, and so nosotros beginning the searching from the root node and then compare the item value or element value with key values. If the search value is less than the key value, and so nosotros perform the search on the left side and if the search value is greater than central value then we perform a search at the correct side.
Preorder Traversal:
Nosotros traverse the node in a pre-club manner as per requirement.
Inorder Traversal:
We traverse the node in an in-order fashion as per requirement.
Postorder Traversal:
We traverse the node in a post-order way as per requirement.
Different Types of Binary Tree
Unlike types of a binary tree equally follows.
1. Full or strict or proper Binary tree
The full binary means if each node should have been 0 or 2 child nodes then nosotros say that this tree is a full binary tree and total binary we can besides call it a strict binary tree. The simple example of a total binary tree we illustrated by using the following figure as follows.
2. Complete Binary tree
The complete binary tree is a tree where every ane of the nodes is totally filled with the exception of the last level. In the last level, every i of the nodes should be just about as left as could be expected. In a consummate binary tree, the nodes should be added from the left side. The complete binary tree is illustrated by using the following figure as follows.
iii. Perfect Binary Tree
In a perfect binary tree if all internal nodes have 2 children nodes and all leafage nodes have the aforementioned level then nosotros tin say this tree is a perfect binary tree. The perfect binary nosotros illustrated by using the post-obit figure as follows.
4. Degenerate Binary Tree
The degenerate binary tree ways all internal nodes accept merely ane kid node either the left side or the right side of the tree. The degenerate binary tree we illustrate by using the following figure as follows.
The above tree has only one child node and it is also chosen a right-skewed tree. Similarly, we tin draw the left-skewed tree.
5. Counterbalanced Binary Tree:
The balanced tree means both sides of the tree that is left and right side of the tree differ by at most 1. The balanced binary we illustrated by using the following effigy equally follows.
Example
Now let's see the case of a binary tree in a data structure as follows.
course B_Node:
def __init__(self, key_value):
self.left_B = None
self.right_B = None
cocky.val_B = key_value
def PreOrder_B(self):
print(self.val_B, end=' ')
if cocky.left_B:
self.left_B.PreOrder_B()
if cocky.right_B:
self.right_B.PreOrder_B()
def InOrder_B(cocky):
if cocky.left_B:
self.left_B.InOrder_B()
print(cocky.val_B, end=' ')
if self.right_B:
self.right_B.InOrder_B()
def PostOrder_B(self):
if self.left_B:
self.left_B.PostOrder_B()
if self.right_B:
cocky.right_B.PostOrder_B()
print(self.val_B, cease=' ')
root = B_Node(4)
root.left_B = B_Node(3)
root.right_B = B_Node(5)
root.left_B.left_B = B_Node(1)
print("Pre-order is: ", stop="")
root.PreOrder_B()
impress("\nIn order is: ", end="")
root.InOrder_B()
print("\nPost order is: ", end="")
root.PostOrder_B()
Explanation
In the above instance, we use a python programming language to implement the binary tree in the data construction. In this example, we implement three different operations a pre-order tree traversal, in-gild tree traversal, and postorder tree traversal as shown in the above programme. The final output of the above statement we illustrate past using the following snapshot.
Decision
We hope from this article yous learn the Binary tree in a information structure. From the above article, we take learned the bones syntax of the Binary tree and we also see unlike examples of the Binary tree. From this article, nosotros learned how and when we use the Binary tree in a data structure.
Recommended Articles
This is a guide to Binary Tree in Information Construction. Hither we talk over Definition, syntax, How Binary tree works in information structure?, Different types of binary tree, examples with code implementation. Yous may also have a wait at the following manufactures to larn more than –
- Heap Data Structure
- Hashing in Data Construction
- B+ Tree in Data Structure
- Linked List in Data Structure
Source: https://www.educba.com/binary-tree-in-data-structure/
Post a Comment for "What Data Structure Does a Binary Tree Degenerate to"