Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit-4 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

BINARY TREES: A binary tree is a finite set of elements that are either empty or is

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:

A binary tree can be represented in two ways. They are,


 Sequential representation (using arrays)
 Linked representation (using pointers)

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

* To represent a tree of height n an array of size 2n is required.


* Sequential representaion is useful to represent full binarytree or complete binary tree. If
the tree height is high and the no. Of nodes are less then sequential representation is not
preferable.

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.

Node can be represented as,

struct tnode
{
int value;
struct node *left, *right;
}
Ex:

Operations o Binary Trees:

The various operations that can be performed on binary trees are:


 Insertion : the process of inserting a node into the binary tree.
 Deletion : The process of deleting a node from Binary Tree.
 Traversal : The process of visiting each and every node once.
Binary tree traversals: Tree Traversal refers to the process of visiting (examining and/or
updating) each node in a tree, exactly once, in a systematic way. Such traversals are
classified by the order in which the nodes are visited. A Binary tree can be traversed in
three ways. They are,

 Inroder traversal
 Preorder Traversal
 Postorder Traversal
Inorder Traversal:

The process of inorder traversla is,


1. Traverse the left subtree in inorder
2. visit the root node
3. Traverse the right subtree in inorder

Ex:

Inorder traversal: B D A E C F

Algorithm: inorder (root) // root is the root node of the tree to be traversed
begin

if(root != NULL) then


{
inorder( root-> left);
print root->value;
inorder(root->right);
}
end.

Preorder Traversal: The process of preorder traversla is,


1. Visit the root node
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder

Ex:

preorder traversal: A B D C E F

Algorithm: preorder (root) // root is the root node of the tree to be traversed
begin

if(root != NULL) then


{
print root->value;
preorder( root-> left);
preorder(root->right);
}
end.

Postorder Traversal: The process of postorder traversla is,


1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root node

Ex:

postorder traversal: D B E F C A
Algorithm: postorder (root) // root is the root node of the tree to be traversed
begin

if(root != NULL) then


{
postorder( root-> left);
postorder(root->right);
print root->value;
}
end.

Threaded Binary Tree:

 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.

You might also like