0% found this document useful (0 votes)
13 views17 pages

Adobe Scan 29 Aug 2023

Uploaded by

godlikegamer4you
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

Adobe Scan 29 Aug 2023

Uploaded by

godlikegamer4you
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UN IT-5

TREES
'T'he tree: is a non linear hierarchical data structure and comprises a collection oi
entities known as nodes. It connects each node in the tree data structure using
"edges'', both directed and undirected.
11 I 1. ·e d·ita str•lll,. 1ure• I he bluc.:-colon.:d. c1rclc d n"'t
.
I he imaei.,: t.:·low
rqm.:sent~ t ,c r i.: '
. 1 I , bhck lines conncL: "
~l
· • ig c·,ch
. IH>c.ll' \\ 1I h .tlllll 11cr arc
thl' nodes of the tree anu t 1t.: '
called edges. . .

You wi ll understand the parts o f, t '·ccs. ucttc1


L.
' • n ti,.t.: Lcrminologrl!s ~t:ct 1on.
• 1

Aller learning the introduction to a tree in data stru ctures, yo u will sec wh y you
need a tree in data structures.

The Necessity for a Tree in Data Structures

Other data structures li ke arrays, linked-li st, stacks, and queues arc linear data
structures, and all these da ta structures store data in sequential order. Time
complex ity increases with increas ing data size to perform operation s like insertion
and deletion on these linear data structures. But it is not acceptable for today's
world of computation.

The non-Ii near structu re of trees enhances the data storing, d a la accessi ng, an d
manipulation processes by employing advanced control methods traversal th ro ugh
it. You will learn about tree traversa l in the upcoming section

Tree Tenninologies

• In a tree data structure, the root is the first node of the tree. The root node is
the initial node of the tree in data structures.
• In the tree data structure, there must be only one root node.
r~ Root Node

Ed ge

• In a tree in dat a stru ctu res, the con nec ting


link of any two nodes is called the
edg e of the tree dat a structure.
• In the tree dat a stru ctu re, N nu1nber of
nod es con nec ting wit h N -1 num ber
of edg es.
Edge

