Adobe Scan 29 Aug 2023
Adobe Scan 29 Aug 2023
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. . .
Aller learning the introduction to a tree in data stru ctures, yo u will sec wh y you
need 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 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.
• 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
•
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.
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
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
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
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
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:
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
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...
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
tN d \
A
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)