SlideShare a Scribd company logo
Bachelor of Information Technology
Chapter(7)
Tree
,
./ Tree
n Concept and Definitions,Basic Operations inBinary Tree,
Tree Height, Level and Depth
OBinary Search Tree, Insertion, Deletion, Traversals, Search
inBST
2
o A tree is a non-linear data structure in which items are
arranged in a sorted sequence.
o It is used to represent hierarchical relationship existing
amongst several data items.
o Recursive definition: A tree is defined as a finite set of
one or more data items (nodes) such that:
There is a special data item called the root ofthe tree.
And its remaining data items are partitioned into a number of
1nutually exclusive (i.e. disjoint) subsets, each ofwhich itself
is a tree (called subtrees)
o Tree data structure grows downwards from top to bottom.
3
Root
i
Key--- p
a
A
,,
, '
, '
'
'  t
,
,
,
'
'
'
'
,. '
Siblings
B
,, E G '
,
'
,+•--subtree
ďż˝ - -- -- .._, - - . - - - - - - - _,
Children
Edge
I
R
C D Height
of the
tree
M
H
Leaf nodes
4
Root
Level 0
B C Level 1
Parent Node E Siblings .. G Level 2
Child Node Level3
Sub-tree Leaf Node
5
1. Each data item in a tree is called a node
2. It is the first data item (or node) in the hierarchical arrangement of data
iten1s. In the above tree, A is the rootiten1
3. The degree ofa node is the number of children of that node
4. The degree of a tree is the maximum degree ofnodes in a given tree
s. Path is the sequence of consecutive edges from source node to
destination node
6. The height of a node is the max path length form that node to a leaf
node
1. The height of a tree is the height ofthe root
8. Depth of a tree is the max level ofany leaf in the tree
9. Level of a node represents the generation of a node. If root node is at
level 0, then its next child node is at level 1, its grandchild is at level 2
and so on.
6
Level 0
B Level 1
Lev-el 2
0 ----------
__.. A is the root node
__.. B is the parent ofE and F
__.. D is the sibling ofB and C
__.. E and F are children ofB
__.. E, F, G, D are external nodes or leaves
__.. A, B, C are internal nodes
__.. Depth ofF is 2
__.. the height of tree is 2
__.. the degree ofnode A is 3
__.. The degree of tree is 3
7
o A binary tree is a finite set of nodes that is either empty or consists of a
root and two disjoint binary trees called the left subtree and the right
subtree.
o Any tree can be transfonned into binary tree.
LI by left child-right sibling representation
o The left subtree and the right subtree are distinguished.
- -
- -
(a) Binary tree
with one node
- -
- - - -
- -
A
- - - -
- - - -
(b) Binary tree (c) Binary tree (d) Binary tree
with two nodes with two nodes with three nodes
8
o Ifevery non-leaf node in a binary tree has nonempty left and right sub­
trees, then such a tree is called a strictly binary tree.
o Or, to put it another way, all ofthe nodes in a strictly binary tree are of
degree zero or two, never degree one.
o A strictly binary treewith N leaves always contains 2N - 1 nodes.
B
0 E C
A Strictly Bln11ry Tree
9
o A complete binary tree is a binary tree in which every level, except possibly the
last, is completely filled, and all nodes are as far left as possible.
o A complete binary tree ofdepth d is called strictly bina1y tree ifall ofwhose leaves
are at level d.
D A complete binary tree has 211 nodes at every depth d and 2t1-1 non leafnodes
A complete Binary TrM of depth 3
10
o An almost con1plete binary tree is a tree where for:
.. a right child, there is always a left child,
.J but for a left child there may not be a right child.
8 C
H J
Fig Almost completebinary treebut not strictly binary tree.
Fig Almost complete binarytree. Since node Ehas a left son but not a right soo.
11
o A binary search tree is a binary tree that is either empty or in
which each node possesses a key that satisfies the following three
conditions:
[J For every node X in the tree, the values of all the keys in its left
subtree are smaller than the key value in X.
rJ Keys in its right subtree are greater than the key value in X.
U The left and right subtrees of the root are again binary search trees.
12
Create the binary search tree using the following data elements.
43, 10,79,90, 12,54, 11,9,50
Step 1
Step S
Step 8
Step 2
_ft
®
Step 3
r.-J��,
ďż˝ ďż˝
Step 6
43
Step 4
-------ďż˝-
79
'
00 e
St.ep 9
Binary search Tree Creatiol'I
s-i-ep 7
43
fe
05
13
INSERTION
o Inserting a node into an empty tree: In this case, the node inserted into the tree
is considered as the root node.
o Inserting a node into a non-empty tree: In this case, we compare the new node
to the root node of the tree.
.J Ifthe value oftbe new node is less than the value ofthe root node, then if the left
subtree is etnpty, the new node is appended as the left leaf of the root node
else we search continuous down the left subtree.
,.... Tf the value of the new node is greater than the value of the root node, then
if the right subtree is empty, the new node is appended as the right leaf of
the root node else we search continuous do,vn the right subtree.
If the value of the new node is equal to the value of the root node, then
print "DUPLICATE ENTRY" and return.
14
lnsert (TREE, lTEM)
1. IF TREE= NULL
Allocatemen101y for TREE
SET TREE ->DATA=JTEM
SET TRl:E -> LEFT TREE -> RIGHT -NULL
ELSE
IF ITE:1 <TREE-> DATA
lnsert(TREE-> LEFT, ITEM)
ELSE
lnscrt(TREE-> RlGHT. fTEM)
fEND OFIFl
[END OFTF]
2. END
15
ITEM>ROOT·> DATA ROOT - 50
-'"=·="' � 0
Item= 95
tT£M>ROOT·>DATA
ROOT =ROOT ·> "'IQHT
I  . 
. .. I
0 eďż˝ e
�� �v
ITl;M>·�OOT -> DATA
ROOT •ROOT·>RIG.HT
STEPl
i 
@e
STcEP3
INSERT IT�MTO THERIGHT
OF6S
50
STEP2
STEP4
16
o 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order,
starting from an empty tree insert according to BST
17
SEARCH
./ Search operation is performed with O(log n) time complexity iJ1 a binary search tree.
./ This operation starts from the root node. Jt is used whenever an element is to be searched.
./ The following algorithm shows the search operation in bina1y search tree:
Read the element from the user .
Compare this element with the value of root node in a tree.
Ifelement and value are matching, display "Node is Found" and Lerminate Lbe function.
If element and value are not matching, check whether an element is smaller or larger than a
node value.
lf an clement is smaller, continue the search operation in left subtree.
Ifan element is larger, continue the search operation in right subtree.
Repeal U1c same process until we found the exact clement.
Ifan element wilh search value is found, display "Element is found" and terminate the
function.
I Ifwe reach to a leafnode and the search value is not match to a leafnode, display "Element is
ool found" and terminate the function
18
.; The element to be searched is 10
., Compare the element with the root node l2, IO < 12, hence you move to the left subtree. No need to analyze
the right-subtree
.; Now compare 10 with node 7, 10 > 7, so move to the right-subtree
.; Then compare 10 with the next node, which is 9, 10 > 9, look iu the right subtree child
.; IO matches with the value in the node, IO = I0, return the value to t]le user
..... Elements co be searched
ďż˝ in the tree .l.O
•10 ·
... I :;c-.,
n1ovP. rl-, rhe
nght: sub-
tit:,P.
.LO <- 1.2 so No need to
.10 > 9 so
move to tho
,1ght S;1..ib tree
ch,lc.1
On comparison
10 matches,
ruturn LhO vuluu
0
19
�&i§�t eg���tY�©� �� [8)�7r
DELETION
1. Locate the node to be deleted and its parent (The
parent of root node is NULL).
2. If leaf node - set NULL to its parent pointer.If
node with one child- redirect the parent pointer
to point to its child pointer.
3. If node with two child- replace node being
deleted by I. Leftmost node of its right subtree II.
OR rightmost node of its left subtree
20
Case 1- Node with zero children: this is the easiest situation, you just need to delete
the node which has 110 fwiher children 011 the right or left.
N00ctocedeleted has0
:hdreo
-
S1mpe)elererre·.oje
ardrerrovetheI•k
1 1. This is the first case ofdeletion
in which you delete a node that
has no children. As you can see
in the diagram that 19, 10 and 5
have no children. But we will
delete 19.
2. Delete the value 19 and remove
the link from the node.
3. View the nevv structure of the
BST vvithout 19
21
Case 2 - Node with one child: once you delete the node, simply connect its child node
with the parent node ofthe deleted value.
A'J:detobedeleleliasl
W010
..
Smoel)rnetierode
anc rep.:er.1·,,ll'me
:M: ,oc,
1. This is the second case ofdeletion in
which you delete a node that has I
child, as you can see in the diagra1n
that 9 has one child.
2. Delete the node 9 and replace it with
its child IO and add a link from 7 to
10
3. View the new structure of the BST
without 9
22
Case 3 Node with two children: this is the most difficult situation, and it works on the
following two rules
• 3a -In Order Predecessor: you need to delete the node with two children and
replace it with the largest value on the left-subtree of the deleted node
I,i:<t,,ďż˝"""'0b1ge,i
e-n<t,,tts,b,,,,'llit
IX,j'l)be�II) llll
0
SmpeDl�letie,M?
:1andr;p;,:e�11,;h10
t. Here you will be deleting the
node 12 that has two children
2. The deletion of the node will
occur based upon the in order
predecessor rule, which means
that the largest elen1ent on the
left subtree of 12 will replace it.
3. Delete the node 12 and replace
it with 10 as it is the largest
value on the left subtree
4. View the new structure of the
BST after deleting 12
23
3b - In Order Successor: you need to delete the node with two children and replace it
with the largest value on the right-subtree of the deleted node
hC!Ce·�s��s:.S:r-em
ri1t11'Q'U,b-trc�ct·"'('n.�1c:o
te�ca�
0
S:npleD�etemeIY.}:le
12ind •eplm,rt11,ch:,
f) Re,Jlt
1. Delete a node 12 that has two
children
2. The deletion of the node will
occur based upon the In Order
Successor rule, which means
that the largest element on the
right subtree of 12 will replace
it
3. Delete the node 12 and replace
it with 19 as it is the largest
value on the right subtree
4. View the new structure of the
BST after deleting 12
24
../ Binary tree traversing is a process of accessing every node of
the tree and exactly once. A tree is defined in a recursive
manner. Binary tree traversal also defined recursively.
../ All nodes are connected via edges (links) we always start from
tl1e root(head) node.
../ There are three ways which we use to traverse a tree
,. In-order Traversal
Pre-order Traversal
Post-order Traversal
../ Generally we traverse a h·ee to search or locate given item or
key in the tree or to print all the values it contains.
25
,,, The preorder traversal of a nonempty binary tree is defined as follows:
Visit the root node
Traverse the left sub-tree in preorder <root><left><right>
,_ Traverse the right sub-tree in preorder
8 C
C
fig Binary tree
The preorder traversal output of the given tree is: AB DH IE CF G
The preorder is also known as depth first order.
26
,,, The in-order traversal of a nonen1pty binary tree is defined as follows:
Traverse the left sub-tree in in-order
Visit the root node
- Traverse the right sub-tree in inorder
fig Binary tree
<left><root><right>
C
C
The in-order traversal output ofthe given tree is lf DI BE AF CG
27
,,, The post-order traversal of a nonempty binary tree is defined as follows:
Traverse the left sub-tree in post-order
Traverse the right sub-tree in post-order
- Visit the root node
<left><right><root>
C
D ÂŁ
fig Binary tree
• • • + � -
The post-order traversal output ofthe given tree is HID E BF G CA
28
T
I
0 p
0
I
t d a
t'
g h I
I m
ntiu.!ic.t I
r I b C 9 h I d a
1·
I
n o P
'/.
I m
•••
lllAATllli l
I k t I b C I mg h I da
'Ii .
•••• • • •••
tl!ltAll)H l
n a P
Jn opktfbclmghl do
t t t I t t t t I •••••• •
IT!itATllH •
The poll ordertrewrnl ol T
T
0
I
0 p
a t d
• •
I
J
ITEAATIO!f I
a b t / ( d 9 h
• •
I . .'/. .
k I m
n 0 p
ITlll.l!lOIU
a & t I k I C dglmhl
'''';!'
• •• •••
n o P
lrnATIOIIl
ob t J kn op/ r dg/ mh I
•••••• •••• ••• •• •
ITERA1IOH4
The preordl!r t1ave1utof T
29
t. Binary Search Tree - Used in many search applications that constantly show and hide
data, such as data. For example, map and set objects in many libraries.
2. Binary Space Partition - Used in almost any 3D video game to detennine which objects
need to be rendered.
3. Binary Tries - Used in almost every high-bandwidth router to store router tables.
4. SyntaxTree - Constructed by compilers and (implicit) calculators to parse expressions.
5. Hash Trees - Used in P2P prograrns and special image signatures that require a hash to
be validated, but the entire file is not available.
6. Heaps - Used to implement efficient priority queues and also used in heap sort.
7. Treap - Randomized data stlucture for Vireless networks and mernory allocation.
8. T-Tree - Although most databases use a form of 8-tree to store data on the drive,
databases that store all (most) data often use T-trees.
9. Huffman Coding Tree (Chip Uni) - Used in compression algorithms, eg. For exarnple,
in .jpeg and .tnp3.GGM Trees file formats - used in cryptographic applications to
generate a tree with pseudo-randorn nurnbers.
30
Ad

More Related Content

Similar to Chapter 7 - Binary Search Tree in the context of DSA.pdf (20)

Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
 
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
data structures Unit 3 notes.docxdata structures Unit 3 notes.docxdata structures Unit 3 notes.docxdata structures Unit 3 notes.docx
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
yatakonakiran2
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
Prof Ansari
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
VijayaLakshmi506
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
JITTAYASHWANTHREDDY
 
Unit iii(dsc++)
Unit iii(dsc++)Unit iii(dsc++)
Unit iii(dsc++)
Durga Devi
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
Guru Nanak Institute Of Tech
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
SIVAKUMARM603675
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
Om Prakash
 
Binary search Tree and avl tree , treee.ppt
Binary search Tree and avl tree , treee.pptBinary search Tree and avl tree , treee.ppt
Binary search Tree and avl tree , treee.ppt
sjain87654321
 
Tree
TreeTree
Tree
Raj Sarode
 
Algorithm and Data Structure - Binary Trees
Algorithm and Data Structure - Binary TreesAlgorithm and Data Structure - Binary Trees
Algorithm and Data Structure - Binary Trees
donotreply20
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2
smruti sarangi
 
Data Structure And Algorithms for Computer Science
Data Structure And Algorithms for Computer ScienceData Structure And Algorithms for Computer Science
Data Structure And Algorithms for Computer Science
ManishShukla712917
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
maamir farooq
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
19EBKCS055KarishmaVe
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
Dabbal Singh Mahara
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
 
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
data structures Unit 3 notes.docxdata structures Unit 3 notes.docxdata structures Unit 3 notes.docxdata structures Unit 3 notes.docx
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
yatakonakiran2
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
Prof Ansari
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
 
Unit iii(dsc++)
Unit iii(dsc++)Unit iii(dsc++)
Unit iii(dsc++)
Durga Devi
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
Om Prakash
 
Binary search Tree and avl tree , treee.ppt
Binary search Tree and avl tree , treee.pptBinary search Tree and avl tree , treee.ppt
Binary search Tree and avl tree , treee.ppt
sjain87654321
 
Algorithm and Data Structure - Binary Trees
Algorithm and Data Structure - Binary TreesAlgorithm and Data Structure - Binary Trees
Algorithm and Data Structure - Binary Trees
donotreply20
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2
smruti sarangi
 
Data Structure And Algorithms for Computer Science
Data Structure And Algorithms for Computer ScienceData Structure And Algorithms for Computer Science
Data Structure And Algorithms for Computer Science
ManishShukla712917
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
maamir farooq
 

More from Dibyesh1 (19)

Chapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptxChapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptx
Dibyesh1
 
This Presentation is about HTML which is second chapter
This Presentation is about HTML which is second chapterThis Presentation is about HTML which is second chapter
This Presentation is about HTML which is second chapter
Dibyesh1
 
Cybersecurity artificial intelligence presentation.pptx
Cybersecurity artificial intelligence presentation.pptxCybersecurity artificial intelligence presentation.pptx
Cybersecurity artificial intelligence presentation.pptx
Dibyesh1
 
Lecture 10 - Export Risk Management (Lecturer) 140908.pptx
Lecture 10 - Export Risk Management (Lecturer) 140908.pptxLecture 10 - Export Risk Management (Lecturer) 140908.pptx
Lecture 10 - Export Risk Management (Lecturer) 140908.pptx
Dibyesh1
 
Lecture 7 - International Recruitment Selection and Repatriation (Lecturer) 1...
Lecture 7 - International Recruitment Selection and Repatriation (Lecturer) 1...Lecture 7 - International Recruitment Selection and Repatriation (Lecturer) 1...
Lecture 7 - International Recruitment Selection and Repatriation (Lecturer) 1...
Dibyesh1
 
SE Chapter 4 - Software Requirements.pptx
SE Chapter 4 - Software  Requirements.pptxSE Chapter 4 - Software  Requirements.pptx
SE Chapter 4 - Software Requirements.pptx
Dibyesh1
 
SE Chapter 6 - Software Reliability.pptx
SE Chapter 6 - Software  Reliability.pptxSE Chapter 6 - Software  Reliability.pptx
SE Chapter 6 - Software Reliability.pptx
Dibyesh1
 
Organisational Structures.ppt
Organisational Structures.pptOrganisational Structures.ppt
Organisational Structures.ppt
Dibyesh1
 
chapter17.pptx
chapter17.pptxchapter17.pptx
chapter17.pptx
Dibyesh1
 
Chapter 3-Time Scheduling a Project.pptx
Chapter 3-Time Scheduling a Project.pptxChapter 3-Time Scheduling a Project.pptx
Chapter 3-Time Scheduling a Project.pptx
Dibyesh1
 
Lecture 12 - Project Management (Lecturer).pptx
Lecture 12 - Project Management (Lecturer).pptxLecture 12 - Project Management (Lecturer).pptx
Lecture 12 - Project Management (Lecturer).pptx
Dibyesh1
 
Week 5 Building Digital Capabilites in Workforce and Organization.pptx
Week 5 Building Digital Capabilites in Workforce and Organization.pptxWeek 5 Building Digital Capabilites in Workforce and Organization.pptx
Week 5 Building Digital Capabilites in Workforce and Organization.pptx
Dibyesh1
 
Week 2 Tutorial - Examining Digital Business Environment.pptx
Week 2 Tutorial - Examining Digital Business Environment.pptxWeek 2 Tutorial - Examining Digital Business Environment.pptx
Week 2 Tutorial - Examining Digital Business Environment.pptx
Dibyesh1
 
4BU017 Lecture Slides - Definition and Scope of Digital Business - 2021-22-1....
4BU017 Lecture Slides - Definition and Scope of Digital Business - 2021-22-1....4BU017 Lecture Slides - Definition and Scope of Digital Business - 2021-22-1....
4BU017 Lecture Slides - Definition and Scope of Digital Business - 2021-22-1....
Dibyesh1
 
Lecture 2 - Balance Sheet (Lecturer).pptx
Lecture 2 - Balance Sheet (Lecturer).pptxLecture 2 - Balance Sheet (Lecturer).pptx
Lecture 2 - Balance Sheet (Lecturer).pptx
Dibyesh1
 
Lecture 4.2- Internet and WWW.pptx
Lecture 4.2- Internet and WWW.pptxLecture 4.2- Internet and WWW.pptx
Lecture 4.2- Internet and WWW.pptx
Dibyesh1
 
Chapter 3 - Software.pptx
Chapter 3 - Software.pptxChapter 3 - Software.pptx
Chapter 3 - Software.pptx
Dibyesh1
 
Lecture 5.1.pptx
Lecture 5.1.pptxLecture 5.1.pptx
Lecture 5.1.pptx
Dibyesh1
 
I Academy Topics of Introduction to business.pptx
I Academy Topics of  Introduction to business.pptxI Academy Topics of  Introduction to business.pptx
I Academy Topics of Introduction to business.pptx
Dibyesh1
 
Chapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptxChapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptx
Dibyesh1
 
This Presentation is about HTML which is second chapter
This Presentation is about HTML which is second chapterThis Presentation is about HTML which is second chapter
This Presentation is about HTML which is second chapter
Dibyesh1
 
Cybersecurity artificial intelligence presentation.pptx
Cybersecurity artificial intelligence presentation.pptxCybersecurity artificial intelligence presentation.pptx
Cybersecurity artificial intelligence presentation.pptx
Dibyesh1
 
Lecture 10 - Export Risk Management (Lecturer) 140908.pptx
Lecture 10 - Export Risk Management (Lecturer) 140908.pptxLecture 10 - Export Risk Management (Lecturer) 140908.pptx
Lecture 10 - Export Risk Management (Lecturer) 140908.pptx
Dibyesh1
 
Lecture 7 - International Recruitment Selection and Repatriation (Lecturer) 1...
Lecture 7 - International Recruitment Selection and Repatriation (Lecturer) 1...Lecture 7 - International Recruitment Selection and Repatriation (Lecturer) 1...
Lecture 7 - International Recruitment Selection and Repatriation (Lecturer) 1...
Dibyesh1
 
SE Chapter 4 - Software Requirements.pptx
SE Chapter 4 - Software  Requirements.pptxSE Chapter 4 - Software  Requirements.pptx
SE Chapter 4 - Software Requirements.pptx
Dibyesh1
 
SE Chapter 6 - Software Reliability.pptx
SE Chapter 6 - Software  Reliability.pptxSE Chapter 6 - Software  Reliability.pptx
SE Chapter 6 - Software Reliability.pptx
Dibyesh1
 
Organisational Structures.ppt
Organisational Structures.pptOrganisational Structures.ppt
Organisational Structures.ppt
Dibyesh1
 
chapter17.pptx
chapter17.pptxchapter17.pptx
chapter17.pptx
Dibyesh1
 
Chapter 3-Time Scheduling a Project.pptx
Chapter 3-Time Scheduling a Project.pptxChapter 3-Time Scheduling a Project.pptx
Chapter 3-Time Scheduling a Project.pptx
Dibyesh1
 
Lecture 12 - Project Management (Lecturer).pptx
Lecture 12 - Project Management (Lecturer).pptxLecture 12 - Project Management (Lecturer).pptx
Lecture 12 - Project Management (Lecturer).pptx
Dibyesh1
 
Week 5 Building Digital Capabilites in Workforce and Organization.pptx
Week 5 Building Digital Capabilites in Workforce and Organization.pptxWeek 5 Building Digital Capabilites in Workforce and Organization.pptx
Week 5 Building Digital Capabilites in Workforce and Organization.pptx
Dibyesh1
 
Week 2 Tutorial - Examining Digital Business Environment.pptx
Week 2 Tutorial - Examining Digital Business Environment.pptxWeek 2 Tutorial - Examining Digital Business Environment.pptx
Week 2 Tutorial - Examining Digital Business Environment.pptx
Dibyesh1
 
4BU017 Lecture Slides - Definition and Scope of Digital Business - 2021-22-1....
4BU017 Lecture Slides - Definition and Scope of Digital Business - 2021-22-1....4BU017 Lecture Slides - Definition and Scope of Digital Business - 2021-22-1....
4BU017 Lecture Slides - Definition and Scope of Digital Business - 2021-22-1....
Dibyesh1
 
Lecture 2 - Balance Sheet (Lecturer).pptx
Lecture 2 - Balance Sheet (Lecturer).pptxLecture 2 - Balance Sheet (Lecturer).pptx
Lecture 2 - Balance Sheet (Lecturer).pptx
Dibyesh1
 
Lecture 4.2- Internet and WWW.pptx
Lecture 4.2- Internet and WWW.pptxLecture 4.2- Internet and WWW.pptx
Lecture 4.2- Internet and WWW.pptx
Dibyesh1
 
Chapter 3 - Software.pptx
Chapter 3 - Software.pptxChapter 3 - Software.pptx
Chapter 3 - Software.pptx
Dibyesh1
 
Lecture 5.1.pptx
Lecture 5.1.pptxLecture 5.1.pptx
Lecture 5.1.pptx
Dibyesh1
 
I Academy Topics of Introduction to business.pptx
I Academy Topics of  Introduction to business.pptxI Academy Topics of  Introduction to business.pptx
I Academy Topics of Introduction to business.pptx
Dibyesh1
 
Ad

Recently uploaded (20)

"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxbMain cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
SunilSingh610661
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxbMain cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
SunilSingh610661
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Ad

Chapter 7 - Binary Search Tree in the context of DSA.pdf

  • 1. Bachelor of Information Technology Chapter(7) Tree ,
  • 2. ./ Tree n Concept and Definitions,Basic Operations inBinary Tree, Tree Height, Level and Depth OBinary Search Tree, Insertion, Deletion, Traversals, Search inBST 2
  • 3. o A tree is a non-linear data structure in which items are arranged in a sorted sequence. o It is used to represent hierarchical relationship existing amongst several data items. o Recursive definition: A tree is defined as a finite set of one or more data items (nodes) such that: There is a special data item called the root ofthe tree. And its remaining data items are partitioned into a number of 1nutually exclusive (i.e. disjoint) subsets, each ofwhich itself is a tree (called subtrees) o Tree data structure grows downwards from top to bottom. 3
  • 4. Root i Key--- p a A ,, , ' , ' ' ' t , , , ' ' ' ' ,. ' Siblings B ,, E G ' , ' ,+•--subtree ďż˝ - -- -- .._, - - . - - - - - - - _, Children Edge I R C D Height of the tree M H Leaf nodes 4
  • 5. Root Level 0 B C Level 1 Parent Node E Siblings .. G Level 2 Child Node Level3 Sub-tree Leaf Node 5
  • 6. 1. Each data item in a tree is called a node 2. It is the first data item (or node) in the hierarchical arrangement of data iten1s. In the above tree, A is the rootiten1 3. The degree ofa node is the number of children of that node 4. The degree of a tree is the maximum degree ofnodes in a given tree s. Path is the sequence of consecutive edges from source node to destination node 6. The height of a node is the max path length form that node to a leaf node 1. The height of a tree is the height ofthe root 8. Depth of a tree is the max level ofany leaf in the tree 9. Level of a node represents the generation of a node. If root node is at level 0, then its next child node is at level 1, its grandchild is at level 2 and so on. 6
  • 7. Level 0 B Level 1 Lev-el 2 0 ---------- __.. A is the root node __.. B is the parent ofE and F __.. D is the sibling ofB and C __.. E and F are children ofB __.. E, F, G, D are external nodes or leaves __.. A, B, C are internal nodes __.. Depth ofF is 2 __.. the height of tree is 2 __.. the degree ofnode A is 3 __.. The degree of tree is 3 7
  • 8. o A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree. o Any tree can be transfonned into binary tree. LI by left child-right sibling representation o The left subtree and the right subtree are distinguished. - - - - (a) Binary tree with one node - - - - - - - - A - - - - - - - - (b) Binary tree (c) Binary tree (d) Binary tree with two nodes with two nodes with three nodes 8
  • 9. o Ifevery non-leaf node in a binary tree has nonempty left and right sub­ trees, then such a tree is called a strictly binary tree. o Or, to put it another way, all ofthe nodes in a strictly binary tree are of degree zero or two, never degree one. o A strictly binary treewith N leaves always contains 2N - 1 nodes. B 0 E C A Strictly Bln11ry Tree 9
  • 10. o A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. o A complete binary tree ofdepth d is called strictly bina1y tree ifall ofwhose leaves are at level d. D A complete binary tree has 211 nodes at every depth d and 2t1-1 non leafnodes A complete Binary TrM of depth 3 10
  • 11. o An almost con1plete binary tree is a tree where for: .. a right child, there is always a left child, .J but for a left child there may not be a right child. 8 C H J Fig Almost completebinary treebut not strictly binary tree. Fig Almost complete binarytree. Since node Ehas a left son but not a right soo. 11
  • 12. o A binary search tree is a binary tree that is either empty or in which each node possesses a key that satisfies the following three conditions: [J For every node X in the tree, the values of all the keys in its left subtree are smaller than the key value in X. rJ Keys in its right subtree are greater than the key value in X. U The left and right subtrees of the root are again binary search trees. 12
  • 13. Create the binary search tree using the following data elements. 43, 10,79,90, 12,54, 11,9,50 Step 1 Step S Step 8 Step 2 _ft ® Step 3 r.-J��, ďż˝ ďż˝ Step 6 43 Step 4 -------ďż˝- 79 ' 00 e St.ep 9 Binary search Tree Creatiol'I s-i-ep 7 43 fe 05 13
  • 14. INSERTION o Inserting a node into an empty tree: In this case, the node inserted into the tree is considered as the root node. o Inserting a node into a non-empty tree: In this case, we compare the new node to the root node of the tree. .J Ifthe value oftbe new node is less than the value ofthe root node, then if the left subtree is etnpty, the new node is appended as the left leaf of the root node else we search continuous down the left subtree. ,.... Tf the value of the new node is greater than the value of the root node, then if the right subtree is empty, the new node is appended as the right leaf of the root node else we search continuous do,vn the right subtree. If the value of the new node is equal to the value of the root node, then print "DUPLICATE ENTRY" and return. 14
  • 15. lnsert (TREE, lTEM) 1. IF TREE= NULL Allocatemen101y for TREE SET TREE ->DATA=JTEM SET TRl:E -> LEFT TREE -> RIGHT -NULL ELSE IF ITE:1 <TREE-> DATA lnsert(TREE-> LEFT, ITEM) ELSE lnscrt(TREE-> RlGHT. fTEM) fEND OFIFl [END OFTF] 2. END 15
  • 16. ITEM>ROOT·> DATA ROOT - 50 -'"=·="' ďż˝ 0 Item= 95 tTÂŁM>ROOT·>DATA ROOT =ROOT ·> "'IQHT I . . .. I 0 eďż˝ e �� ďż˝v ITl;M>·�OOT -> DATA ROOT •ROOT·>RIG.HT STEPl i @e STcEP3 INSERT ITďż˝MTO THERIGHT OF6S 50 STEP2 STEP4 16
  • 17. o 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting from an empty tree insert according to BST 17
  • 18. SEARCH ./ Search operation is performed with O(log n) time complexity iJ1 a binary search tree. ./ This operation starts from the root node. Jt is used whenever an element is to be searched. ./ The following algorithm shows the search operation in bina1y search tree: Read the element from the user . Compare this element with the value of root node in a tree. Ifelement and value are matching, display "Node is Found" and Lerminate Lbe function. If element and value are not matching, check whether an element is smaller or larger than a node value. lf an clement is smaller, continue the search operation in left subtree. Ifan element is larger, continue the search operation in right subtree. Repeal U1c same process until we found the exact clement. Ifan element wilh search value is found, display "Element is found" and terminate the function. I Ifwe reach to a leafnode and the search value is not match to a leafnode, display "Element is ool found" and terminate the function 18
  • 19. .; The element to be searched is 10 ., Compare the element with the root node l2, IO < 12, hence you move to the left subtree. No need to analyze the right-subtree .; Now compare 10 with node 7, 10 > 7, so move to the right-subtree .; Then compare 10 with the next node, which is 9, 10 > 9, look iu the right subtree child .; IO matches with the value in the node, IO = I0, return the value to t]le user ..... Elements co be searched ďż˝ in the tree .l.O •10 · ... I :;c-., n1ovP. rl-, rhe nght: sub- tit:,P. .LO <- 1.2 so No need to .10 > 9 so move to tho ,1ght S;1..ib tree ch,lc.1 On comparison 10 matches, ruturn LhO vuluu 0 19
  • 20. ďż˝&i§�t eg���tY�©� �� [8)ďż˝7r DELETION 1. Locate the node to be deleted and its parent (The parent of root node is NULL). 2. If leaf node - set NULL to its parent pointer.If node with one child- redirect the parent pointer to point to its child pointer. 3. If node with two child- replace node being deleted by I. Leftmost node of its right subtree II. OR rightmost node of its left subtree 20
  • 21. Case 1- Node with zero children: this is the easiest situation, you just need to delete the node which has 110 fwiher children 011 the right or left. N00ctocedeleted has0 :hdreo - S1mpe)elererre·.oje ardrerrovetheI•k 1 1. This is the first case ofdeletion in which you delete a node that has no children. As you can see in the diagram that 19, 10 and 5 have no children. But we will delete 19. 2. Delete the value 19 and remove the link from the node. 3. View the nevv structure of the BST vvithout 19 21
  • 22. Case 2 - Node with one child: once you delete the node, simply connect its child node with the parent node ofthe deleted value. A'J:detobedeleleliasl W010 .. Smoel)rnetierode anc rep.:er.1·,,ll'me :M: ,oc, 1. This is the second case ofdeletion in which you delete a node that has I child, as you can see in the diagra1n that 9 has one child. 2. Delete the node 9 and replace it with its child IO and add a link from 7 to 10 3. View the new structure of the BST without 9 22
  • 23. Case 3 Node with two children: this is the most difficult situation, and it works on the following two rules • 3a -In Order Predecessor: you need to delete the node with two children and replace it with the largest value on the left-subtree of the deleted node I,i:<t,,ďż˝"""'0b1ge,i e-n<t,,tts,b,,,,'llit IX,j'l)beďż˝II) llll 0 SmpeDlďż˝letie,M? :1andr;p;,:eďż˝11,;h10 t. Here you will be deleting the node 12 that has two children 2. The deletion of the node will occur based upon the in order predecessor rule, which means that the largest elen1ent on the left subtree of 12 will replace it. 3. Delete the node 12 and replace it with 10 as it is the largest value on the left subtree 4. View the new structure of the BST after deleting 12 23
  • 24. 3b - In Order Successor: you need to delete the node with two children and replace it with the largest value on the right-subtree of the deleted node hC!Ce·�s��s:.S:r-em ri1t11'Q'U,b-trcďż˝ct·"'('n.ďż˝1c:o teďż˝caďż˝ 0 S:npleDďż˝etemeIY.}:le 12ind •eplm,rt11,ch:, f) Re,Jlt 1. Delete a node 12 that has two children 2. The deletion of the node will occur based upon the In Order Successor rule, which means that the largest element on the right subtree of 12 will replace it 3. Delete the node 12 and replace it with 19 as it is the largest value on the right subtree 4. View the new structure of the BST after deleting 12 24
  • 25. ../ Binary tree traversing is a process of accessing every node of the tree and exactly once. A tree is defined in a recursive manner. Binary tree traversal also defined recursively. ../ All nodes are connected via edges (links) we always start from tl1e root(head) node. ../ There are three ways which we use to traverse a tree ,. In-order Traversal Pre-order Traversal Post-order Traversal ../ Generally we traverse a h·ee to search or locate given item or key in the tree or to print all the values it contains. 25
  • 26. ,,, The preorder traversal of a nonempty binary tree is defined as follows: Visit the root node Traverse the left sub-tree in preorder <root><left><right> ,_ Traverse the right sub-tree in preorder 8 C C fig Binary tree The preorder traversal output of the given tree is: AB DH IE CF G The preorder is also known as depth first order. 26
  • 27. ,,, The in-order traversal of a nonen1pty binary tree is defined as follows: Traverse the left sub-tree in in-order Visit the root node - Traverse the right sub-tree in inorder fig Binary tree <left><root><right> C C The in-order traversal output ofthe given tree is lf DI BE AF CG 27
  • 28. ,,, The post-order traversal of a nonempty binary tree is defined as follows: Traverse the left sub-tree in post-order Traverse the right sub-tree in post-order - Visit the root node <left><right><root> C D ÂŁ fig Binary tree • • • + ďż˝ - The post-order traversal output ofthe given tree is HID E BF G CA 28
  • 29. T I 0 p 0 I t d a t' g h I I m ntiu.!ic.t I r I b C 9 h I d a 1· I n o P '/. I m ••• lllAATllli l I k t I b C I mg h I da 'Ii . •••• • • ••• tl!ltAll)H l n a P Jn opktfbclmghl do t t t I t t t t I •••••• • IT!itATllH • The poll ordertrewrnl ol T T 0 I 0 p a t d • • I J ITEAATIO!f I a b t / ( d 9 h • • I . .'/. . k I m n 0 p ITlll.l!lOIU a & t I k I C dglmhl '''';!' • •• ••• n o P lrnATIOIIl ob t J kn op/ r dg/ mh I •••••• •••• ••• •• • ITERA1IOH4 The preordl!r t1ave1utof T 29
  • 30. t. Binary Search Tree - Used in many search applications that constantly show and hide data, such as data. For example, map and set objects in many libraries. 2. Binary Space Partition - Used in almost any 3D video game to detennine which objects need to be rendered. 3. Binary Tries - Used in almost every high-bandwidth router to store router tables. 4. SyntaxTree - Constructed by compilers and (implicit) calculators to parse expressions. 5. Hash Trees - Used in P2P prograrns and special image signatures that require a hash to be validated, but the entire file is not available. 6. Heaps - Used to implement efficient priority queues and also used in heap sort. 7. Treap - Randomized data stlucture for Vireless networks and mernory allocation. 8. T-Tree - Although most databases use a form of 8-tree to store data on the drive, databases that store all (most) data often use T-trees. 9. Huffman Coding Tree (Chip Uni) - Used in compression algorithms, eg. For exarnple, in .jpeg and .tnp3.GGM Trees file formats - used in cryptographic applications to generate a tree with pseudo-randorn nurnbers. 30