SlideShare ist ein Scribd-Unternehmen logo
1 von 16
DATA
STRUCTURE
INFORMATION TECHNOLOGY
Prepared By,
BHUMISH :- 150410116031
Sardar vallabhbhai patel institute of technology
BINARY TREE TRAVERSAL
2
Data structure is divided into two categories
Linear Data
Structure
Non Linear Data
Structure
Data Structure
Data elements in Sequential Order
E.g.- Array, Stack, Link list, Queue
Data elements in Hierarchical
/ Non-Sequential Order
E.g.- Tree, Graph
Tree is Non-Linear DS used in computer science
E.g.
Solving Algebraic Equation
Information Retrieval
Data Mining
Artificial Intelligence
Natural Language Processing
Tree
“Tree is a finite collection of special Data items called the NODE ”
The node of Tree are arranged in the hierarchical structure to represent the
parent-child relationship.
 First node of tree does not have any parent is called the Root (node) of tree.
Parent is connected through an edge to its child or descendents.
3
4
Tree
Terminology
Node
Root
Edge
1. Node: Each Elements of Tree is called node
2. Edges: The Lines Connecting the nodes are called Edges.
3. Parent Node: The Immediate predecessor of node is called parent node.
4. Grand Parent: Parent of Parent
5. Child Node: All the immediate successor of a node are its child node.
6. Root Node: The node does not having any parent.
7. Leaf Node: The node does not having any child called Leaf node or Terminal node.
8. Level: Level of Node is distance of that node from root.
(Rot node at distance 0 from itself hence is at level 0)
9. Height: Total No. of level in tree (is equal to depth of tree)
10. Degree of Node: No. of children it has.
11. Siblings: children of the same parent node (two or more node have same parent are
called sibling or brother
Binary Tree
5
“Is a tree which is either empty or each node can have maximum two child”
Means each node have 0, or 1 or 2 Childs
Left Child: The node on the left of any node is called left child.
Right child: The node on the right of any node is called right child.
Left Sub tree: sub tree attached to left side of root node is called left sub tree.
Right Sub tree: sub tree attached to right side of root node is called right sub tree.
Left Subtree
Left Subtree
The node of a binary tree is divided into three parts :
Left Info RightLeft child Address Right child Address
Classification of Binary Tree
6
1. Strictly Binary Tree:
Each node in a tree is either Leaf or has exactly two children
Strictly binary tree with n Leaves always contains 2n-1 nodes
E.g.
2 Leaves in First Example having 2*2-1=3 Nodes.
3 Leaves in Second Example having 3*2-1=5 Nodes.
4 Leaves in Third Example having 4*2-1=7 Nodes
Classification of Binary Tree
7
2. Complete Binary Tree:
A binary tree where having height h is a strictly binary tree and all
leaves are at h levels that tree is complete binary tree
Or
All the terminal node of a tree are at level d ,where d is the depth of
tree, then such trees are called complete binary tree.
Classification of Binary Tree
8
3. Almost Complete Binary Tree:
A binary tree in which all the terminal node or leaf node of tree are at
level d or d-1, where d is the depth of tree, then such trees are
called almost complete binary tree.
L 0
L 1
L 2
L 3
L 0
L 1
L 2
Classification of Binary Tree
9
4. Extended Binary Tree:
If in a binary tree ,each empty sub tree (Leaf Node) is replaced by a
special node then the resulting tree is extended binary tree or 2-tree
 We can add special node to leaf node & node that have only one child.
 The special nodes added to the tree are called External Node & original
node of tree are called Internal Node
External Node
Internal Node
Binary Tree
Extended Binary Tree
Note: Binary Tree of Height h has maximum (2 h+1 - 1) nodes
Binary Search Tree
A binary Search Tree is special kind if tree that satisfied the following
conditions.
1. The data elements of left sub tree are smaller than the root of the
tree.
2. The data elements of right sub tree are greater than or equal to
the root of the tree.
3. The left sub tree and right sub tree are also the binary search
trees, i.e. they must also follow the above two rules.
10
Operation on Binary Search Tree
11
1. Construction of binary Search Tree (BST)
Step.1: Initially tree is empty ,place the first element at the root.
Step.2: Compare the next element with root
if element is grater than or equal to root then place it in right child position.
else
place it in the left child position.
Step.3: Repeat the process (step 2) for the next element until end of elements or nodes.
E.g. 30, 26, 35, 9, 12, 19, 32, 40, 50, 45, 2, 9
30
26 35
9 12 32 40
45 50192 9
Operation On Binary Search Tree
12
2. Traversal of binary Search Tree
it is the process in which each node of tree is visited exactly once
Step.1: Visit the root node denoted by N.
Step.2: Visit the Left Sub Tree (node) denoted by L.
Step.3: Visit the Right sub Tree (node) denoted by R.
Methods of Traversal
1.In-order Traversal
2.Pre-order Traversal
3.Post-order Traversal
1. In-order Traversal (L N R)
13
Step.1: Visit the Left Sub Tree (node) denoted by L in In-order.
Step.2: Visit the root node denoted by N.
Step.3: Visit the Right sub Tree (node) denoted by R in In-order.
Algorithm 1:-
Inorder (ptr)
1. If ptr!=NULL
a. Inorder (ptr -> Left)
b. Print ptr ->info.
c. Inorder (ptr -> right)
2. Exit.
78
26 94
23 43 97
Solution:- 23 , 26 , 43 , 78 , 94 , 97
A
B C
D E F G
H I J
Solution:- D , B , H , E , I , A , F , C , J , G
E.g. 1
E.g. 2
1
2
3
4
5
6
1
2
3
4
5
6
7
8
9
10
2. Pre-order Traversal (N L R)
14
Step.1: Visit the root node denoted by N.
Step.2: Visit the Left Sub Tree (node) denoted by L in Pre-order.
Step.3: Visit the Right sub Tree (node) denoted by R in Pre-order.
Algorithm 2:-
Preorder (ptr)
1. If ptr!=NULL
a. Print ptr ->info.
b. Preorder (ptr -> Left)
c. Preorder (ptr -> right)
2. Exit.
78
26 94
23 43 97
Solution:- 78 , 26 , 23 , 43 , 94 , 97
A
B C
D E F G
H I J
Solution:- A , B , D , E , H , I , C , F , G , J
E.g. 1
E.g. 2
1
2
3 4
5
6
1
2
3 4
5 6
7
8 9
10
3. Post-order Traversal (L R N)
15
Step.1: Visit the Left Sub Tree (node) denoted by L in Post-order.
Step.2: Visit the Right sub Tree (node) denoted by R in Post-order.
Step.3: Visit the root node denoted by N.
Algorithm 3:-
Postorder (ptr)
1. If ptr !=NULL
a. Postorder (ptr -> Left)
b. Postorder (ptr -> right)
c. Print ptr ->info.
2. Exit.
78
26 94
23 43 97
Solution:- 23 , 43 , 26 , 97 , 94 , 78
A
B C
D E F G
H I J
Solution:- D , H , I , E , B , F , J , G , C , A
E.g. 1
E.g. 2
1 2
3
4
5
6
1
2 3
4
5
6
7
8
9
10
Tree

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
 
Trees
TreesTrees
Trees
 
Red black tree
Red black treeRed black tree
Red black tree
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Linked List
Linked ListLinked List
Linked List
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Heap sort
Heap sortHeap sort
Heap sort
 
Linked stacks and queues
Linked stacks and queuesLinked stacks and queues
Linked stacks and queues
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
single linked list
single linked listsingle linked list
single linked list
 
Avl trees
Avl treesAvl trees
Avl trees
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
 

Andere mochten auch

Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)raj upadhyay
 
