SlideShare a Scribd company logo
1 of 14
A tree is a finite set of one or more nodes
 such that:
There is a specially designated node called
 the root.
The remaining nodes are partitioned into n>=0
 disjoint sets T1, ..., Tn, where each of these sets is a
 tree.
We call T1, ..., Tn the subtrees of the root.
Crown


Church of England
                          Cabinet           House of Commons         House of Lords   Supreme Court




                                     Ministers

                    County Council
                                                    Metropolitan police

     Rural District Council    County Borough Council
Level
node (13)
degree of a node
                                      A                  1
leaf (terminal)                    3      1
Non terminal
parent                      B        C          D2       2
                          2    2 1     2      3
children
sibling                  E     F     G      H   I    J
                       2    30   30    31 3 0     30   3 3
degree of a tree (3)
ancestor               K    L              M             4
                     0   40    4        0     4
level of a node
height of a tree (4)
♣ The degree of a node is the number of subtrees
  of the node
   ◙   The degree of A is 3; the degree of C is 1.
♣ The node with degree 0 is a leaf or terminal node.
♣ A node that has subtrees is the parent of the roots of the
  subtrees.
♣ The roots of these subtrees are the children of the node.
♣ Children of the same parent are siblings.
♣ The ancestors of a node are all the nodes along the path from
  the root to the node.
Property            Value
        A           Number of nodes        9
                    Height                 5
    B           C   Root Node              A
                    Leaves             C,D,F,H,I
D   E       F       Interior nodes      A,B,E,G
                    Number of levels       5
                    Ancestors of H      G,E,B,A
    G               Descendants of B     E,G,H,I
                    Siblings of E          G
H       I
◛Every tree node:
     object – useful information
     children – pointers to its children nodes



          O


O                          O                      O


                                 O
Two main methods:
    ► Preorder
    ► Postorder
   Recursive definition

▓ PREorder:
   ► visit the root
   ► traverse in preorder the children (subtrees)
▓ POSTorder
   ► traverse in postorder the children (subtrees)
   ► visit the root
Algorithm preorder(v)
  “visit” node v
  for each child w of v do
     recursively perform preorder(w)
In simple language first Root node is traversed then Left child
  then Right child is traversed.
                                               Root Node
                                      1

   Left Child
                    2                                   3        Right Child


         4                   5                 6                 7

     8          9       10       11       12       13       14       15
Algorithm postorder(v)
  for each child w of v do
     recursively perform postorder(w)
  “visit” node v
In simple language first Left child is traversed then Right child
  then Root node is traversed.
                                               Root Node
                                      1

   Left Child
                    2                                   3        Right Child


           4                 5                 6                 7

     8          9       10       11       12       13       14       15
◘ A special class of trees: max degree for each node is 2
◘ Recursive definition: 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.
◘ Any tree can be transformed into binary tree.
   ◚ By left child-right   sibling representation
A

        B

    E           C


            G       D
K       F

                H
    L

            M           I

                            J
The maximum number of nodes on level i of a binary tree is 2i-
 1, i>=1.

The maximum number of nodes in a binary tree of depth k is 2k-
 1, k>=1.


 Prove by
 induction              k
                             2   i 1
                                       2   k
                                               1
                       i 1
If a complete binary tree with n nodes (depth = log n + 1) is
represented sequentially, then for any node with index i,
1<=i<=n, we have:
   parent(i) is at i/2 if i!=1. If i=1, i is at the root and
   has no parent.
   leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no
   left child.
   rightChild(i) is at 2i+1 if 2i +1 <=n. If 2i +1 >n,
   then i has no right child.

More Related Content

Viewers also liked

Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
Abhishek Pachisia
 
Expert Systems
Expert SystemsExpert Systems
Expert Systems
osmancikk
 
Program Evaluation and Review Technique (PERT)
Program Evaluation and Review Technique (PERT)Program Evaluation and Review Technique (PERT)
Program Evaluation and Review Technique (PERT)
Abhishek Pachisia
 

Viewers also liked (12)

Telecom Industry
Telecom IndustryTelecom Industry
Telecom Industry
 
V.G. siddhartha
V.G. siddharthaV.G. siddhartha
V.G. siddhartha
 
Strategic alignment model (SAM)
Strategic alignment model (SAM)Strategic alignment model (SAM)
Strategic alignment model (SAM)
 
Vb.net (loop structure)
Vb.net (loop structure)Vb.net (loop structure)
Vb.net (loop structure)
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
 
Hydrogen energy
Hydrogen energyHydrogen energy
Hydrogen energy
 