(A' ' '


0
Parent

In the tree in data structures, the node that is the predecessor of any node is known
as a parent node, or a node with a branch from itself to any other successive node
is called the parent node.

Here A, B and Care


parent nodes
• The node, a descendant of any node, is known as child nodes in data
structures.

• In a tree, any number of parent nodes can have any number of child nodes.
• In a tree, every node except the root node is a child node.
I
I

• Here B and care children of A


• Here o and E are children of B
• Here E and F are children of C


Siblings

In trees 1·n the data structure, no des that belong to the same p arent are called
siblings.
I
I

\I
I
\
11
\
11
\
I
• Here B and Care sib lings
• Here D and E are sib lings
~ ere F and Gare siblings

0
Leaf

• Trees in the data structure, the node with no child, is known as a leaf node.
• In trees, leaf nodes are also called external nodes or termina l nodes.

Here D, E, F and G are leaf nodes.


• ~~~::.in the data structure have at least one child node known as internal

• In tree~, nodes other than leaf nodes are internal nodes.


• Sometidmes root nodes are also called internal nodes if the tree has more th
one no e. an

Here A, Band Care internal nodes.

Degree

• In the tree data structure, the total number of children of a node is called the
degree of the node.
• The highest degree of the node among all the nodes in a tree is called the
Degree of Tree.
I
• Here degree of A, Band C is 2.
• Here degree of D, E, F an d G is 0 .

Level

In tree data structures, the root node is said to be at level 0, and the root node's
children are at level 1, and the children of that node at level 1 will be level 2 , and
so on.

- Level2
• In a tree data structure th
particular node in the 1' e number_ of edges from the leaf node to the
• In the tre . ongest path ts known as the height of that node.
1:
Th e, ~he height of the root node is called "Height of Tree" I

• e tree height of all leaf nodes is 0. . '

I
r ,Height is 2

f \Height is 1

Height is 0
r~@

Depth
• In a tree, many edges from the root node to the particular node are called the
depth of the tree.
• In the tree, the total number of edges from the root node to the leaf node in
the longest path is known as "Depth of Tree".
• In the tree data structures, the depth of the root node is O.
f , oepth is O

f , oepth is 1

Depth is 2
r

Path
• In the tree in data structures, the sequence of nodes and edges from one node
to another node is called the path between those two nodes.
• The length of a path is the total number of nodes in a path.zx

Here the path between A and D is


A-8-D
~uhf t l'l'

111 th1..·
,,ml 111 data structures eac
cry'I.' child
,., t1\. in the lrcc ;. h_ child . from a node shapes a sub-tree recursively
111 fo, 111 a sub-tree on its parent node.

Subtree
,~ Subtree

BINARY TREES:
Binary Tree:.!'! tree who~e elements have at most 2 childrenjs calleda.binacy tree" Since each
clement in a binary tree can have only 2 c ~, we typically name them.Jhe.left and ,:ight
child. - =--- -
Binary Tree Representation: A tree is represented by a pointer to the topmost node of the tree.
ff the tree is empty, then the value of the root is NULL.
A Tree node contains the following parts.

l. Data
2. Pointer to the left child
3. Pointer to the right child

PROPERTIES OF BINARY TREE:


1

~ maximum number of nodes at level'/' of a binary tree is 2 .


Herc level is the number of nodes on the path from the root to the node (including
root and node). Level of the root is 0.
This can be proved by induction.
I or root, I = 0, number of nodes = 2° = I . . . -J I
Assume that the maximum number of nodes on level l is - .
. · B" d has at 111 ost 'J children next level would have twice
S 111ce m mary tree every no e - '
1
nodes · .e. 2 * 2
·
· a bmary
The Maximum number of nodes 111 . 2" - 1·
· I zt 'II ' 15
tree 01.r !,e1g
Here the height of a tree is the maximum number of nodes on the root to leaf path.
Height of a tree with a si ng le node is considered as 1.
This result can be de rived from point 2 above. A tree has maximum nodes if all
levels have maximum nodes. So maximum number of nodes in a binary tree of
height h is I + 2 + 4 + .. + 2h· 1• This is a simp le geometric series with h terms and
sum of this series is 2 11- 1.
In some books, the height of the root is considered as 0. In this convention, the
above formula becomes 21i+t - I

3) A Binary Tree with L leaves has at least I Log2L I+ 1 levels.


A Binary tree has the maximum number of leaves (and a minimum number of
levels) when all levels are fully filled. Let all leaves be at level 1, then below is true
for the number of leaves L.

L <= 2 1· 1 [From Point I]


l = I Log2L I + 1
where is the minimum number of levels.

In a non empty binary tree, if n is th e total number of nodes and e is the total
number of edges, then e = n-1

Every node in a binary tree has exactly one parent w ith the exception of root node.
So if n is the total
number of nodes then n-1 nodes h ave exactly one parent. There is only one edge
between any child and its
nt. So the total numbe r o f edges is n- 1.

~.;.,u,,__ ... i a full bina tree if every node has O or 1...c.hild.ren. The following
sofa full binary tree. We can also say a full binary tree is a binary
nodes except leaf nodes have two children.
A full Binary tree is a specialty e of bin . . .
node/internal node has ei·th p a?' li ce in whi ch cvl:ry parent
binary tree. er two or no child ren. Jt 1·5 a Iso l<nown m; a pror,c.: r

18
I \
15 30
I \ I \
40 50 100 40

18
I \
15 20
I \
40 50
I \
30 50

18
I \
40 30
I \
100 40

Complete Binary Tree:-

A Binary Tree is a Complete Binary Tr.e..eJi.aJJ the levels are completeJy filled
g ept possibly the last level and the last level has all keys as left as possible: .

A complete binary tree is just like a full binary tree, but with two major differences:

• Every level must be completely filled


