# C Programming Examples on Trees

Here is the listing of C programming examples on Trees.

## 1. C Examples on Tree Traversals

To traverse a tree means to visit the vertices of the tree in some systematic order. preorder: visit each node before its children. postorder: visit each node after its children. inorder : visit left subtree, node, right subtree. Depth first search is a way of traversing graphs, which is closely related to preorder traversal of a tree. The C programs in this section demonstrate the DFS traversal of a tree using recursion and without using recursion, common tree traversals using recursion and without using recursion, implement DFS using post order, searches for an element in the tree recursively and non-recursively and builds a binary tree using inorder or postorder traversal as input.

## 2. C Examples on Heap and Binary Tree Implementation

A Binary Tree is made up of a set of nodes in which each node has atmost two children, one is the left child and other is the right child. The C programs in this section implement Binary Tree using linked list and performs the deletion and inorder traversal on it. It also constructs a Balanced Binary Tree using sorted array. A binary tree where no leaf is more than a certain amount farther from the root than any other is called a Balanced Binary Tree. After inserting or deleting a node, the tree may be re-balanced with “rotations”. The heap data structure is an ordinary binary tree with two properties. The shape property and the heap property. The shape property states that the tree is perfectly balanced and that the elements at the bottom level are pushed as far to the left as possible. The heap property simply states that every element of the tree is larger than any of its descendants if they exists. The given program Implements a heap and provides insertion and deletion operation.

## 3. C Examples dealing with the Nodes of a Tree

The components of a tree are called nodes. At the top is the root node of the tree; in the interior of the diagram there are branch nodes; at the bottom are the leaf nodes, from which no further branches extend. The C programs in this section performs counting the of-leaf-nodes-in-a-binary-tree and non-leaf-nodes-in-a-binary-tree. The maximum height of a binary tree is defined as the number of nodes along the path from the root node to the deepest leaf node. The depth of a binary tree is the depth of its deepest node. The programs in the section calculate the height and depth of given binary Tree and prints it.

## 4. C Examples on Special Properties of Binary Trees

If you are given a binary tree and two nodes and you have to find the lowest common ancestor, you can use the following approach.

1. If the binary tree itself is null, you don’t have any common ancestor. This is an error condition.

2. If one of the two nodes is the root, then the root itself is the common ancestor.

3. Otherwise, recursively start finding the ancestor on the left node and right node.

The C programs in this section finds the Nearest Common Ancestor in the Given set of Nodes. The other programs in the section check whether a tree and its mirror image are same, performs conversion of binary tree to binary search tree, finds the total columns in a given binary search tree and calculates the sum of all nodes in a tree.

## 5. C Examples on BFS and DFS Traversals

A Binary Tree is made up of a set of nodes in which each node has at-most two children, one is the left child and other is the right child. Inorder : visit left subtree, node, right subtree. Breadth-first search is a way to find all the vertices reachable from the a given source vertex. A sibling is a node that has same parent. The C programs in this section construct a tree & perform insertion, deletion, display, checks whether a tree is a binary search tree, finds the nearest sibling of a node in a tree, finds 2 nodes with longest distance and display using inorder traversal and displays the nodes of a tree using BFS traversal.

## 6. C Examples on Inorder Traversal of a Binary Tree

Inorder : visit left subtree, node, right subtree. The C programs in this section searches for a particular value in a binary tree, finds the sum of all nodes in a binary tree, constructs a binary search tree and perform deletion and inorder traversal, counts number of leaf node in a tree and finds nodes which are at maximum distance in binary tree. A Binary Tree is made up of a set of nodes in which each node has atmost two children, one is the left child and other is the right child. The components of a tree are called nodes. At the top is the root node of the tree; in the interior of the diagram there are branch nodes; at the bottom are the leaf nodes, from which no further branches extend.