Unit 4 Ds
Unit 4 Ds
TREES
INTRODUCTION
Inlineardatastructure datais organized in sequentialorderandinnon-lineardatastructure data
is organized in random order. A tree is a very popular non-linear data structure used in a
wide range of applications. Tree is a non-linear data structure which organizes data in
hierarchical structure and this is a recursive definition.
DEFINITIONOFTREE:
Tree is collection of nodes (or) vertices and their edges (or) links. In tree data structure,
every individual element is called as Node. Node in a tree data structure stores the actual
data of that particular element and link to next element in hierarchical structure.
2. Edge: In a Tree, the connecting link between any two nodes is called as EDGE. In a tree
with 'N' number of nodes there will be a maximum of 'N-1' number of edges.
1
3. Parent Node: In a Tree, the node which is a predecessor of any node is called as PARENT
NODE. In simple words, the node which has a branch from it to any other node is called a
parent node. Parent node can also be defined as "The node which has child / children".
5. Siblings:InaTreedatastructure,nodeswhichbelongtosameParentarecalled as SIBLINGS. In
simple words, the nodes with the same parent are called Sibling nodes.
2
6. LeafNode:InaTreedatastructure,thenodewhichdoesnothaveachildiscalled asLEAF Node. In
simple words, a leaf is a node with no child.In a tree data structure, the leaf nodes are also
called as External Nodes. External node is also a node with no child. In a tree, leaf node is
also called as 'Terminal' node.
7. InternalNodes:InaTreedatastructure,thenodewhichhasatleastonechildiscalled as
INTERNALNode.Insimplewords,aninternalnodeisanodewithatleastonechild.
In a Tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root
node is alsosaid to be Internal Node if the tree has more than one node. Internal nodes are
also called as 'Non-Terminal' nodes.
8. Degree:InaTreedatastructure,thetotalnumberofchildrenofanodeiscalledasDEGREEofthat
Node.Insimple words,the Degreeofa node istotalnumberofchildren it has. The highest
degree of a node among all the nodes in a tree is called as 'Degree of Tree'
DegreeofTreeis:3
9. Level: In a Tree data structure, the root node is said to be at Level 0 and the children of
root node are at Level 1and the children of the nodes which are at Level 1will be at Level 2
3
and so on... In simple words, in a tree each step from top to bottom is called as a Level and
the Level count starts with '0' and incremented by one at each level (Step).
11. Depth: In a Tree data structure, the total number of egdes from root node to a
particular node is called as DEPTH of that Node. In a tree, the total number of edges from
root node toa leafnode inthe longest pathis saidtobe Depth of the tree. Insimple words, the
highest depth of any leaf node in a tree is said to be depth of that tree. In a tree, depth of
the root node is '0'.
12. Path: In a Tree data structure, the sequence of Nodes and Edges from one node to
another nodeiscalled as PATHbetween that two Nodes. Length of a Path is total numberof
nodes in that path. In below example the path A - B - E - J has length 4.
4
13. Sub Tree: In a Tree data structure, each child from a node forms a subtree recursively.
Every child node will form a subtree on its parent node.
TREEREPRESENTATIONS:
Atreedatastructurecanbe representedintwomethods.Thosemethodsare asfollows...
1. ListRepresentation
2. LeftChild-RightSibling Representation
Considerthefollowingtree...
1. ListRepresentation
In this representation, we use two types of nodes one for representing the node with data
called 'data node' and another for representing only references called 'reference node'. We
start with a 'data node' from the root node in the tree. Then it is linkedto an internal node
5
through a 'reference node' which is further linked to any other node directly. This process
repeats for all the nodes in the tree.
TheaboveexampletreecanberepresentedusingListrepresentationasfollows...
2. LeftChild-RightSibling Representation
In this representation, we use a list with one type of node which consists of three fields
namely Data field, Left child reference field and Right sibling reference field. Data fieldstores
the actual value of a node, left reference field stores the address of the left child and right
reference fieldstores the address ofthe right sibling node. Graphical representationof that
node is as follows...
In this representation, every node's data field stores the actual value of that node. If that node has
left a child, then left reference field stores the address of that left child node otherwise stores NULL.
If that node has the right sibling, then right reference field stores the address of right sibling node
otherwise stores NULL.
The above example tree can be represented using Left Child - Right Sibling representation as
follows...
BINARYTREE:
In general, tree nodes can have any number of children. In a binary tree, each node
canhaveatmosttwochildren.A binary tree iseitheremptyorconsistsofa node calledthe root
together with two binary trees called the left subtree and the right subtree.A tree with no
nodes is called as a null tree
Example:
TYPESOFBINARY TREE:
1. StrictlyBinary Tree:
Ina binary tree,every node can have a maximum of twochildren. But instrictly binary tree,
every node should have exactly two children or none. That means every internal node must
have exactly two children. A strictly Binary Tree can be defined as follows...
Strictlybinarytreedatastructureisusedtorepresentmathematicalexpressions.
7
Example
2. CompleteBinaryTree:
Ina binary tree,every node can have a maximum of twochildren. But instrictly binary tree,
every node should have exactly two children or none and in complete binary tree all the
nodes must have exactly two children and at every level of complete binary tree there must
be 2levelnumber of nodes. For example at level 2 there must be 2 2= 4 nodes and at level 3
there must be 23 = 8 nodes.
A binary tree in which every internal node has exactly two children and all leaf nodes are
at same level is called Complete Binary Tree.
CompletebinarytreeisalsocalledasPerfectBinaryTree.
3. ExtendedBinaryTree:
AbinarytreecanbeconvertedintoFullBinarytreebyaddingdummynodestoexisting nodes
wherever required.
Thefullbinarytreeobtainedbyaddingdummynodestoa binarytreeiscalledas
Extended Binary Tree.
8
Inabovefigure,anormalbinarytreeisconvertedintofullbinarytreebyaddingdummy nodes.
4. SkewedBinaryTree:
Ifatreewhichisdominatedbyleftchildnodeorrightchildnode,issaidtobea Skewed Binary Tree.
Inaskewedbinarytree,allnodesexceptonehaveonlyonechildnode.Theremaining node has no
child.
Inaleftskewedtree,mostofthenodeshavetheleftchildwithoutcorrespondingright child.
Inarightskewedtree, mostofthenodeshavetherightchildwithoutcorrespondingleft child.
Propertiesofbinarytrees:
Someoftheimportantpropertiesofabinarytreeareas follows:
1. Ifh=heightofabinary tree, then
a. Maximumnumberofleaves=2h
b. Maximumnumberofnodes=2h +1- 1
2. Ifabinarytreecontainsmnodesatlevell,itcontainsatmost2mnodesatlevell+1.
3. Since abinarytree cancontainatmostonenodeatlevel0(theroot),itcancontainat most 2l
node at level l.
4. Thetotalnumber ofedgesin afullbinarytreewithnnodeisn–
BINARYTREEREPRESENTATIONS:
Abinarytreedatastructureisrepresentedusingtwomethods.Thosemethodsareas follows...
1. Array Representation
2. LinkedListRepresentation
Considerthefollowingbinarytree...
9
1. ArrayRepresentationofBinaryTree
Inarrayrepresentationofa binarytree,we useone-dimensionalarray(1-DArray)to represent a
binary tree.
Considertheaboveexampleofabinarytreeanditisrepresentedas follows...
The above example of the binary tree represented using Linked list representation is shown
as follows...
10