Trees: Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are hierarchical data structures.

Tree Vocabulary: The topmost node is called root of the tree. The elements that are directly under an element are called its children. The element directly above something is called its parent. For example, a is a child of f and f is the parent of a. Finally, elements with no children are called leaves.

      tree
      ----
       j    <-- root
     /   \
    f      k  
  /   \      \
 a     h      z    <-- leaves

Why Trees?
1. One reason to use trees might be because you want to store information that naturally forms a hierarchy. For example, the file system on a computer:

file system
-----------
     /    <-- root
  /      \
...       home
      /          \
   ugrad        course
    /       /      |     \
  ...      cs101  cs112  cs113

2. Trees (with some ordering e.g., BST) provide moderate access/search (quicker than Linked List and slower than arrays).
3. Trees provide moderate insertion/deletion (quicker than Arrays and slower than Unordered Linked Lists).
4. Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on number of nodes as nodes are linked using pointers.

Main applications of trees include:
1. Manipulate hierarchical data.
2. Make information easy to search (see tree traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for visual effects.
5. Router algorithms
6. Form of a multi-stage decision-making (see business chess).

[ad type=”banner”]

Binary Tree: A tree whose elements have at most 2 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 child.

Binary Tree Representation in C: A tree is represented by a pointer to the topmost node in tree. If the tree is empty, then value of root is NULL.
A Tree node contains following parts.
1. Data
2. Pointer to left child
3. Pointer to right child

in C, we can represent a tree node using structures. Below is an example of a tree node with an integer data.

[pastacode lang=”c” manual=”struct%20node%20%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20node%20*left%3B%0A%20%20struct%20node%20*right%3B%0A%7D%3B” message=”” highlight=”” provider=”manual”/]

First Simple Tree in C
Let us create a simple tree with 4 nodes in C. The created tree would be as following.

      tree
      ----
       1    <-- root
     /   \
    2     3  
   /   
  4
[pastacode lang=”c” manual=”struct%20node%20%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node%20*left%3B%0A%20%20%20%20struct%20node%20*right%3B%0A%7D%3B%0A%20%0A%2F*%20newNode()%20allocates%20a%20new%20node%20with%20the%20given%20data%20and%20NULL%20left%20and%20%0A%20%20%20right%20pointers.%20*%2F%0Astruct%20node*%20newNode(int%20data)%0A%7B%0A%20%20%2F%2F%20Allocate%20memory%20for%20new%20node%20%0A%20%20struct%20node*%20node%20%3D%20(struct%20node*)malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%2F%2F%20Assign%20data%20to%20this%20node%0A%20%20node-%3Edata%20%3D%20data%3B%0A%20%0A%20%20%2F%2F%20Initialize%20left%20and%20right%20children%20as%20NULL%0A%20%20node-%3Eleft%20%3D%20NULL%3B%0A%20%20node-%3Eright%20%3D%20NULL%3B%0A%20%20return(node)%3B%0A%7D%0A%20%0A%20%0Aint%20main()%0A%7B%0A%20%20%2F*create%20root*%2F%0A%20%20struct%20node%20*root%20%3D%20newNode(1)%3B%20%20%0A%20%20%2F*%20following%20is%20the%20tree%20after%20above%20statement%20%0A%20%0A%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%2F%20%20%20%5C%0A%20%20%20%20%20NULL%20%20NULL%20%20%0A%20%20*%2F%0A%20%20%20%0A%20%0A%20%20root-%3Eleft%20%20%20%20%20%20%20%20%3D%20newNode(2)%3B%0A%20%20root-%3Eright%20%20%20%20%20%20%20%3D%20newNode(3)%3B%0A%20%20%2F*%202%20and%203%20become%20left%20and%20right%20children%20of%201%0A%20%20%20%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%20%20%20%2F%20%20%20%5C%0A%20%20%20%20%20%20%20%202%20%20%20%20%20%203%0A%20%20%20%20%20%2F%20%20%20%20%5C%20%20%20%20%2F%20%20%5C%0A%20%20%20%20NULL%20NULL%20NULL%20NULL%0A%20%20*%2F%0A%20%0A%20%0A%20%20root-%3Eleft-%3Eleft%20%20%3D%20newNode(4)%3B%0A%20%20%2F*%204%20becomes%20left%20child%20of%202%0A%20%20%20%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%20%2F%20%20%20%20%20%20%20%5C%0A%20%20%20%20%20%202%20%20%20%20%20%20%20%20%20%203%0A%20%20%20%20%2F%20%20%20%5C%20%20%20%20%20%20%20%2F%20%20%5C%0A%20%20%204%20%20%20%20NULL%20%20NULL%20%20NULL%0A%20%20%2F%20%20%5C%0ANULL%20NULL%0A*%2F%0A%20%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]