SlideShare a Scribd company logo
TREE
 1. THE CONCEPT OF TREES
 2. BINARY TREE AND REPRESENTATION
 3. BINARY TREE TRAVERSAL
 4. BINARY SEARCH TREE
6
9
2
4
1 8
<
>
=
1. THE CONCEPT OF TREES
 A tree is a set of one or more nodes T:
– there is a specially designated node called a
root
– The remaining nodes are partitioned into n
disjointed set of nodes T1, T2,…,Tn, each of
which is a tree.
1. THE CONCEPT OF TREES
 Example
1. THE CONCEPT OF TREES
 It’s not a tree
Tree
1. THE CONCEPT OF TREES: Some
terminology
 Root
 Child (left,right)
 Parent
 Leaf node
 Subtree
 Ancestor of a node
 Descendant of a node
1. THE CONCEPT OF TREES
 Degree of a Node of a Tree
– The degree of a node of a tree is the number of subtrees
having this node as a root.
 Degree of a Tree
– The degree of a tree is defined as the maximum of degree
of the nodes of the tree
 Level of a Node
– level of the root node as 1, and incrementing it by 1 as we
move from the root towards the subtrees.
2. BINARY TREE AND REPRESENTATION
 BINARY TREE
– no node can have a degree of more than 2.
– The maximum number of nodes at level i will be
2i−1
– If k is the depth of the tree then the maximum
number of nodes that the tree can have is
– 2k − 1 = 2k−1 + 2k−2 + … + 20
2. BINARY TREE AND REPRESENTATION
 BINARY TREE
– A full binary tree is a binary of depth k having
2k − 1 nodes.
– If it has < 2k − 1, it is not a full binary tree
What is the height h of a full tree
with N nodes?
 The max height of a tree with N nodes is N
(same as a linked list)
 The min height of a tree with N nodes is
log(N+1)
2 1
2 1
log( 1) (log )
h
h
N
N
h N O N
 =
 = 
 =  
2. BINARY TREE AND REPRESENTATION
3=22-1
7=23-1 15=24-1
full binary
2. BINARY TREE AND REPRESENTATION
struct node
{ int data;
node *left;
node *right;
};
Tree traversal
 Used to print out the data in a tree in a certain order
– inorder (LDR )
– Postorder (LRD )
– preorder (DLR )
 Pre-order traversal
– Print the data at the root
– Recursively print out all data in the left subtree
– Recursively print out all data in the right subtree
Preorder, Postorder and Inorder
 Preorder traversal
– node, left, right
– prefix expression
++a*bc*+*defg
Preorder, Postorder and Inorder
 Postorder traversal
– left, right, node
– postfix expression
abc*+de*f+g*+
 Inorder traversal
– left, node, right.
– infix expression
a+b*c+d*e+f*g
Preorder, Postorder and Inorder
3. BINARY TREE TRAVERSAL
3. BINARY TREE TRAVERSAL
Inorder = DBEAC
Many trees
4. BINARY SEARCH TREE
 A binary search tree
– is a binary tree (may be empty)
– every node must contain an identifier.
– An identifier of any node in the left subtree is less
than the identifier of the root.
– An identifier of any node in the right subtree is
greater than the identifier of the root.
– Both the left subtree and right subtree are binary
search trees.
4. BINARY SEARCH TREE
 binary search tree.
Binary Search Trees
A binary search tree Not a binary search tree
Performance
 Consider a dictionary with n
items implemented by
means of a binary search
tree of height h
– the space used is O(n)
– methods find, insert and
remove take O(h) time
 The height h is O(n) in the
worst case and O(log n) in
the best case
4. BINARY SEARCH TREE
 Why using binary search tree
– traverse in inorder: sorted list
– searching becomes faster
 But..
– Insert, delete: slow
 Important thing: Index in Database system
– Using the right way of Index property
Search
 To search for a key k, we