computer notes - Traversal of a binary tree
computer notes - Traversal of a binary treecomputer notes - Traversal of a binary tree
computer notes - Traversal of a binary treeecomputernotes
 
Disadvantages of file management system (file processing systems)
Disadvantages of file management system(file processing systems)Disadvantages of file management system(file processing systems)
Disadvantages of file management system (file processing systems) raj upadhyay
 
Tree data structure
Tree data structureTree data structure
Tree data structureDana dia
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsVenkatesh Iyer
 
Presentation tree traversal
Presentation tree traversalPresentation tree traversal
Presentation tree traversalAyesha Tahir
 

Andere mochten auch (10)

Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)
 
Tree
TreeTree
Tree
 
computer notes - Traversal of a binary tree
computer notes - Traversal of a binary treecomputer notes - Traversal of a binary tree
computer notes - Traversal of a binary tree
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Disadvantages of file management system (file processing systems)
Disadvantages of file management system(file processing systems)Disadvantages of file management system(file processing systems)
Disadvantages of file management system (file processing systems)
 
Binary tree
Binary treeBinary tree
Binary tree
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Presentation tree traversal
Presentation tree traversalPresentation tree traversal
Presentation tree traversal
 

Ähnlich wie Tree

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 presentationnakulvarshney371
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxRajitha Reddy Alugati
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptxskilljiolms
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdfssuser50179b
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptxAbirami A
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures treemaamir farooq
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxHamzaUsman48
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data StructureOm Prakash
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms CourseHunt
 