Inference engine
Inference engineInference engine
Inference engine
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Work breakdown structure
Work breakdown structureWork breakdown structure
Work breakdown structure
 
IEEE 802.11
IEEE 802.11IEEE 802.11
IEEE 802.11
 
Expert Systems
Expert SystemsExpert Systems
Expert Systems
 
Program Evaluation and Review Technique (PERT)
Program Evaluation and Review Technique (PERT)Program Evaluation and Review Technique (PERT)
Program Evaluation and Review Technique (PERT)
 

Similar to Tree

09 binary-trees
09 binary-trees09 binary-trees
09 binary-trees
Tech_MX
 
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptxTERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
KALPANAC20
 

Similar to Tree (20)

Data structures
Data structuresData structures
Data structures
 
Trees
TreesTrees
Trees
 
heap sort
 heap sort heap sort
heap sort
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
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
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Lec6
Lec6Lec6
Lec6
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
Trees ayaz
Trees ayazTrees ayaz
Trees ayaz
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
09 binary-trees
09 binary-trees09 binary-trees
09 binary-trees
 
Binary tree traversal ppt - 02.03.2020
Binary tree traversal   ppt - 02.03.2020Binary tree traversal   ppt - 02.03.2020
Binary tree traversal ppt - 02.03.2020
 
Binary tree
Binary treeBinary tree
Binary tree
 
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptxTERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
 
Unit 8
Unit 8Unit 8
Unit 8
 
TREES34.pptx
TREES34.pptxTREES34.pptx
TREES34.pptx
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
Unit 3 Tree chapter 5
Unit 3  Tree chapter 5Unit 3  Tree chapter 5
Unit 3 Tree chapter 5
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

Tree

  • 1.
  • 2. A tree is a finite set of one or more nodes such that: There is a specially designated node called the root. The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. We call T1, ..., Tn the subtrees of the root.
  • 3. Crown Church of England Cabinet House of Commons House of Lords Supreme Court Ministers County Council Metropolitan police Rural District Council County Borough Council
  • 4. Level node (13) degree of a node A 1 leaf (terminal) 3 1 Non terminal parent B C D2 2 2 2 1 2 3 children sibling E F G H I J 2 30 30 31 3 0 30 3 3 degree of a tree (3) ancestor K L M 4 0 40 4 0 4 level of a node height of a tree (4)
  • 5. ♣ The degree of a node is the number of subtrees of the node ◙ The degree of A is 3; the degree of C is 1. ♣ The node with degree 0 is a leaf or terminal node. ♣ A node that has subtrees is the parent of the roots of the subtrees. ♣ The roots of these subtrees are the children of the node. ♣ Children of the same parent are siblings. ♣ The ancestors of a node are all the nodes along the path from the root to the node.
  • 6. Property Value A Number of nodes 9 Height 5 B C Root Node A Leaves C,D,F,H,I D E F Interior nodes A,B,E,G Number of levels 5 Ancestors of H G,E,B,A G Descendants of B E,G,H,I Siblings of E G H I
  • 7. ◛Every tree node:  object – useful information  children – pointers to its children nodes O O O O O
  • 8. Two main methods: ► Preorder ► Postorder Recursive definition ▓ PREorder: ► visit the root ► traverse in preorder the children (subtrees) ▓ POSTorder ► traverse in postorder the children (subtrees) ► visit the root
  • 9. Algorithm preorder(v) “visit” node v for each child w of v do recursively perform preorder(w) In simple language first Root node is traversed then Left child then Right child is traversed. Root Node 1 Left Child 2 3 Right Child 4 5 6 7 8 9 10 11 12 13 14 15
  • 10. Algorithm postorder(v) for each child w of v do recursively perform postorder(w) “visit” node v In simple language first Left child is traversed then Right child then Root node is traversed. Root Node 1 Left Child 2 3 Right Child 4 5 6 7 8 9 10 11 12 13 14 15
  • 11. ◘ A special class of trees: max degree for each node is 2 ◘ Recursive definition: 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. ◘ Any tree can be transformed into binary tree. ◚ By left child-right sibling representation
  • 12. A B E C G D K F H L M I J
  • 13. The maximum number of nodes on level i of a binary tree is 2i- 1, i>=1. The maximum number of nodes in a binary tree of depth k is 2k- 1, k>=1. Prove by induction k 2 i 1 2 k 1 i 1
  • 14. If a complete binary tree with n nodes (depth = log n + 1) is represented sequentially, then for any node with index i, 1<=i<=n, we have: parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no parent. leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no left child. rightChild(i) is at 2i+1 if 2i +1 <=n. If 2i +1 >n, then i has no right child.