SlideShare a Scribd company logo
Trees
   trees
   binary trees
   data structures for trees




                                1
Trees: Definitions
   A is the root node.
   B is parent of D & E.
   A is ancestor of D & E.
   D and E are
    descendants of A.
   C is the sibling of B
   D and E are the
    children of B.
   D, E, F, G, I are
    leaves.
                              2
Trees: Definitions (2)
 A, B, C, H are internal nodes
 The depth (level) of E is 2
 The height of the tree is 3
 The degree of node B is 2


                              level 0
                              level 1

                              level 2

                              level 3
                                        3
Trees


A tree represents a
hierarchy, for e.g. the
organization structure of a
corporation


Or table of contents
of a book

                              4
Another Example
Unix or DOS/Windows file system




                                  5
Binary Tree
   An ordered tree is one in which the children of
    each node are ordered.
   Binary tree: ordered tree with all nodes having
    at most 2 children.




                                                      6
Recursive definition of binary tree
A binary tree is either a
   leaf or
   An internal node (the root) and one/two binary
    trees (left subtree and/or right subtree).




                                                     7
Examples of Binary Trees
arithmetic expressions




                           8
Examples of Binary Trees
decision trees




                           9
Complete Binary tree

level i has 2i nodes
In a tree of height h
   leaves are at level h
   No. of leaves is 2h
                                             h
   No. of internal nodes = 1+2+22+…+2h-1 = 2 -1
   No of internal nodes = no of leaves -1
   Total no. of nodes is 2h+1-1 = n
In a tree of n nodes
   No of leaves is (n+1)/2
   Height = log2 (no of leaves)
                                                   10
Binary Tree
   A Binary tree can be obtained from an
    appropriate complete binary tree by
    pruning




                                            11
Minimum height of a binary tree
   A binary tree of height h has
     At most 2i nodes at level i
     At most 1+2+22+…+2h = 2h+1-1 nodes

   If the tree has n nodes then
    n <= 2h+1-1
     Hence h >= log2 (n+1)/2




                                           12
Maximum height of a binary tree
 A binary tree on n nodes has height at
  most n-1
 This is obtained when every node (except
  the leaf) has exactly one child




                                             13
No of leaves in a binary tree
 no of leaves <= 1+ no of internal nodes.
 Proof: by induction on no of internal nodes
     Tree with 1 node has a leaf but no internal
      node.
     Assume stmt is true for tree with k-1 internal
      nodes.
     A tree with k internal nodes has k1 internal
      nodes in left subtree and (k-k1-1) internal
      nodes in right subtree.
     No of leaves <= (k1+1)+(k-k1) = k+1


                                                       14
leaves in a binary tree (2)

For a binary tree on n nodes
 No of leaves + no of internal nodes = n
 No of leaves <= no of internal nodes + 1
 Hence, no of leaves <= (n+1)/2
 Minimum no of leaves is 1




                                             15
ADTs for Trees
   generic container methods: size(), isEmpty(),
    elements()
   positional container methods: positions(),
    swapElements(p,q), replaceElement(p,e)
   query methods: isRoot(p), isInternal(p),
    isExternal(p)
   accessor methods: root(), parent(p),
    children(p)
   update methods: application specific

                                                    16
ADTs for Binary Trees

 accessor methods: leftChild(p),
  rightChild(p), sibling(p)
 update methods:
     expandExternal(p),  removeAboveExternal(p)
     other application specific methods




                                                   17
The Node Structure
Each node in the tree
contains
   key[x]  – key
   left[x] – pointer to left
    child
   right[x] – pt. to right
    child
   p[x] – pt. to parent node



                                18
Representing Rooted Trees
BinaryTree:                        Root
   Parent: BinaryTree
   LeftChild: BinaryTree                 

   RightChild: BinaryTree


                                              




                                                  




                                           
                                                    19
Unbounded Branching
UnboundedTree:
                                  Root
   Parent: UnboundedTree
   LeftChild: UnboundedTree             
                                          
   RightSibling: UnboundedTree



                                                            




                                                




                             
                                                        20

More Related Content

What's hot

Lec1
Lec1Lec1
Lec3
Lec3Lec3
Chapter 2: Boolean Algebra and Logic Gates
Chapter 2: Boolean Algebra and Logic GatesChapter 2: Boolean Algebra and Logic Gates
Chapter 2: Boolean Algebra and Logic Gates
Heman Pathak
 
Queue
QueueQueue
Circular linked list
Circular linked listCircular linked list
Circular linked listdchuynh
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
Krish_ver2
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
vinaykhimsuriya1
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithnKumar
 
Lec2
Lec2Lec2
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
rajshreemuthiah
 
Linked list
Linked listLinked list
Linked list
Md. Afif Al Mamun
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
CHANDAN KUMAR
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
Ayesha Tahir
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 

What's hot (20)

Lec10
Lec10Lec10
Lec10
 
Lec8
Lec8Lec8
Lec8
 
Lec1
Lec1Lec1
Lec1
 
Lec3
Lec3Lec3
Lec3
 