trace a downward path
starting at the root
 The next node visited
depends on the outcome
of the comparison of k with
the key of the current node
 If we reach a leaf, the key
is not found and we return
nukk
 Example: find(4):
– Call TreeSearch(4,root)
6
9
2
4
1 8
<
>
=
Insertion
 To perform operation inser(k,
o), we search for key k (using
TreeSearch)
 Assume k is not already in the
tree, and let let w be the leaf
reached by the search
 We insert k at node w and
expand w into an internal node
 Example: insert 5
6
9
2
4
1 8
6
9
2
4
1 8
5
<
>
>
w
w
4. BINARY SEARCH TREE
 Insert new node
4. BINARY SEARCH TREE
 Insert new node
void InsertNode(node* &root,node *newnode)
{
if (root==NULL)
root=newnode;
else
if (root->data>newnode->data)
InsertNode(root->l,newnode);
else
if (root->data<newnode->data)
InsertNode(root->r,newnode);
}
4. BINARY SEARCH TREE
 traverse node
void preorder(node* r)
{
if (r!=NULL)
{ cout<<r->data<<" ";
inorder(r->l);
inorder(r->r);
}
}
4. BINARY SEARCH TREE
 traverse node
4. BINARY SEARCH TREE
 traverse node

More Related Content

Similar to tree.ppt

Tree Data Structure Tree Data Structure Details
Tree Data Structure Tree Data Structure DetailsTree Data Structure Tree Data Structure Details
Tree Data Structure Tree Data Structure Details
ssusera8c91a
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
AnuradhaJadiya1
 
Tree chapter
Tree chapterTree chapter
Tree chapter
LJ Projects
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
Revathiparamanathan
 
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
maamir farooq
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
Saurabh Mishra
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
kalyanineve
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
Guru Nanak Institute Of Tech
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
Aakash deep Singhal
 
Tree
TreeTree
Tree
bhumish
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
Varun Mahajan
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
AdityaK92
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
Suneel61
 
Binary tree
Binary  treeBinary  tree
Binary tree
Vanitha Chandru
 
Tree
TreeTree
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Getachew Ganfur
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
Rai University
 

Similar to tree.ppt (20)

Tree Data Structure Tree Data Structure Details
Tree Data Structure Tree Data Structure DetailsTree Data Structure Tree Data Structure Details
Tree Data Structure Tree Data Structure Details
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
Tree chapter
Tree chapterTree chapter
Tree chapter
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
 
Binary tree
Binary treeBinary tree
Binary tree
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Tree
TreeTree
Tree
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Tree
TreeTree
Tree
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 

More from wondmhunegn

sql-basic.ppt
sql-basic.pptsql-basic.ppt
sql-basic.ppt
wondmhunegn
 
Microsoft Access.ppt
Microsoft Access.pptMicrosoft Access.ppt
Microsoft Access.ppt
wondmhunegn
 
Module 2 Slides.ppt
Module 2 Slides.pptModule 2 Slides.ppt
Module 2 Slides.ppt
wondmhunegn
 
ETE105_lecture 3.ppt
ETE105_lecture 3.pptETE105_lecture 3.ppt
ETE105_lecture 3.ppt
wondmhunegn
 
BasicComputerParts.ppt
BasicComputerParts.pptBasicComputerParts.ppt
BasicComputerParts.ppt
wondmhunegn
 
Chapter 1 Data structure.pptx
Chapter 1 Data structure.pptxChapter 1 Data structure.pptx
Chapter 1 Data structure.pptx
wondmhunegn
 
IS230 - Chapter 4 - Keys and Relationship - Revised.ppt
IS230 - Chapter 4 - Keys and Relationship - Revised.pptIS230 - Chapter 4 - Keys and Relationship - Revised.ppt
IS230 - Chapter 4 - Keys and Relationship - Revised.ppt
wondmhunegn
 
