SlideShare a Scribd company logo
6
Most read
14
Most read
20
Most read
DATA STRUCTURES AND
ALGORITHMS
Mrs.V.JAYAVANI., M.S(IT&M)
Assistant Professor
Department of Computer Science
UNIT – III
TREES
 Tree ADT
 Tree Traversals
 Binary Tree ADT
 Expression Trees
 Applications of Trees
 Binary Search Tree ADT
SUMMARY
 Threaded Binary Trees
 AVL Trees
 B-Tree
 B+ Tree
 Heap
 Applications of heap
 So far we have discussed mainly linear data structures – Arrays, Lists, Stacks and
Queues.
 Now we will discuss a non-linear data structure called tree.
TREE
 A tree is a non-linear and hierarchical data structure that
has a group of nodes.
 Tree is a sequence of nodes.
 Trees represent data containing a hierarchical relationship
between elements.
 Eg: Records, Family Trees and Table Contents.
 Consider a parent-child relationship.
Trees, Basic Terminology and Binary Trees
Basic Terminology
 Node – Node is elements of a Tree.
(A,B,C,D,E, F,G,H,I,J)
 Root Node − The first node is called as Root node. Every tree must
have only one root node.
(A is a root node.)
 Edge – Edge is a connection between one node to another. In a tree
with ‘N’ number of nodes there will be a maximum of ‘N-1’ number of
edges.
No. of Edges = N – 1
= 10 – 1 = 9 Edges
 Parent Node − The node which is a predecessor of any node is
called as Parent Node. It can also be defined as “the node which
has child / children”.
Here, A, B, C, D and E are Parent nodes.
Basic Terminology
 Child − The node which is descendant of any node is called as child
node. In a tree all the nodes except root are child nodes.
Here, B,C,D,E,F,G,H,I and J are children nodes.
 Siblings – The nodes with the same parent are called sibling nodes.
Here, B & C, D & E, F & G and H & I are Siblings
 Leaf − The node which does not have any child is called as leaf node. It
is also called as External nodes (or) Terminal nodes.
Here, H, I, J, F and G are Leaf nodes.
 Internal Node – The node which has atleast one child is called as
Internal node. Nodes other than leaf nodes are called as internal nodes.
Here, A, B, C, D and E are Internal nodes
 Degree – The degree of a node is total number of children it has. The
highest degree of a node among all the nodes in a tree is called as
“Degree of Tree”.
Here, Degree of A is 2, Degree of D is 2, Degree of F is 0.
Basic Terminology
 Levels − In a tree, each step from top to bottom is called as a level.
Here, Level 0 is A, Level 1 is B & C, Level 2 is D,E,F, & G
 Path − It is a sequence of nodes and edges between two nodes.
Here, ‘Path’ between A & I is A – B – D - I
 Height − Height of node is total number of edges from leaf to that
node is longest path.
Here, Height of Tree is 3.
 Depth − Depth of node is total number of edges from root to that
node.
Here, Depth of tree is 3.
 Sub Tree – Each child from a node forms a sub tree recursively.
Every child node will form a sub tree on its parent node.
Here, D,H & I is sub tree.
Tree Traversals
 Traversal is a process to visit all the nodes of a tree and may print their values too.
 All nodes are connected via edges (links) we always start from the root (head)
node.
 There are three ways which we use to traverse a tree
 Pre-order Traversal
 In-order Traversal
 Post-order Traversal
Pre-order, in-order, post-order
 In-order <left><root><right> (HDIBJEAFCG)
I. Traverse the left sub tree of R in preorder.
II. Process the root R.
III. Traverse the right sub tree of R in preorder.
 Pre-order <root><left><right> (ABDHIEJCFG)
I. Process the root R.
II. Traverse the left sub tree of R in preorder.
III. Traverse the right sub tree of R in preorder.
 Post-order <left><right><root> (HIDJEBFGCA)
I. Traverse the left sub tree of R in preorder.
II. Traverse the right sub tree of R in preorder.
III. Process the root R.
Example
Consider the traversal of a tree
Preorder : ABCEIFJDGHKL
Inorder: EICFJBGDKHLA which of the following is correct post order traversal?
Solution: Post Order traversal is IEJFCGKLHDBA
2
1
5
3
4 6
7
Binary Tree ADT
 A binary tree T is defined as a finite set of elements, called nodes,