Lec11
Lec11Lec11
Lec11
 
Lec20
Lec20Lec20
Lec20
 
Chapter 2: Boolean Algebra and Logic Gates
Chapter 2: Boolean Algebra and Logic GatesChapter 2: Boolean Algebra and Logic Gates
Chapter 2: Boolean Algebra and Logic Gates
 
Hashing
HashingHashing
Hashing
 
Queue
QueueQueue
Queue
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Lec2
Lec2Lec2
Lec2
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Linked list
Linked listLinked list
Linked list
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 

Viewers also liked

Jim sterne terametric_twitter-webinar_120910
Jim sterne terametric_twitter-webinar_120910Jim sterne terametric_twitter-webinar_120910
Jim sterne terametric_twitter-webinar_120910
Terametric
 
Abctest
AbctestAbctest
Abctestrupamb
 
History and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
History and Overview of Tobacco Policy Initiatives in Maine, 1897-PresentHistory and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
History and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
Maine Public Health Association Tobacco Policy Subcommittee and Friends of the FHM
 
Mexico by john2
Mexico by john2  Mexico by john2
Mexico by john2
filipj2000
 
Fascinating....... mother russia
Fascinating....... mother russiaFascinating....... mother russia
Fascinating....... mother russiafilipj2000
 
移动产品界面适配设计
移动产品界面适配设计移动产品界面适配设计
Corporate World
Corporate WorldCorporate World
Corporate Worldtanishgu
 
Tecnicas
TecnicasTecnicas
Building a Successful Facebook Marketing Program
Building a Successful Facebook Marketing ProgramBuilding a Successful Facebook Marketing Program
Building a Successful Facebook Marketing Program
Internet Gal
 
12강 기업교육론 20110601
12강 기업교육론 2011060112강 기업교육론 20110601
12강 기업교육론 20110601
조현경
 
10강 기업교육론 20110504
10강 기업교육론 2011050410강 기업교육론 20110504
10강 기업교육론 20110504
조현경
 
CofC presentation
CofC presentationCofC presentation
CofC presentation
Susan Isbell
 
Прориси
ПрорисиПрориси
Прорисиbelyavsky
 

Viewers also liked (20)

Jim sterne terametric_twitter-webinar_120910
Jim sterne terametric_twitter-webinar_120910Jim sterne terametric_twitter-webinar_120910
Jim sterne terametric_twitter-webinar_120910
 
Abctest
AbctestAbctest
Abctest
 
Fog
FogFog
Fog
 
Cigarette excise tax add data 04 11
Cigarette excise tax add data 04 11Cigarette excise tax add data 04 11
Cigarette excise tax add data 04 11
 
Akropolis
AkropolisAkropolis
Akropolis
 
History and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
History and Overview of Tobacco Policy Initiatives in Maine, 1897-PresentHistory and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
History and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
 
Mexico by john2
Mexico by john2  Mexico by john2
Mexico by john2
 
Fascinating....... mother russia
Fascinating....... mother russiaFascinating....... mother russia
Fascinating....... mother russia
 
Shanxi china
Shanxi  chinaShanxi  china
Shanxi china
 
移动产品界面适配设计
移动产品界面适配设计移动产品界面适配设计
移动产品界面适配设计
 
Lec13
Lec13Lec13
Lec13
 
Corporate World
Corporate WorldCorporate World
Corporate World
 
Tecnicas
TecnicasTecnicas
Tecnicas
 
Building a Successful Facebook Marketing Program
Building a Successful Facebook Marketing ProgramBuilding a Successful Facebook Marketing Program
Building a Successful Facebook Marketing Program
 
STEM Powers Jets
STEM Powers JetsSTEM Powers Jets
STEM Powers Jets
 
12강 기업교육론 20110601
12강 기업교육론 2011060112강 기업교육론 20110601
12강 기업교육론 20110601
 
People scenes
People scenesPeople scenes
People scenes
 
10강 기업교육론 20110504
10강 기업교육론 2011050410강 기업교육론 20110504
10강 기업교육론 20110504
 
CofC presentation
CofC presentationCofC presentation
CofC presentation
 
Прориси
ПрорисиПрориси
Прориси
 

Similar to Lec6

Lec6
Lec6Lec6
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
HamzaUsman48
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
Dabbal Singh Mahara
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
ssuser50179b
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
Hanif Durad
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
Tribhuvan University
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
DhanushSrinivasulu
 
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of treeSiddhi Viradiya
 
Tree
TreeTree
Tree
bhumish
 
Introduction to Binary Tree and Conersion of General tree to Binary Tree
Introduction to Binary Tree  and Conersion of General tree to Binary TreeIntroduction to Binary Tree  and Conersion of General tree to Binary Tree
Introduction to Binary Tree and Conersion of General tree to Binary Tree
SwarupaDeshpande4
 
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 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
 
data structures module III & IV.pptx
data structures module III & IV.pptxdata structures module III & IV.pptx
data structures module III & IV.pptx
rani marri
 
