C
C
A is a parent of B, C, D,
B is called a child of A.
on the other hand, B is a parent of E, F, K
Each node can have arbitrary number of children. Nodes with no children are
called leaves, or external nodes. In the above picture, C, E, F, L, G are leaves.
Nodes, which are not leaves, are called internalnodes. Internal nodes have at
least one child.
Nodes with the same parent are called siblings. In the picture, B, C, D are called
siblings. The depth of a node is the number of edges from the root to the node.
The depth of K is 2. The height of a node is the number of edges from the
node to the deepest leaf. The height of B is 2. The height of a tree is a height of
a root.
Applications of Trees
Trees and their variants are an extremely useful data structure with lots
of practical applications.
Array Representation
One of the ways to represent a tree using an array is to store the nodes level-by-
level, starting
from the level 0 where the root is present. Such a representation requires
sequential
numbering of the nodes, starting with the nodes on level 0, then those on level 1,
and so on.
Advantages The various merits of representing binary trees using arrays are as
follows:
1. Any node can be accessed from any other node by calculating the index.
2. Here, the data is stored without any pointers to its successor or predecessor.
3. In the programming languages, where dynamic memory allocation is not
possible
(such as BASIC, fortran ), array representation is the only means to store a tree.
Disadvantages The various demerits when representing binary trees using
arrays are
as follows:
1. Other than full binary trees, majority of the array entries may be empty.
2. It allows only static representation. The array size cannot be changed during
the execution.
3. Inserting a new node to it or deleting a node from it is inefficient with this
representation,
because it requires considerable data movement up and down the array, which
demand
excessive amount of processing time.
Here, 0 (zero) stored at Lchild or Rchild fields represents that the respective
child is not present. Let us consider one more example as in Fig. 7.29.
In this node structure, Lchild and Rchild are the two link fields to store the
addresses of left child and right child of a node; data is the information content
of the node. With this representation, if we know the address of the root node,
then using it, any other node can be accessed.
Each node of a binary tree (as the root of some subtree) has both left and right
subtrees, which can be accessed through pointers.
Advantages The merits of representing binary trees through liked
representations are
as follows:
or
1. One can search for the largest data in the deleted node’s left subtree and replace the
deleted node with it.
2. One can search for the smallest data from the deleted node’s right sub tree and replace
the deleted node with it.