such that:
T is empty [called the null tree or empty tree]
T contains a distinguished node R, called the root of T form
an ordered pair of disjoint binary trees LT and RT.
 If T does contain a root R.
 Two trees LT and RT are called respectively, the left and right sub
trees of R.
 If LT is non-empty, then its root is called the left successor of R.
 If RT is non-empty, then its root is called the right successor of R.
Binary Tree
Binary Tree ADT
 T consists of 10 nodes, represented by the letter A through J.
 The root of T is the node A at the top of the diagram.
 B is a left successor and C is a right successor of that node A.
 The left subtree of he root A consists of the nodes B, D, E, H & I.
 The right subtree of he root A consists of the nodes C, F, G & J.
 Any node N in a binary tree T has either 0,1 or 2 successor.
 The nodes A, B, C and E have two successors.
 The node G have only one successor.
 The nodes D, H, I, F and J have no successors.
 The nodes with no successors are called Terminal nodes.
Binary Tree
Expression Trees
 An expression tree is a tree built up from the simple operands as the leaves of binary tree and
operators as the non -leaves of binary tree.
 It is a special kind of binary tree in which:-
(i) Each leaf node contains single operand.
(ii) Each non-leaf node contains a single binary operator.
(iii) The left and right subtrees of an operator node represent sub-expression that must be
evaluated before applying the operator at the root of the subtree.
 The levels in a binary expression tree represent the precedence of operators.
 The operators at the lower level must be evaluated first and then the operators at the next level
and so on and at the last operator at the root node is applied and there by the expression is
evaluated.
Expression Trees
 Eg. Expression: - (a-b) * (c+ d )
Prefix expression is *-ab+cd similar to preorder traversal
Postfix expression is ab-cd+* similar to postorder traversal
*
+
-
a b c d
Construction of an Expression Tree
(1) From prefix and infix expression
 1. Read the prefix expression, the first element becomes the root.
 2. Now scan the infix expression till you get an element found in step1. Place all
the elements left of this element to the left of it and other element to right of it.
 3. Repeat steps 1 and 2 till all the elements from infix expression gets placed in
tree.
 4. stop
Construction of an Expression Tree
(2) From infix and postfix expression
 1. Read the postfix expression, the last element becomes the root.
 2. Now scan the infix expression till you get an element found in step1. Place all the
elements left of this element to the left of it and other element to right of it.
 3. Repeat steps 1 and 2 till all the elements from infix expression get placed in tree.
 4. Stop
Construction of an Expression Tree
(3) From a given postfix expression
 1. Read the element from postfix expression from left to right.
 2. If it is an operand then push address of this node onto stack go to step 1.
 3. If the element is an operator then pop twice which gives the address of the two
operands. Create a new node for this operator and attach the operand to the left and
right branch. Push the address of this node onto stack .
 4. If all the elements from postfix expression are not read then go to step 1.
 5. Pop the contents of stack which give the root address of expression tree.
 6. Stop
Example
Construct expression tree from following expressions:-
Postfix expression:- 5,3,2,^,*,3,7,3,+,10,/,+/
Infix expression :- (5*3^2)/(3+(7+3)/10)
Sol.
 From postfix we get root i.e. / ,hence (5*3^2) is left subtree and
(3+(7+3)/10) is right subtree.
 Now again find root from postfix for right You get + , now 3 is left and
is (7+3)/10 is right, then 3 is left child.
 In right subtree / is root , (7+3) is left subtree and 10 is right subtree.
 10 is root for right subtree. Now in left subtree + is root and 7is left
child and 3 is right child.
 Similarly for left subtree * is root and 5 is left subtree and 3^2 is right
subtree.
 Now again from right subtree root is ^, 3 is left child and 2 is right
child.
3 2
3
7
+ 10
/
3
Λ
5
+
*
/
Applications of Trees
 Directory structure of a file store
 Structure of an arithmetic expressions
 Used in almost every 3D video game to determine what objects need to be