09 binary-trees
09 binary-trees09 binary-trees
09 binary-treesTech_MX
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary treeTree terminology and introduction to binary tree
Tree terminology and introduction to binary tree
jyoti_lakhani
 
AD3251-Data Structures Design-Notes-Tree.pdf
AD3251-Data Structures  Design-Notes-Tree.pdfAD3251-Data Structures  Design-Notes-Tree.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
Ramco Institute of Technology, Rajapalayam, Tamilnadu, India
 

Similar to Lec6 (20)

Lec6
Lec6Lec6
Lec6
 
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
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
Trees
TreesTrees
Trees
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Tree
TreeTree
Tree
 
Introduction to Binary Tree and Conersion of General tree to Binary Tree
Introduction to Binary Tree  and Conersion of General tree to Binary TreeIntroduction to Binary Tree  and Conersion of General tree to Binary Tree
Introduction to Binary Tree and Conersion of General tree to Binary Tree
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
data structures module III & IV.pptx
data structures module III & IV.pptxdata structures module III & IV.pptx
data structures module III & IV.pptx
 
09 binary-trees
09 binary-trees09 binary-trees
09 binary-trees
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary treeTree terminology and introduction to binary tree
Tree terminology and introduction to binary tree
 
AD3251-Data Structures Design-Notes-Tree.pdf
AD3251-Data Structures  Design-Notes-Tree.pdfAD3251-Data Structures  Design-Notes-Tree.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
 

Lec6

  • 1. Trees  trees  binary trees  data structures for trees 1
  • 2. Trees: Definitions  A is the root node.  B is parent of D & E.  A is ancestor of D & E.  D and E are descendants of A.  C is the sibling of B  D and E are the children of B.  D, E, F, G, I are leaves. 2
  • 3. Trees: Definitions (2)  A, B, C, H are internal nodes  The depth (level) of E is 2  The height of the tree is 3  The degree of node B is 2 level 0 level 1 level 2 level 3 3
  • 4. Trees A tree represents a hierarchy, for e.g. the organization structure of a corporation Or table of contents of a book 4
  • 5. Another Example Unix or DOS/Windows file system 5
  • 6. Binary Tree  An ordered tree is one in which the children of each node are ordered.  Binary tree: ordered tree with all nodes having at most 2 children. 6
  • 7. Recursive definition of binary tree A binary tree is either a  leaf or  An internal node (the root) and one/two binary trees (left subtree and/or right subtree). 7
  • 8. Examples of Binary Trees arithmetic expressions 8
  • 9. Examples of Binary Trees decision trees 9
  • 10. Complete Binary tree level i has 2i nodes In a tree of height h leaves are at level h No. of leaves is 2h h No. of internal nodes = 1+2+22+…+2h-1 = 2 -1 No of internal nodes = no of leaves -1 Total no. of nodes is 2h+1-1 = n In a tree of n nodes No of leaves is (n+1)/2 Height = log2 (no of leaves) 10
  • 11. Binary Tree  A Binary tree can be obtained from an appropriate complete binary tree by pruning 11
  • 12. Minimum height of a binary tree  A binary tree of height h has  At most 2i nodes at level i  At most 1+2+22+…+2h = 2h+1-1 nodes  If the tree has n nodes then n <= 2h+1-1  Hence h >= log2 (n+1)/2 12
  • 13. Maximum height of a binary tree  A binary tree on n nodes has height at most n-1  This is obtained when every node (except the leaf) has exactly one child 13
  • 14. No of leaves in a binary tree  no of leaves <= 1+ no of internal nodes.  Proof: by induction on no of internal nodes  Tree with 1 node has a leaf but no internal node.  Assume stmt is true for tree with k-1 internal nodes.  A tree with k internal nodes has k1 internal nodes in left subtree and (k-k1-1) internal nodes in right subtree.  No of leaves <= (k1+1)+(k-k1) = k+1 14
  • 15. leaves in a binary tree (2) For a binary tree on n nodes  No of leaves + no of internal nodes = n  No of leaves <= no of internal nodes + 1  Hence, no of leaves <= (n+1)/2  Minimum no of leaves is 1 15
  • 16. ADTs for Trees  generic container methods: size(), isEmpty(), elements()  positional container methods: positions(), swapElements(p,q), replaceElement(p,e)  query methods: isRoot(p), isInternal(p), isExternal(p)  accessor methods: root(), parent(p), children(p)  update methods: application specific 16
  • 17. ADTs for Binary Trees  accessor methods: leftChild(p), rightChild(p), sibling(p)  update methods:  expandExternal(p), removeAboveExternal(p)  other application specific methods 17
  • 18. The Node Structure Each node in the tree contains  key[x] – key  left[x] – pointer to left child  right[x] – pt. to right child  p[x] – pt. to parent node 18
  • 19. Representing Rooted Trees BinaryTree: Root  Parent: BinaryTree  LeftChild: BinaryTree   RightChild: BinaryTree           19
  • 20. Unbounded Branching UnboundedTree: Root  Parent: UnboundedTree  LeftChild: UnboundedTree    RightSibling: UnboundedTree          20