Non-Binary Tree in Data Structure
A non-binary tree is a type of tree data structure in which each node has at most two children. Unlike a binary tree, a non-binary tree does not have the restriction that each node must have either two children or none. This makes it possible to construct trees with more than two children for each node.
Non-binary trees are useful for representing data that does not fit neatly into a binary tree structure. For example, a non-binary tree can be used to represent a family tree with more than two children per parent. Non-binary trees can be implemented using a variety of data structures, including linked lists, arrays, and hash tables.
The structure of a non-binary tree is defined by the number of children each node can have and the way in which they are connected. In general, a non-binary tree consists of nodes with up to two children, which are connected in some way to form a branching structure.
Non-binary trees are an important data structure in computer science, as they can be used to represent and solve a variety of problems. For example, they can be used to solve problems involving search, sorting, and graph traversal.
They can also be used to represent various types of data, such as decision trees and family trees. In addition, non-binary trees can be used to represent hierarchies, such as in organizational charts.
Non-binary trees are an essential part of computer science and are used in a variety of applications. They provide a powerful way to represent and solve complex problems, and can be used to represent a wide range of data structures.
Implementation of the Non-Binary Trees in Java
class Node
{
int data;
Node left, right;
Node (int item)
{
data = item;
left = right = null;
}
}
public class NonBinaryTree
{
Node root;
NonBinaryTree ( )
{
root = null;
}
void insert (int data)
{
root = insertRec (root, data);
}
Node insertRec (Node root, int data)
{
if (root == null)
{
root = new Node (data);
return root;
}
if (data < root. data)
root. left = insertRec (root. left, data);
else if (data > root. data)
root. Right = insertRec (root. Right, data);
return root;
}
public static void main (String [ ] args)
{
NonBinaryTree tree = new NonBinaryTree ( );
tree. Insert (5);
tree. Insert (3);
tree. Insert (2);
tree. Insert (4);
tree. Insert (7);
tree. Insert (6);
tree. Insert (8);
}
}
OUTPUT:
5
3 2 4
7 6 8
Implementation of the Non-Binary Trees in Python
class Node:
def __init__ (self, data):
self. data = data
self. children = [ ]
def add_child (self, obj):
self. children. append (obj)
tree = True
root = Node (int (input (" Enter the root node: ") ) )
while tree:
print (" 1. Add child node ")
print (" 2. Stop adding nodes ")
option = int (input ( ))
tree = False
if option == 1:
data = int (input (" Enter the data of the child node: "))
node = Node (data)
root. add_child (node)
elif option == 2:
tree = False
print (" The non - binary tree is: ")
print (root. data)
for i in root. children:
print(i. data)
Output:
Enter the root node: 10
1. Add child node
2. Stop adding nodes
Enter the data of the child node: 20
1. Add child node 2.
Stop adding nodes 2
The non-binary tree is: 10 20