Javatpoint Logo
Javatpoint Logo

Types of Tree in Data Structure

Before understanding the types of Tree in Data Structure, first, let us understand what is Tree as a Data Structure. The tree can be defined as a non-linear data structure that stores data in the form of nodes and nodes are connected to each other with the help of edges. Among all the nodes there is one main node called root node and all other nodes are the children of these nodes. There are some properties or rules that each and every node should follow or satisfy. And these rules are:

  • Each tree emerges from the one main node called the root node. And all other nodes are children of this root node. The root node doesn't have any parent.
  • Each node in a tree can have multiple children associated with that particular parent node but each child node should have only one parent node strictly.
  • Each parent node in the tree is connected to its child node with the help of an edge that will be used to traverse the tree in case the traversal of the tree is needed. In some types of trees, their edges are denoted some numeric value called cost of that is used to determine the overall cost of traversing or traversing that particular node of the tree will be calculated by adding all the edges that are used to join that particular node to the root node.

As we can see in the above image, there is shown a tree having a root node named A and having three children named B, C, and D. And the child node B again have two children nodes by the name E and F. Similarly, the node C has three child nodes named G, H and I. And same for node D having J and K as child nodes. If we want to traverse to node J, first we will start from the root node A and then go to the child node D and then finally reach node J.

The various types of trees that are available:

  • General Tree
  • Binary Tree
  • Binary Search Tree
  • AVL Tree
  • Red Black Tree
  • N-ary Tree

Now let us see each one of them in detail to have a better understanding of each of them.

General Tree:

A General Tree is one of the basic forms of Tree Data Structure. In General Tree, each node can have either zero or more than zero nodes associated with it as the child nodes. The subtree of a general tree does not hold the ordered property. In a general tree, a node can have at most n (number of child nodes) nodes. If no constraint is placed on the tree's hierarchy, a tree is called a general tree. Every node may have infinite numbers of children in the General Tree. The tree is the super-set of all other trees.

Code:

Output:

Printing the nodes of tree level wise :
Level order traversal :
(level 0) 10
(level 1) 2 34 56 100
(level 2) 77 88 1 7 8 9

In the code written above, we have created a class named GenralTree that has one static inner class named Node that will depict the actual node of a tree. And we have also a public static function named newNode that is used to add a new node to the tree and the value that the newly added node holding is passed as a parameter to this newNode function.

In the main function first, we have created a root node as an object of the inner static class named Node having a value of 10. Then we added four nodes as the child nodes to the root having values 2, 34, 56, and 100. There newly added four children will be in level 1 and the root node will be in level 0. Now we added two child nodes to the first node in level 1 (i.e., node having value 2) having values 77 and 88 respectively. Further, we added one child node to the second node in level 1 (i.e., node having value 34) having value 1. And in the last, we added three child nodes to the last node in level 1 (i.e., node having value 100) having values 7, 8, and 9 respectively.

Once the tree is created successfully, we have printed the tree level-wise, which means the nodes in one level are printed and then the nodes in the next level are printed until all the nodes in the tree are exhausted.

And for printing the tree level-wise, we have created a function named LevelOrderTraversal that will print the tree level-wise. After the successful creation of the tree, we have called the LevelOrderTraversal passing the root of the node as a parameter to the LevelOrderTraversal function.

Binary Tree:

A binary tree can be defined as one of the trees in which only two children can be added to each parent node. The child nodes are known as the left child node and right child node. A binary tree is one of the most popular trees. When we apply various constraints and characteristics to a Binary tree, various numbers of other trees such as AVL tree, BST (Binary Search Tree), RBT tree, etc. are formed. We will explain in detail these types of trees in further discussion.

In other words, we can say that a generic tree whose elements have at most two children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right children.

A Binary Tree node contains the following parts.

  • Data
  • Pointer to the left child
  • Pointer to the right child

Code:

Output:

Printing the nodes of tree level wise :
Level order traversal :
(level 0) 150
(level 1) 250 270
(level 2) 320 350

In the code written above, we have created a class named BinaryTree that has one another class named Node that will depict the actual node of a tree.