rendered.
 Used in almost every high-bandwidth router for storing router-tables.
 Used in compression algorithms, such as those used by the .jpeg and .mp3 file-
formats.
Trees, Basic Terminology and Binary Trees

More Related Content

Similar to Trees, Basic Terminology and Binary Trees (20)

PPTX
tree Data Structures in python Traversals.pptx
RupaRaj6
 
PPT
Final tree.ppt tells about tree presentation
nakulvarshney371
 
PPTX
Data Structures -Non Linear DS-Basics ofTrees
sailaja156145
 
PPTX
Basic Tree Data Structure BST Traversals .pptx
rajinooka
 
PPTX
Trees
surya pandian
 
PPTX
Unit-VStackStackStackStackStackStack.pptx
nakshpub
 
PPTX
trees in data structure
shameen khan
 
PPTX
unit ii.pptx
ssuser24292c
 
PPTX
Data structure using c module 2
smruti sarangi
 
DOCX
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
yatakonakiran2
 
PPTX
Why Tree is considered a non-linear data structure?
mominkainat05
 
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
PDF
Chapter 5_Trees.pdf
ssuser50179b
 
PPT
Trees
Avila Rebello
 
DOCX
Unit 3,4.docx
Revathiparamanathan
 
PPTX
notesCHAPTER_5_tree_data_structure_ds.pppt
ssuserf265e9
 
PPT
Tree and Binary Search tree
Muhazzab Chouhadry
 
PPTX
7.tree
Chandan Singh
 
PPT
Lecture 5 trees
Victor Palmar
 
PPTX
unit 4 for trees data structure notes it is
logesswarisrinivasan
 
tree Data Structures in python Traversals.pptx
RupaRaj6
 
Final tree.ppt tells about tree presentation
nakulvarshney371
 
Data Structures -Non Linear DS-Basics ofTrees
sailaja156145
 
Basic Tree Data Structure BST Traversals .pptx
rajinooka
 
Unit-VStackStackStackStackStackStack.pptx
nakshpub
 
trees in data structure
shameen khan
 
unit ii.pptx
ssuser24292c
 
Data structure using c module 2
smruti sarangi
 
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
yatakonakiran2
 
Why Tree is considered a non-linear data structure?
mominkainat05
 
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
Chapter 5_Trees.pdf
ssuser50179b
 
Unit 3,4.docx
Revathiparamanathan
 
notesCHAPTER_5_tree_data_structure_ds.pppt
ssuserf265e9
 
Tree and Binary Search tree
Muhazzab Chouhadry
 
Lecture 5 trees
Victor Palmar
 
unit 4 for trees data structure notes it is
logesswarisrinivasan
 

More from JayaKamal (12)

PPT
Introduction to Machine Learning Concepts
JayaKamal
 
PPT
To learn Basic Excel - Data Entry, Formulas and Functions
JayaKamal
 
PPTX
Introduction Linked Lists - Singly Linked List,
JayaKamal
 
PPT
Introduction - Data Structures and Algorithms.ppt
JayaKamal
 
PPTX
What is an Operating Systems?
JayaKamal
 
PPTX
Jsp
JayaKamal
 
PPTX
Software Engineering
JayaKamal
 
PPTX
Software Engineering
JayaKamal
 
PPTX
Software Engineering
JayaKamal
 
PPTX
Software Engineering
JayaKamal
 
PPTX
Software Engineering
JayaKamal
 
PPTX
Software Engineering
JayaKamal
 
Introduction to Machine Learning Concepts
JayaKamal
 
To learn Basic Excel - Data Entry, Formulas and Functions
JayaKamal
 
Introduction Linked Lists - Singly Linked List,
JayaKamal
 
Introduction - Data Structures and Algorithms.ppt
JayaKamal
 
What is an Operating Systems?
JayaKamal
 
Software Engineering
JayaKamal
 
Software Engineering
JayaKamal
 
Software Engineering
JayaKamal
 
Software Engineering
JayaKamal
 
Software Engineering
JayaKamal
 
Software Engineering
JayaKamal
 