MS-Access Tables Forms Queries Reports.ppt
MS-Access Tables Forms Queries Reports.pptMS-Access Tables Forms Queries Reports.ppt
MS-Access Tables Forms Queries Reports.ppt
wondmhunegn
 
DatabaseFundamentals.ppt
DatabaseFundamentals.pptDatabaseFundamentals.ppt
DatabaseFundamentals.ppt
wondmhunegn
 
Data Types and Field Properties.ppt
Data Types and Field Properties.pptData Types and Field Properties.ppt
Data Types and Field Properties.ppt
wondmhunegn
 
Template.pptx
Template.pptxTemplate.pptx
Template.pptx
wondmhunegn
 

More from wondmhunegn (11)

sql-basic.ppt
sql-basic.pptsql-basic.ppt
sql-basic.ppt
 
Microsoft Access.ppt
Microsoft Access.pptMicrosoft Access.ppt
Microsoft Access.ppt
 
Module 2 Slides.ppt
Module 2 Slides.pptModule 2 Slides.ppt
Module 2 Slides.ppt
 
ETE105_lecture 3.ppt
ETE105_lecture 3.pptETE105_lecture 3.ppt
ETE105_lecture 3.ppt
 
BasicComputerParts.ppt
BasicComputerParts.pptBasicComputerParts.ppt
BasicComputerParts.ppt
 
Chapter 1 Data structure.pptx
Chapter 1 Data structure.pptxChapter 1 Data structure.pptx
Chapter 1 Data structure.pptx
 
IS230 - Chapter 4 - Keys and Relationship - Revised.ppt
IS230 - Chapter 4 - Keys and Relationship - Revised.pptIS230 - Chapter 4 - Keys and Relationship - Revised.ppt
IS230 - Chapter 4 - Keys and Relationship - Revised.ppt
 
MS-Access Tables Forms Queries Reports.ppt
MS-Access Tables Forms Queries Reports.pptMS-Access Tables Forms Queries Reports.ppt
MS-Access Tables Forms Queries Reports.ppt
 
DatabaseFundamentals.ppt
DatabaseFundamentals.pptDatabaseFundamentals.ppt
DatabaseFundamentals.ppt
 
Data Types and Field Properties.ppt
Data Types and Field Properties.pptData Types and Field Properties.ppt
Data Types and Field Properties.ppt
 
Template.pptx
Template.pptxTemplate.pptx
Template.pptx
 

Recently uploaded

Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
Sm321
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
GetInData
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
apvysm8
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
g4dpvqap0
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
aqzctr7x
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
74nqk8xf
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
ahzuo
 
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
zsjl4mimo
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
AndrzejJarynowski
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
bopyb
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
roli9797
 

Recently uploaded (20)

Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
 
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
 