• All the leaf elements must lean towards the left.
• The last leaf element might not have a right sibling i.e. a complete binaf)'
tree doesn't have to be a full binary tree.

The following are examples of Complete Binary Trees.

18
I \
15 30
I
I \ I \
40 50 JOO 40

I8
I \
15 30
I \ I \
40 50 100 40
I \ I
8 7 9

BINARY SEARCH TREE


Binary Search Tree is a node-based binary tree data structure which has the
following properties:
• .,)fhe left subtree of a node contains only nodes with keys lesser than the
_ / node's key.
• , -The right subtree of a node contains only nodes with keys greater than the
( node's key.
• The left and right subtree each must also be a binary search tree.

ON OF TREES:
.
A bin ary tre e da ta str uct u I.e Is t d .
re pre s h d
en e usi ng two methods • Th ose met o s are
as fol low s...

1. Ar ra y Re.pr ese nt afion


2 L. n
. rnked Li st Representatio

Co nsi de r the fol low ing b.rnary tree ...

ary Tre e
1. Ar ray Re pre sen tat ion of Bin
Ar ray )
ent ati on of a bin ary tre e, we use on e-d im ens ion al array ( 1-D
In arr ay rep res
to rep res ent a bin ary tree. lows ...
abo ve exa mp le of a bin ary tree and it is rep res ent ed as fol
Co nsi de r the

/B/CIDI FJG/HII IJJ- J- J- I Kl - 1- 1- 1- 1 1-1- 1- 0I


lA
___)

of Bin ary Tre e


2. Lin ked Lis t Re pre sen tat ion
ry
lis t to rep res ent a bin ary tree. In a do ub le linked list, eve
We use a do ub le lin ked res s, sec on d for
con sis ts of thr ee fie lds . Fir st field for sto rin g left child add
no de
sto rin g rig ht chi ld add res s.
sto rin g act ual dat a and third for ure ...
lin ked list rep res ent ati on , a no de has the fol low ing str uct
In this
Lef t Child Right Child
Dat a Add ress
Add tess

ed list repre sent tion


:he aho, c C'\.am plc of the bi nai) tree rcrrc scntc d using Link
1s sho" n as folio ,, s ..

tN d \
A

TRA VER SAL OF BINA RY TRE E


s, de) ,,hi("h ha,e
Unli ke linea r data struc tures (Array. l inked List, Queue:-., St,tl'.k
in difforent wU)S .
only one logical way to trave rse them , trees can be trave rsed
Follo wing are the gene rally used ways for trave rsing trc~s
.
b
0
Depth First Traversa ls:
(a) l norder (Left, Root, Right) : 4 2 5 l 3
(b) Preorder (Root, Left, Ri ght) : 1 2 4 5 3
( c) Postor~er (Left, R ight, Root) : 4 5 2 3 l
Breadth-F irst or Leve l Order Traversal: 1 2 3 4 5
Please see this post for Breadth-First Traversa l.

I norder Traversal ():

Algorithm Inorder(tree)
l. Traverse the left subtree, i. e., call Inorder(l eft-subtree)
2. Visit the root.
3. Traverse the ri ght subtree, i. e., call Inorder(right- subtree)

Uses of Inorder
In the case of bin ary search trees (BST), In order traversal gives nodes in non-
decreasing order. To get nodes of BST in non-increas ing order, a variation of
Inorder traversal where Inorder traversal s reversed can be used.
Example: In order traversal for the above-g iven fi gure is 4 2 5 1 3.

Preorder Traversal():

Algorithm Preorder(tree)
I. Visit the root.
2. Traverse the left subtree, i.e. , call Preorder(left- subtree)
3. Traverse the right subtree, i.e. , call Preorder(ri ght-subtree)
Example: Preorder traversal for the above-given fi gure is I 2 4 5 3.

rder(tree)
left subtree, i.e., call Postorder(left- subtree)
right subtree, i.e., call Postorder(ri ght- subtree)

You might also like