Ad

Recently uploaded (20)

PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Ad

Trees, Basic Terminology and Binary Trees

  • 1. DATA STRUCTURES AND ALGORITHMS Mrs.V.JAYAVANI., M.S(IT&M) Assistant Professor Department of Computer Science UNIT – III TREES
  • 2.  Tree ADT  Tree Traversals  Binary Tree ADT  Expression Trees  Applications of Trees  Binary Search Tree ADT SUMMARY  Threaded Binary Trees  AVL Trees  B-Tree  B+ Tree  Heap  Applications of heap
  • 3.  So far we have discussed mainly linear data structures – Arrays, Lists, Stacks and Queues.  Now we will discuss a non-linear data structure called tree.
  • 4. TREE  A tree is a non-linear and hierarchical data structure that has a group of nodes.  Tree is a sequence of nodes.  Trees represent data containing a hierarchical relationship between elements.  Eg: Records, Family Trees and Table Contents.  Consider a parent-child relationship.
  • 6. Basic Terminology  Node – Node is elements of a Tree. (A,B,C,D,E, F,G,H,I,J)  Root Node − The first node is called as Root node. Every tree must have only one root node. (A is a root node.)  Edge – Edge is a connection between one node to another. In a tree with ‘N’ number of nodes there will be a maximum of ‘N-1’ number of edges. No. of Edges = N – 1 = 10 – 1 = 9 Edges  Parent Node − The node which is a predecessor of any node is called as Parent Node. It can also be defined as “the node which has child / children”. Here, A, B, C, D and E are Parent nodes.
  • 7. Basic Terminology  Child − The node which is descendant of any node is called as child node. In a tree all the nodes except root are child nodes. Here, B,C,D,E,F,G,H,I and J are children nodes.  Siblings – The nodes with the same parent are called sibling nodes. Here, B & C, D & E, F & G and H & I are Siblings  Leaf − The node which does not have any child is called as leaf node. It is also called as External nodes (or) Terminal nodes. Here, H, I, J, F and G are Leaf nodes.  Internal Node – The node which has atleast one child is called as Internal node. Nodes other than leaf nodes are called as internal nodes. Here, A, B, C, D and E are Internal nodes  Degree – The degree of a node is total number of children it has. The highest degree of a node among all the nodes in a tree is called as “Degree of Tree”. Here, Degree of A is 2, Degree of D is 2, Degree of F is 0.
  • 8. Basic Terminology  Levels − In a tree, each step from top to bottom is called as a level. Here, Level 0 is A, Level 1 is B & C, Level 2 is D,E,F, & G  Path − It is a sequence of nodes and edges between two nodes. Here, ‘Path’ between A & I is A – B – D - I  Height − Height of node is total number of edges from leaf to that node is longest path. Here, Height of Tree is 3.  Depth − Depth of node is total number of edges from root to that node. Here, Depth of tree is 3.  Sub Tree – Each child from a node forms a sub tree recursively. Every child node will form a sub tree on its parent node. Here, D,H & I is sub tree.
  • 9. Tree Traversals  Traversal is a process to visit all the nodes of a tree and may print their values too.  All nodes are connected via edges (links) we always start from the root (head) node.  There are three ways which we use to traverse a tree  Pre-order Traversal  In-order Traversal  Post-order Traversal
  • 10. Pre-order, in-order, post-order  In-order <left><root><right> (HDIBJEAFCG) I. Traverse the left sub tree of R in preorder. II. Process the root R. III. Traverse the right sub tree of R in preorder.  Pre-order <root><left><right> (ABDHIEJCFG) I. Process the root R. II. Traverse the left sub tree of R in preorder. III. Traverse the right sub tree of R in preorder.  Post-order <left><right><root> (HIDJEBFGCA) I. Traverse the left sub tree of R in preorder. II. Traverse the right sub tree of R in preorder. III. Process the root R.
  • 11. Example Consider the traversal of a tree Preorder : ABCEIFJDGHKL Inorder: EICFJBGDKHLA which of the following is correct post order traversal? Solution: Post Order traversal is IEJFCGKLHDBA 2 1 5 3 4 6 7
  • 12. Binary Tree ADT  A binary tree T is defined as a finite set of elements, called nodes, such that: T is empty [called the null tree or empty tree] T contains a distinguished node R, called the root of T form an ordered pair of disjoint binary trees LT and RT.  If T does contain a root R.  Two trees LT and RT are called respectively, the left and right sub trees of R.  If LT is non-empty, then its root is called the left successor of R.  If RT is non-empty, then its root is called the right successor of R. Binary Tree
  • 13. Binary Tree ADT  T consists of 10 nodes, represented by the letter A through J.  The root of T is the node A at the top of the diagram.  B is a left successor and C is a right successor of that node A.  The left subtree of he root A consists of the nodes B, D, E, H & I.  The right subtree of he root A consists of the nodes C, F, G & J.  Any node N in a binary tree T has either 0,1 or 2 successor.  The nodes A, B, C and E have two successors.  The node G have only one successor.  The nodes D, H, I, F and J have no successors.  The nodes with no successors are called Terminal nodes. Binary Tree
  • 14. Expression Trees  An expression tree is a tree built up from the simple operands as the leaves of binary tree and operators as the non -leaves of binary tree.  It is a special kind of binary tree in which:- (i) Each leaf node contains single operand. (ii) Each non-leaf node contains a single binary operator. (iii) The left and right subtrees of an operator node represent sub-expression that must be evaluated before applying the operator at the root of the subtree.  The levels in a binary expression tree represent the precedence of operators.  The operators at the lower level must be evaluated first and then the operators at the next level and so on and at the last operator at the root node is applied and there by the expression is evaluated.
  • 15. Expression Trees  Eg. Expression: - (a-b) * (c+ d ) Prefix expression is *-ab+cd similar to preorder traversal Postfix expression is ab-cd+* similar to postorder traversal * + - a b c d
  • 16. Construction of an Expression Tree (1) From prefix and infix expression  1. Read the prefix expression, the first element becomes the root.  2. Now scan the infix expression till you get an element found in step1. Place all the elements left of this element to the left of it and other element to right of it.  3. Repeat steps 1 and 2 till all the elements from infix expression gets placed in tree.  4. stop
  • 17. Construction of an Expression Tree (2) From infix and postfix expression  1. Read the postfix expression, the last element becomes the root.  2. Now scan the infix expression till you get an element found in step1. Place all the elements left of this element to the left of it and other element to right of it.  3. Repeat steps 1 and 2 till all the elements from infix expression get placed in tree.  4. Stop
  • 18. Construction of an Expression Tree (3) From a given postfix expression  1. Read the element from postfix expression from left to right.  2. If it is an operand then push address of this node onto stack go to step 1.  3. If the element is an operator then pop twice which gives the address of the two operands. Create a new node for this operator and attach the operand to the left and right branch. Push the address of this node onto stack .  4. If all the elements from postfix expression are not read then go to step 1.  5. Pop the contents of stack which give the root address of expression tree.  6. Stop
  • 19. Example Construct expression tree from following expressions:- Postfix expression:- 5,3,2,^,*,3,7,3,+,10,/,+/ Infix expression :- (5*3^2)/(3+(7+3)/10) Sol.  From postfix we get root i.e. / ,hence (5*3^2) is left subtree and (3+(7+3)/10) is right subtree.  Now again find root from postfix for right You get + , now 3 is left and is (7+3)/10 is right, then 3 is left child.  In right subtree / is root , (7+3) is left subtree and 10 is right subtree.  10 is root for right subtree. Now in left subtree + is root and 7is left child and 3 is right child.  Similarly for left subtree * is root and 5 is left subtree and 3^2 is right subtree.  Now again from right subtree root is ^, 3 is left child and 2 is right child. 3 2 3 7 + 10 / 3 Λ 5 + * /
  • 20. Applications of Trees  Directory structure of a file store  Structure of an arithmetic expressions  Used in almost every 3D video game to determine what objects need to be rendered.  Used in almost every high-bandwidth router for storing router-tables.  Used in compression algorithms, such as those used by the .jpeg and .mp3 file- formats.