tree.ppt

  • 1. TREE  1. THE CONCEPT OF TREES  2. BINARY TREE AND REPRESENTATION  3. BINARY TREE TRAVERSAL  4. BINARY SEARCH TREE 6 9 2 4 1 8 < > =
  • 2. 1. THE CONCEPT OF TREES  A tree is a set of one or more nodes T: – there is a specially designated node called a root – The remaining nodes are partitioned into n disjointed set of nodes T1, T2,…,Tn, each of which is a tree.
  • 3. 1. THE CONCEPT OF TREES  Example
  • 4. 1. THE CONCEPT OF TREES  It’s not a tree Tree
  • 5. 1. THE CONCEPT OF TREES: Some terminology  Root  Child (left,right)  Parent  Leaf node  Subtree  Ancestor of a node  Descendant of a node
  • 6. 1. THE CONCEPT OF TREES  Degree of a Node of a Tree – The degree of a node of a tree is the number of subtrees having this node as a root.  Degree of a Tree – The degree of a tree is defined as the maximum of degree of the nodes of the tree  Level of a Node – level of the root node as 1, and incrementing it by 1 as we move from the root towards the subtrees.
  • 7. 2. BINARY TREE AND REPRESENTATION  BINARY TREE – no node can have a degree of more than 2. – The maximum number of nodes at level i will be 2i−1 – If k is the depth of the tree then the maximum number of nodes that the tree can have is – 2k − 1 = 2k−1 + 2k−2 + … + 20
  • 8. 2. BINARY TREE AND REPRESENTATION  BINARY TREE – A full binary tree is a binary of depth k having 2k − 1 nodes. – If it has < 2k − 1, it is not a full binary tree
  • 9. What is the height h of a full tree with N nodes?  The max height of a tree with N nodes is N (same as a linked list)  The min height of a tree with N nodes is log(N+1) 2 1 2 1 log( 1) (log ) h h N N h N O N  =  =   =  
  • 10. 2. BINARY TREE AND REPRESENTATION 3=22-1 7=23-1 15=24-1 full binary
  • 11. 2. BINARY TREE AND REPRESENTATION struct node { int data; node *left; node *right; };
  • 12. Tree traversal  Used to print out the data in a tree in a certain order – inorder (LDR ) – Postorder (LRD ) – preorder (DLR )  Pre-order traversal – Print the data at the root – Recursively print out all data in the left subtree – Recursively print out all data in the right subtree
  • 13. Preorder, Postorder and Inorder  Preorder traversal – node, left, right – prefix expression ++a*bc*+*defg
  • 14. Preorder, Postorder and Inorder  Postorder traversal – left, right, node – postfix expression abc*+de*f+g*+  Inorder traversal – left, node, right. – infix expression a+b*c+d*e+f*g
  • 16. 3. BINARY TREE TRAVERSAL
  • 17. 3. BINARY TREE TRAVERSAL Inorder = DBEAC Many trees
  • 18. 4. BINARY SEARCH TREE  A binary search tree – is a binary tree (may be empty) – every node must contain an identifier. – An identifier of any node in the left subtree is less than the identifier of the root. – An identifier of any node in the right subtree is greater than the identifier of the root. – Both the left subtree and right subtree are binary search trees.
  • 19. 4. BINARY SEARCH TREE  binary search tree.
  • 20. Binary Search Trees A binary search tree Not a binary search tree
  • 21. Performance  Consider a dictionary with n items implemented by means of a binary search tree of height h – the space used is O(n) – methods find, insert and remove take O(h) time  The height h is O(n) in the worst case and O(log n) in the best case
  • 22. 4. BINARY SEARCH TREE  Why using binary search tree – traverse in inorder: sorted list – searching becomes faster  But.. – Insert, delete: slow  Important thing: Index in Database system – Using the right way of Index property
  • 23. Search  To search for a key k, we trace a downward path starting at the root  The next node visited depends on the outcome of the comparison of k with the key of the current node  If we reach a leaf, the key is not found and we return nukk  Example: find(4): – Call TreeSearch(4,root) 6 9 2 4 1 8 < > =
  • 24. Insertion  To perform operation inser(k, o), we search for key k (using TreeSearch)  Assume k is not already in the tree, and let let w be the leaf reached by the search  We insert k at node w and expand w into an internal node  Example: insert 5 6 9 2 4 1 8 6 9 2 4 1 8 5 < > > w w
  • 25. 4. BINARY SEARCH TREE  Insert new node
  • 26. 4. BINARY SEARCH TREE  Insert new node void InsertNode(node* &root,node *newnode) { if (root==NULL) root=newnode; else if (root->data>newnode->data) InsertNode(root->l,newnode); else if (root->data<newnode->data) InsertNode(root->r,newnode); }
  • 27. 4. BINARY SEARCH TREE  traverse node void preorder(node* r) { if (r!=NULL) { cout<<r->data<<" "; inorder(r->l); inorder(r->r); } }
  • 28. 4. BINARY SEARCH TREE  traverse node
  • 29. 4. BINARY SEARCH TREE  traverse node