What is Tree in Data Structures ? In-Order,Pre-order,Post-Order Display in Tree

Tree :Binary Tree is a special data structure used for data storage purposes. A binary tree has a special condition that each node can have a maximum of two children.

A complete binary tree is a tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right. A complete binary tree of the height h has between 2h and 2(h+1)-1

Path − Path refers to the sequence of nodes along the edges of a tree.

Root − The node at the top of the tree is called root. There is only one root per tree and one path from the root node to any node.

Parent − Any node except the root node has one edge upward to a node called parent.

Child − The node below a given node connected by its edge downward is called its child node.

Leaf − The node which does not have any child node is called the leaf node.

Subtree − Subtree represents the descendants of a node.

Visiting − Visiting refers to checking the value of a node when control is on the node.

Traversing − Traversing means passing through nodes in a specific order.

Levels − Level of a node represents the generation of a node. If the root node is at level 0, then its next child node is at level 1, its grandchild is at level 2, and so on.

keys − Key represents a value of a node based on which a search operation is to be carried out for a node.

Binary search tree data structure,

Insert − Inserts an element in a tree/create a tree.

Search − Searches an element in a tree.

Preorder Traversal − Traverses a tree in a pre-order manner.

Inorder Traversal − Traverses a tree in an in-order manner.

Postorder Traversal − Traverses a tree in a post-order manner.

In-Order Display in Tree

void inorder_traversal(node* root)
{ if (root != NULL)
{ inorder_traversal(root->leftChild);
cout << root->data << " ";
inorder_traversal(root->rightChild);
} }

Pre-order Display in Tree

void pre_order_traversal(node* root)
{ if (root != NULL)
{
cout << root->data << " ";
pre_order_traversal(root->leftChild);
pre_order_traversal(root->rightChild);
} }

Post-Order Display in tree

void post_order_traversal(node* root)
{
if (root != NULL)
{
post_order_traversal(root->leftChild);
post_order_traversal(root->rightChild);
cout << root->data << " ";
}