In the main function first, we have created a root node as an object of the class named Node having a value of 150. Then we added two nodes as the child nodes to the root having values 250, and 270. There newly added two children will be in level 1 and the root node will be in level 0. Now we added two child nodes to the first node in level 1 (i.e., node having value 250) having values 320 and 350 respectively.

Once the tree is created successfully, we have printed the tree level-wise, which means the nodes in one level are printed and then the nodes in the next level are printed until all the nodes in the tree are exhausted.

And for printing the tree level-wise, we have created a function named printLevelOrder that will print the tree level-wise. After the successful creation of the tree, we have called the printLevelOrder function.

Binary Search Tree:

Binary Search Tree, also known as BST, is a binary tree extension having various restrictions.

The main constraint in the Binary Search Tree is that the value of the left child value of a node should be less than or equal to the value of the parent node, and the value of the right child value should be greater than or equal to the value of the parent node.

As the name depicts, the Binary Search Tree is best for searching operations because at each node we can determine where to move, that means if we are at a particular node and our search key has a value less than the value of that node then we need to move to left and vice-versa if the search key value is greater.

Code:

Output:

Printing the nodes of tree level wise :
Level order traversal :
(level 0) 50
(level 1) 30 70
(level 2) 20 40 60 80

In the code written above, a binary search tree with a root node having value 50 is created and then one left child and right child with values 30 and 70 are added to it respectively. Then we added one left child and right child with values 20 and 40 to the left child in level1 and after that, we added one left child and right child with values 60 and 80 to the right child of the root node in level1.

Once the tree is created successfully, we have printed the tree level-wise, which means the nodes in one level are printed and then the nodes in the next level are printed until all the nodes in the tree are exhausted.

And for printing the tree level-wise, we have created a function named printLevelOrder that will print the tree level-wise. After the successful creation of the tree, we have called the printLevelOrder function.

AVL Tree:

AVL tree can be defined as a self-balancing binary search tree. The name AVL is given on behalf of the inventors Adelson-Velshi and Landis. The AVL tree has the property to balance the nodes of the tree dynamically. An additional metric named the balancing factor is added that determines whether the tree or subtree needs to be balanced or not for each node in the AVL tree. The height of the node is at most 1. In the AVL tree, the correct balance factor is 1, 0, and -1. If the tree has a new node, it will be rotated to ensure that it is balanced.

Various operations like viewing, insertion, and removal take O(log n) time in the AVL tree. It is mostly applied when working with Lookups operations.

Code:

Output:

Printing the nodes of tree-level wise :
Level order traversal :
45
24 87
12 33 94

Red Black Tree:

Just like AVL, red-black tree is also a type of auto-balancing tree. The name red-black is given because each node is a particular color either red or black depending upon some conditions. As it is a self-balancing tree, it maintains the overall balance of the tree. The searching operation only takes O (log n) time on the AVL tree. The addition of new nodes in the Red-Black Tree results in the rotation of the existing nodes to maintain the Red-Black Tree's properties.

Code:

Output:

Printing the nodes of tree level wise :
Level order traversal :
 
| THREE | B | ONE | TWO |
 
| ONE | B | FOUR | NULL | | TWO | B | NULL | NULL |
 
| FOUR | R | NULL | NULL |

So, in the above code, we have successfully implemented a Red-Black tree with the insert and traversal operation. In the output, the data printed is in the order

  • Data in the node
  • Color of that node
  • Left Child of that particular node
  • Right Child of that particular node

If the right child node or the left child node of a tree is NULL that means that particular nodes either don't have the right child or left child respectively.

N-ary Tree:

In the N-ary tree, the maximum number of child nodes that can be associated with the preceding parent node is N. A N-ary Tree is very similar to the general or generic tree which can also have any number of nodes in the tree.

The implementation of the N-ary Tree is exactly the same as the General tree in the java programming language.

So, in this article, we get a clear idea about the different types of trees and we have seen each one of them in brief for a better understanding. We have also implemented a sample java code for all of these types of trees like General Tree, Binary Tree, Binary Search Tree, AVL Tree, Red-Black Tree, and N-ary Tree.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA