Unit-4 1
Unit-4 1
Unit-4 1
partitioned into three disjoint subsets. The first subset contains a single element called
the root of the tree. The other two subsets are themselves binary trees called the left
and right subtrees of the original tree. A left or the right subtree can be empty.
Each element of a binary tree is called a node of the tree. The following figure shows a
binary tree with 10 nodes where 8 is the root.
One node in the tree is designated as the root. Each tree has exactly one
path between the root and each of the other nodes. If there is more than one
path between the root and some node, or no path at all, we have a graph.
If A is the root of a binary tree and B is the root of its left or right subtrees, then A is
said to be the parent of B and B is said to be the left child of A.
A node that has no child node is called the leaf.
Node n1 is the ancestor of node n2 if n1 is either the parent of n2 or the parent of
some ancestor of n2. In such a case n2 is a descendant of n1.
Two nodes are siblings if they are the childs of the same parent.
Level: The level of a node in a binary tree is defined as follows: The root of the
tree has level 0, and the level of any other node in the tree is one more than the
level of its father.
Depth: The depth of a binary tree is the maximum level of any leaf in the tree.
Height: The no. Of levels in a binary tree
A path in a tree is a list of distinct vertices in which successive vertices are
connected by edges in the tree.
Representation of Binary tree:
Sequentail representation:
A one-dimensional array can be used to represent a binary tree. A binary tree can
be represented using array as,
store the root node at position '1'.
all the remaining nodes can be stored in such a way that, if a parent node is at
position 'i' then the left child of that node can be stored at (2*i)th position and right
child of that node can be stored at (2*i + 1) th position.
Ex:
1.
Index 1 2 3 4 5 6 7
value A B C D E F
Linked Representation:
In linked representation, binary tree can be represented as set of nodes. Each node
consists of three parts.. one is the value part and two are the address parts. Value part
consists the value of the node and one address part consists the adddress of the left child
and the other address part consists the address of the right child. If no left/right child exists
then the coreesponding adree part consists the value NULL.
struct tnode
{
int value;
struct node *left, *right;
}
Ex:
Inroder traversal
Preorder Traversal
Postorder Traversal
Inorder Traversal:
Ex:
Inorder traversal: B D A E C F
Algorithm: inorder (root) // root is the root node of the tree to be traversed
begin
Ex:
preorder traversal: A B D C E F
Algorithm: preorder (root) // root is the root node of the tree to be traversed
begin
Ex:
postorder traversal: D B E F C A
Algorithm: postorder (root) // root is the root node of the tree to be traversed
begin
0In the linked representation of a binary tree, additional space is required to store the
two links of each node.
For leaf nodes, these fields always have NULL values as there are no left or right sub-
trees present.
To remove this drawback of memory wastage, the concept of threaded binary tree was
developed. In this type of tree, the empty links are replaced by pointers, called
threads which point to some other node of the tree.
If the left child of a node of a tree is NULLl (or empty) it will be replaced by a thread
(i.e. a pointer) to that node which appears just before that node, when the tree is
traversed in inorder.
Similarly, if a right child of the node is NULLl (or empty) it will be replaced by a thread
(i.e. a pointer) to that node which appears just after that node, when the tree is
traversed in inorder.
Such threads are called inorder threads.
We also have preorder and postorder threads.