Ähnlich wie Tree (20)

Tree
TreeTree
Tree
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
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
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Trees
TreesTrees
Trees
 
Data Structures
Data StructuresData Structures
Data Structures
 

Kürzlich hochgeladen

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 

Kürzlich hochgeladen (20)

★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 

Tree

  • 1. DATA STRUCTURE INFORMATION TECHNOLOGY Prepared By, BHUMISH :- 150410116031 Sardar vallabhbhai patel institute of technology BINARY TREE TRAVERSAL
  • 2. 2 Data structure is divided into two categories Linear Data Structure Non Linear Data Structure Data Structure Data elements in Sequential Order E.g.- Array, Stack, Link list, Queue Data elements in Hierarchical / Non-Sequential Order E.g.- Tree, Graph Tree is Non-Linear DS used in computer science E.g. Solving Algebraic Equation Information Retrieval Data Mining Artificial Intelligence Natural Language Processing
  • 3. Tree “Tree is a finite collection of special Data items called the NODE ” The node of Tree are arranged in the hierarchical structure to represent the parent-child relationship.  First node of tree does not have any parent is called the Root (node) of tree. Parent is connected through an edge to its child or descendents. 3
  • 4. 4 Tree Terminology Node Root Edge 1. Node: Each Elements of Tree is called node 2. Edges: The Lines Connecting the nodes are called Edges. 3. Parent Node: The Immediate predecessor of node is called parent node. 4. Grand Parent: Parent of Parent 5. Child Node: All the immediate successor of a node are its child node. 6. Root Node: The node does not having any parent. 7. Leaf Node: The node does not having any child called Leaf node or Terminal node. 8. Level: Level of Node is distance of that node from root. (Rot node at distance 0 from itself hence is at level 0) 9. Height: Total No. of level in tree (is equal to depth of tree) 10. Degree of Node: No. of children it has. 11. Siblings: children of the same parent node (two or more node have same parent are called sibling or brother
  • 5. Binary Tree 5 “Is a tree which is either empty or each node can have maximum two child” Means each node have 0, or 1 or 2 Childs Left Child: The node on the left of any node is called left child. Right child: The node on the right of any node is called right child. Left Sub tree: sub tree attached to left side of root node is called left sub tree. Right Sub tree: sub tree attached to right side of root node is called right sub tree. Left Subtree Left Subtree The node of a binary tree is divided into three parts : Left Info RightLeft child Address Right child Address
  • 6. Classification of Binary Tree 6 1. Strictly Binary Tree: Each node in a tree is either Leaf or has exactly two children Strictly binary tree with n Leaves always contains 2n-1 nodes E.g. 2 Leaves in First Example having 2*2-1=3 Nodes. 3 Leaves in Second Example having 3*2-1=5 Nodes. 4 Leaves in Third Example having 4*2-1=7 Nodes
  • 7. Classification of Binary Tree 7 2. Complete Binary Tree: A binary tree where having height h is a strictly binary tree and all leaves are at h levels that tree is complete binary tree Or All the terminal node of a tree are at level d ,where d is the depth of tree, then such trees are called complete binary tree.
  • 8. Classification of Binary Tree 8 3. Almost Complete Binary Tree: A binary tree in which all the terminal node or leaf node of tree are at level d or d-1, where d is the depth of tree, then such trees are called almost complete binary tree. L 0 L 1 L 2 L 3 L 0 L 1 L 2
  • 9. Classification of Binary Tree 9 4. Extended Binary Tree: If in a binary tree ,each empty sub tree (Leaf Node) is replaced by a special node then the resulting tree is extended binary tree or 2-tree  We can add special node to leaf node & node that have only one child.  The special nodes added to the tree are called External Node & original node of tree are called Internal Node External Node Internal Node Binary Tree Extended Binary Tree Note: Binary Tree of Height h has maximum (2 h+1 - 1) nodes
  • 10. Binary Search Tree A binary Search Tree is special kind if tree that satisfied the following conditions. 1. The data elements of left sub tree are smaller than the root of the tree. 2. The data elements of right sub tree are greater than or equal to the root of the tree. 3. The left sub tree and right sub tree are also the binary search trees, i.e. they must also follow the above two rules. 10
  • 11. Operation on Binary Search Tree 11 1. Construction of binary Search Tree (BST) Step.1: Initially tree is empty ,place the first element at the root. Step.2: Compare the next element with root if element is grater than or equal to root then place it in right child position. else place it in the left child position. Step.3: Repeat the process (step 2) for the next element until end of elements or nodes. E.g. 30, 26, 35, 9, 12, 19, 32, 40, 50, 45, 2, 9 30 26 35 9 12 32 40 45 50192 9
  • 12. Operation On Binary Search Tree 12 2. Traversal of binary Search Tree it is the process in which each node of tree is visited exactly once Step.1: Visit the root node denoted by N. Step.2: Visit the Left Sub Tree (node) denoted by L. Step.3: Visit the Right sub Tree (node) denoted by R. Methods of Traversal 1.In-order Traversal 2.Pre-order Traversal 3.Post-order Traversal
  • 13. 1. In-order Traversal (L N R) 13 Step.1: Visit the Left Sub Tree (node) denoted by L in In-order. Step.2: Visit the root node denoted by N. Step.3: Visit the Right sub Tree (node) denoted by R in In-order. Algorithm 1:- Inorder (ptr) 1. If ptr!=NULL a. Inorder (ptr -> Left) b. Print ptr ->info. c. Inorder (ptr -> right) 2. Exit. 78 26 94 23 43 97 Solution:- 23 , 26 , 43 , 78 , 94 , 97 A B C D E F G H I J Solution:- D , B , H , E , I , A , F , C , J , G E.g. 1 E.g. 2 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10
  • 14. 2. Pre-order Traversal (N L R) 14 Step.1: Visit the root node denoted by N. Step.2: Visit the Left Sub Tree (node) denoted by L in Pre-order. Step.3: Visit the Right sub Tree (node) denoted by R in Pre-order. Algorithm 2:- Preorder (ptr) 1. If ptr!=NULL a. Print ptr ->info. b. Preorder (ptr -> Left) c. Preorder (ptr -> right) 2. Exit. 78 26 94 23 43 97 Solution:- 78 , 26 , 23 , 43 , 94 , 97 A B C D E F G H I J Solution:- A , B , D , E , H , I , C , F , G , J E.g. 1 E.g. 2 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10
  • 15. 3. Post-order Traversal (L R N) 15 Step.1: Visit the Left Sub Tree (node) denoted by L in Post-order. Step.2: Visit the Right sub Tree (node) denoted by R in Post-order. Step.3: Visit the root node denoted by N. Algorithm 3:- Postorder (ptr) 1. If ptr !=NULL a. Postorder (ptr -> Left) b. Postorder (ptr -> right) c. Print ptr ->info. 2. Exit. 78 26 94 23 43 97 Solution:- 23 , 43 , 26 , 97 , 94 , 78 A B C D E F G H I J Solution:- D , H , I , E , B , F , J , G , C , A E.g. 1 E.g. 2 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10