SlideShare a Scribd company logo
1 of 20
Download to read offline
1 
Trees 
„ 
trees 
„ 
binary trees 
„ 
data structures for trees
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, Iare leaves.
3 
Trees: Definitions (2) 
„A, B, C, H are internal nodes 
„ 
The depth (level)of E is 2 
„ 
The heightof the tree is 3 
„ 
The degreeof node B is 2 
level 0 
level 1 
level 2 
level 3
4 
Trees 
A tree represents a hierarchy, for e.g. the organization structure of a corporation 
Or table of contents of a book
5 
Another Example 
Unix or DOS/Windows file system
6 
Binary Tree 
„An ordered treeis one in which the children of each node are ordered. 
„ 
Binary tree:ordered tree with all nodes having at most 2 children.
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).
8 
Examples of Binary Trees 
arithmetic expressions
9 
Examples of Binary Trees 
decision trees
10 
Complete Binary tree 
…level i has 2inodes 
… 
In a tree of height h 
… 
leaves are at level h 
… 
No. of leaves is 2h 
… 
No. of internal nodes = 1+2+22+…+2h-1= 2h-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)
11 
Binary Tree 
„A Binary tree can be obtained from an appropriate complete binary tree by pruning
12 
Minimum height of a binary tree 
„A binary tree of height h has 
… 
At most 2inodes 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
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
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 k1internal nodes in left subtree and (k-k1-1) internal nodes in right subtree. 
… 
No of leaves <= (k1+1)+(k-k1) = k+1
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
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
17 
ADTs for Binary Trees 
„accessor methods: leftChild(p), rightChild(p), sibling(p) 
„ 
update methods: 
… 
expandExternal(p), removeAboveExternal(p) 
… 
other application specific methods
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
19 
Representing Rooted Trees 
BinaryTree: 
… 
Parent: BinaryTree 
… 
LeftChild: BinaryTree 
… 
RightChild: BinaryTree 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
Root
20 
Unbounded Branching 
UnboundedTree: 
… 
Parent: UnboundedTree 
… 
LeftChild: UnboundedTree 
… 
RightSibling: UnboundedTree 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
∅ 
Root 
∅

More Related Content

What's hot (20)

Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
 
07 trees
07 trees07 trees
07 trees
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Trees
TreesTrees
Trees
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Binary trees
Binary treesBinary trees
Binary trees
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
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
 
16 Fibonacci Heaps
16 Fibonacci Heaps16 Fibonacci Heaps
16 Fibonacci Heaps
 
B trees
B treesB trees
B trees
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 

Viewers also liked (9)

Lec2
Lec2Lec2
Lec2
 
Lec9
Lec9Lec9
Lec9
 
Cursos sql server .net visual basic octubre 2010
Cursos sql server .net visual basic octubre 2010 Cursos sql server .net visual basic octubre 2010
Cursos sql server .net visual basic octubre 2010
 
Lec5
Lec5Lec5
Lec5
 
Lec4
Lec4Lec4
Lec4
 
List classes
List classesList classes
List classes
 
Lec3
Lec3Lec3
Lec3
 
Java Stack (Pilha)
Java Stack (Pilha)Java Stack (Pilha)
Java Stack (Pilha)
 
Lec1
Lec1Lec1
Lec1
 

Similar to Lec6 (20)

Lec6
Lec6Lec6
Lec6
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
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
 
7.tree
7.tree7.tree
7.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
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Tree
TreeTree
Tree
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
Tree
TreeTree
Tree
 
tree.ppt
tree.ppttree.ppt
tree.ppt
 
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
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Trees
TreesTrees
Trees
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
data structures module III & IV.pptx
data structures module III & IV.pptxdata structures module III & IV.pptx
data structures module III & IV.pptx
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec34
Lec34Lec34
Lec34
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec21
Lec21Lec21
Lec21
 

Recently uploaded

Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 

Recently uploaded (20)

Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 

Lec6

  • 1. 1 Trees „ trees „ binary trees „ data structures for trees
  • 2. 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, Iare leaves.
  • 3. 3 Trees: Definitions (2) „A, B, C, H are internal nodes „ The depth (level)of E is 2 „ The heightof the tree is 3 „ The degreeof node B is 2 level 0 level 1 level 2 level 3
  • 4. 4 Trees A tree represents a hierarchy, for e.g. the organization structure of a corporation Or table of contents of a book
  • 5. 5 Another Example Unix or DOS/Windows file system
  • 6. 6 Binary Tree „An ordered treeis one in which the children of each node are ordered. „ Binary tree:ordered tree with all nodes having at most 2 children.
  • 7. 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).
  • 8. 8 Examples of Binary Trees arithmetic expressions
  • 9. 9 Examples of Binary Trees decision trees
  • 10. 10 Complete Binary tree …level i has 2inodes … In a tree of height h … leaves are at level h … No. of leaves is 2h … No. of internal nodes = 1+2+22+…+2h-1= 2h-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)
  • 11. 11 Binary Tree „A Binary tree can be obtained from an appropriate complete binary tree by pruning
  • 12. 12 Minimum height of a binary tree „A binary tree of height h has … At most 2inodes 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
  • 13. 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
  • 14. 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 k1internal nodes in left subtree and (k-k1-1) internal nodes in right subtree. … No of leaves <= (k1+1)+(k-k1) = k+1
  • 15. 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
  • 16. 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
  • 17. 17 ADTs for Binary Trees „accessor methods: leftChild(p), rightChild(p), sibling(p) „ update methods: … expandExternal(p), removeAboveExternal(p) … other application specific methods
  • 18. 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
  • 19. 19 Representing Rooted Trees BinaryTree: … Parent: BinaryTree … LeftChild: BinaryTree … RightChild: BinaryTree ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ Root
  • 20. 20 Unbounded Branching UnboundedTree: … Parent: UnboundedTree … LeftChild: UnboundedTree … RightSibling: UnboundedTree ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ Root ∅