2. DATA STRUCTURES
LINEAR DS NON-LINEAR
STACKS QUEUES LINKED LIST
TREES GRAPHS
3. TREES
DEF:A TREE IS A SET OF NODES AND LINKS
BINARY TREE:A BINARY TREE IS A FINITE SET OF ELEMENTS
THAT IS EITHER EMPTY OR PARTITIONED INTO 3 DISJOINT
SUBSETS .
THE 1ST SUBSET CONTAINS A SINGLE ELEMENT CALLED AS
“ROOT OF THE BINARY TREE”.THE OTHER TWO SUBSETS ARE
THEMSELVES TREES CALLED LEFT AND RIGHT SUBTREES OF
THE ORIGINAL TREE.
EACH ELEMENT OF THE BINARY TREE IS CALLED “NODE” OF
THE BINARY TREE.
5. In the above example A is the root node of
the tree and B is the root of the left sub tree
and C is the root of the right subtree.
Then A is called as father of B and C.
Where B is the left son of A and C is the
right son of A.
6. TREE BASICS
Number of nodes
Height
Root Node
Leaves
Interior nodes
Number of levels
Ancestors of H
Descendants of B
Siblings of E
Right subtree
7.
8. SIBLING
If N is a node In T that has left sub tree S1,
and a right sub tree S2, then N is called as
the parent of the S1& S2
So S1 &S2 are called siblings.
9. LEVEL NUMBER
Every node in a tree is assigned a level
number.
The root is defined at level 0
The left and right child of root node has
level n0.1 and so on…………….
10. DEGREE
The degree of the node is equal to the num
ber of children that a node has.
for eg: the degree of root node A is 2
11. IN DEGREE AND OUT DEGREE
In in-degree of a node is the number of
edges arriving at that node.
for eg: the root node is the only node that
has an in-degree 0.
Similarly the out degree of a node is the
number of edges that leaving the node.
12. Leaf(or)Leaf Node or Terminal node
A “Node” that has no sons is called as “leaf
node”
In the above fig D,E,F&G are the leaf nodes
Where A,B,C are the non leaf nodes
A
B C
LEAF NODES
13. ANCESTOR
A Node ‘n1’ is an ancestor of node ‘n2’ if
‘n1’ is neither father of ‘n2’(or) father of
some ancestor of ‘n2’. n1
n2
n1
B
n2
14. In the above fig 1 ‘n1’ is the father of ‘n2’ so
directly we call ‘n1 as ancestor of ‘n2’
In fig2 there is no direct relation ship b/w n1
and n2 even though n1 Is the father of B
where b is the father of n2 then we call n1
as father of ancestor of ‘n2’ so n1 become
ancestor of n2
15. Descendent
A node ‘n2 is descendent of ‘n1’if ‘n2 is son
of ‘n1’ n1
In the above fig directly n2 is the
n2
son of n1.
n1
In fig2 n2 is the son of ‘b’ where
b
B is the descendent of n1 the n2 is son of
descendent of n1 n2
Then n2 is descendent of n1.
16. Brother Nodes
Two nodes are said to be brothers if they ae
left and right sons of the same father node
A
B C
17. The direction of travelling from roots to
leaves is “down” and the travelling from
leaves to root is up.
Moving from leaves is known as “climbing”
And moving from roots to leaves is also
known as descending.
18. BINARY TREE
A binary tree is a non-linear data structure
which is defined as collection of elements
called nodes.
Every node has left pointer and right pointer
and the data element.
Every binary tree has a root node which is
the top most node in the tree.
If root is equal to null then the tree is empty.
20. In the above figure, node-2 is the left
successor and node-3 is the right succesor
Note that the left sub-tree of the root node
consists of nodes-2,4,5,8,and 9. and right
sub-tree consists of nodes-3,6,7,10,11,12
The leaf nodes are 5,8,9,10,11,12.
21. Properties of BinaryTree
Property1:A tree with ‘n’ nodes has exactly
n-1 edges or branches.
Property2:In a tree every node except the
root has exactly 1 parent
Property3:There is exactly one path
connecting any 2 nodes in a tree.
Property4:the max no of nodes in a binary
tree of height k is 2k+1 -1 (k>=0)
Property5:Btree with n internal nodes has
n+1 external nodes.
22. Strict Binary Tree
If every non leaf node in the binary tree
having non empty left and right sub trees
then it is known as strict binary trees
In a binary tree if every non-leaf node
having both left and right sons then it is
called as strict binary tree.
24. In the above fig1 is a strict binary tree where
as fig2 is not because the non leaf node D
having only leftson ‘F’ it doesnot have right
son.
So in a strict binary tree with n leaves totally
we have (2n-1) nodes.
Ie from the above fig we have 4 leaf nodes
the n we have (2*4-1)=7
25. Level of the node:
The level of the node in a binary tree is
defined as follows.
The root of the binary tree always having
level ’0’ and the level of any other node is
one more that its father node.
Level of a node=1+its father node level
31. Suppose T is a Binary tree that is complete.
there is an efficient way of maintaining T
in memory called Sequential representation
of T.
The representation uses only a single linear
array tree as follows.
a)The root R of tree T is stored in Tree[i]
b)If a node N occupies Tree[K],then the left
child is stored in Tree[2k+1] and right child
is stored in Tree[2k+2].
32.
Complete trees in arrays:
Storage of complete
Trees
0
1 2
3 4 5 6
7 8
…
0 1 2 3 4 5 6 7 8
k=3 2k+1, 2k+2
CIS 068
33. Sequential [1]
[2]
A
[3] B
Representation [4]
[5] C
(1) waste space
A [1] A [6]
(2) insertion/deletion
[2] B [7] D
problem
[3] -- [8]
B [4] [9] E
[5] C
A
[6] -- F
[7] -- H
C
[8]
[9] -- B I
C
D . D
[16] --
.
E D E F G
E
H I