Unit 3a
Unit 3a
Tree:
A tree is a finite set of one or more nodes such that, there is a specially designated
node called root.
The remaining nodes are partitioned into n>=0 disjoint sets T1, T2,..Tn,
where each of these set is a tree T1,…Tn are called the subtrees of the root.
Branch: Branch is the link between the parent and its child.
Degree:
The number of sub trees of a node is called the degree of the node.
Hence nodes that have degree zero are called leaf or terminal nodes.
Degree of tree: Degree of the tree is the maximum of the degree of the nodes in the tree.
Ancestors: Ancestors of a node are all the nodes along the path from root to that node.
Hence root is ancestor of all the nodes in the tree.
Level:s Level of a node is defined by letting root at level one. If a node is at level L,
then its children are at level L + 1.
Height or depth: The height or depth of a tree is defined to be the maximum level of
any node in the tree.
Climbing: The process of traversing the tree from the leaf to the root is called climbing
the tree.
Descending: The process of traversing the tree from the root to the leaf is called
descending the tree.
BINARY TREE
Binary tree
A binary tree is a finite set of nodes that either is empty or consists of a root
And two disjoint binary trees called the left subtree and right subtree.
Binary tree has nodes each of which has no more than two child nodes.
Left child: The node present to the left of the parent node is called the left child.
Right child: The node present to the right of the parent node is called the right child.
Skewed Binary tree:
If the new nodes in the tree are added only to one side of the binary tree then it is
a skewed binary tree.
If the binary tree has each node consisting of either two nodes or no nodes at all,
then it is called a strictly binary tree.
If all the nodes of a binary tree consist of two nodes each and the nodes at the last
level does not consist any nodes, then that type of binary tree is called a complete
binary tree.
It can be observed that the maximum number of nodes on level i of a binary tree
is 2i - 1, where i ≥ 1. The maximum number of nodes in a binary tree of depth k is 2k – 1,
where k ≥ 1.
REPRESENTATION OF BINARY TREES
There are two ways in which a binary tree can be represented. They are
Rules
Parent[i]=[i/2]
Where i=1 there is no parent.
Lchild(i)=2*i
If 2*i>n then I has no left child.
Rchild(i)=2*i+1
If 2*i+1>n then I has no left child.
For example:
This is because the elements are stored from position one leaving the position 0
vacant.
But generally an array of bigger size is declared so that later new nodes can be
added to the existing tree.
The following binary tree can be represented using arrays as shown.
Array representation
Hence the following formulae can be used to identify the parent, left child
and right child of a particular node.
Parent( i ) = i / 2, if i ≠ 1. If i = 1 then i is the root node and root does not has parent.
The empty positions in the tree where no node is connected are represented in the
array using -1, indicating absence of a node.
LINKED REPRESENTATION OF BINARY TREES
In linked representation of binary trees pointers are used to connect the various
nodes of the tree.
Hence each node of the binary tree consists of three parts namely
Info:- stores the data,
Left :- stores the address of the left child
Right:-stores the address of the right child
LC Data RC
The pointers storing NULL value indicates that there is no node attached to it.
Traversing through this type of representation is very easy.
The left child of a particular node can be accessed by following the left link of
that node and the right child of a particular node can be accessed by following the
right link of that node.
- *
N A N N B N N C N N D N