SlideShare a Scribd company logo
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

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
zia eagle
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
Daniyal Khan
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
cinterviews
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
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
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structureghhgj jhgh
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
SSE_AndyLi
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Treeleminhvuong
 
07 trees
07 trees07 trees
07 trees
Rajan Gautam
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
Trees
TreesTrees
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
varagilavanya
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
Sadaf Ismail
 
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
ManishPrajapati78
 
16 Fibonacci Heaps
16 Fibonacci Heaps16 Fibonacci Heaps
16 Fibonacci Heaps
Andres Mendez-Vazquez
 
B trees
B treesB trees
B trees
prittopaul1
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 

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

Lec2
Lec2Lec2
Lec5
Lec5Lec5
Lec4
Lec4Lec4
Lec3
Lec3Lec3
Java Stack (Pilha)
Java Stack (Pilha)Java Stack (Pilha)
Java Stack (Pilha)
Samuel Santos
 
Lec1
Lec1Lec1

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

Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
Hanif Durad
 
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
 
7.tree
7.tree7.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 treeSiddhi Viradiya
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
Dabbal Singh Mahara
 
Tree
TreeTree
Tree
bhumish
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
ssuser50179b
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
DhanushSrinivasulu
 
Tree
TreeTree
tree.ppt
tree.ppttree.ppt
tree.ppt
wondmhunegn
 
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
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
shashankbhadouria4
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
worldchannel
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
Guru Nanak Institute Of Tech
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
Om Prakash
 
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
 

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

The joyless economy
The joyless economyThe joyless economy
The joyless economy
Nikhil Chilwant
 
I 7
I 7I 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 )
Nikhil Chilwant
 

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

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 

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 ∅