SlideShare uma empresa Scribd logo
1 de 20
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

Mais conteúdo relacionado

Mais procurados (20)

Lec2
Lec2Lec2
Lec2
 
Lec1
Lec1Lec1
Lec1
 
Lec5
Lec5Lec5
Lec5
 
Lec20
Lec20Lec20
Lec20
 
Lec3
Lec3Lec3
Lec3
 
Lec11
Lec11Lec11
Lec11
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Hashing and separate chain
Hashing and separate chainHashing and separate chain
Hashing and separate chain
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Bst(Binary Search Tree)
Bst(Binary Search Tree)Bst(Binary Search Tree)
Bst(Binary Search Tree)
 
Linked list
Linked list Linked list
Linked list
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Hash table
Hash tableHash table
Hash table
 
Hash tables
Hash tablesHash tables
Hash tables
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Heap sort
Heap sort Heap sort
Heap sort
 

Destaque (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
 
Прориси
ПрорисиПрориси
Прориси
 

Semelhante a Lec6

Semelhante a 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