SlideShare a Scribd company logo
TREE-OPERATIONS
ASADULLAH TAREEN
asadullah.tareen@gmail.com
Ghazni University
Computer Science Department
©Sem-II-2017
ALGORITHM & DATA-
STRUCTURE
Outline
This Topic Will Cover Tree Operations:
 1. Insertion of a Node into a tree
 2. Deletion of a Node from a tree
 Cases:
2.1 Case: The node to be deleted is a leaf
2.2 Case: The node to be deleted has 1 child
2.3 Case: The node to be deleted has 2 children
Tree Operations
27-Sep-2017 2Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a Sorted List
5 8 9 13 21 44 45 46
1.1
Tree Operations
27-Sep-2017 3Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a Sorted List
5 8 9 13 21 44 45 46
If 14 is inserted, the list become:
5 8 9 13 14 21 44 45 46
1.1
Tree Operations
27-Sep-2017 4Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a Sorted List
5 8 9 13 21 44 45 46
If 14 is inserted, the list become:
5 8 9 13 14 21 44 45 46
1.1
Tree Operations
27-Sep-2017 5Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a Sorted List
5 8 9 13 21 44 45 46
If 14 is inserted, the list become:
5 8 9 13 14 21 44 45 46
FEATURES OF A SORTED LIST IS PRESERVED
1.1
Tree Operations
27-Sep-2017 6Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a Sorted List
5 8 9 13 21 44 45 46
If 14 is inserted, the list become:
5 8 9 13 14 21 44 45 46
Features of a Sorted List is Preserved
So
WHAT ARE THE FEATURES OF A SORTED LIST ????
1.1
Tree Operations
27-Sep-2017 7Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a BST
1.1
Tree Operations
27-Sep-2017 8Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a BST
1.1
Tree Operations
64
33
88
7 99
10
27-Sep-2017 9Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a BST
If 14 to be inserted
1.1
Tree Operations
64
33
88
7 99
10
27-Sep-2017 10Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a BST
If 14 to be inserted
1.1
Tree Operations
64
33
88
7 99
10
How does the new BST look like ?
27-Sep-2017 11Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a BST
If 14 to be inserted,
1.1
Tree Operations
64
33
88
7 99
10
How does the new BST look like ?
How do you do it?
27-Sep-2017 12Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Given a BST
Are the features of a BST preserved???
1.1
Tree Operations
64
33
88
7 99
10
14
27-Sep-2017 13Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Algorithm to Insert 14
1.1
Tree Operations
64
33
88
7 99
10
27-Sep-2017 14Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Algorithm to Insert 14
1.1
Tree Operations
64
33
88
7 99
10
14 < 64?
27-Sep-2017 15Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Algorithm to Insert 14
1.1
Tree Operations
64
33
88
7 99
10
14 < 10?
27-Sep-2017 16Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Algorithm to Insert 14
1.1
Tree Operations
64
33
88
7 99
10
14 < 33?
27-Sep-2017 17Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Algorithm to Insert 14
1.1
Tree Operations
64
33
88
7 99
10
NULL
27-Sep-2017 18Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Algorithm to Insert 14
1.1
Tree Operations
64
33
88
7 99
10
Insert here
14
27-Sep-2017 19Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Algorithm to Insert 14
1.1
Tree Operations
64
33
88
7 99
10
14
27-Sep-2017 20Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Now try to Insert 99
1.1
Tree Operations
64
33
88
7 99
10
14
99 < 64 ?
27-Sep-2017 21Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Now try to Insert 99
1.1
Tree Operations
64
33
88
7 99
10
14
99 < 88?
27-Sep-2017 22Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
Now try to Insert 99
1.1
Tree Operations
64
33
88
7 99
10
14
99 < 99 ?
27-Sep-2017 23Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
This algorithm is not so effective
Try building a BST using the Same algorithm for the
following Sequence:
K N G I C U
K U C I N G
C G I K N U
1.1
Tree Operations
27-Sep-2017 24Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
ALKHWARIZMI:
• Inserting Node in such algorithm is less effective (insert
algorithms described above to the following sequence:)
K N G I C U
K U C I N G
C G I K N U
1.1
Tree Operations
27-Sep-2017 25Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
This algorithm is not so effective
Try building a BST using the Same algorithm for the
following Sequence:
K N G I C U
K U C I N G
C G I K N U
It is difficult to produce a balanced Tree
One way is using the AVL Tree algorithm
1.1
Tree Operations
27-Sep-2017 26Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Insert Node
• Try to develop PGD ( Proper Generalized Decompositions) use insert
algorithms described above to the following sequence:
K N G I C U
K U C I N G
C G I K N U
• It is difficult to produce a balanced Tree
•  one way is using the AVL Tree algorithm
1.1
Tree Operations
27-Sep-2017 27Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node
3 Cases:
I. The node to be deleted is a leaf
II. The node to be deleted has 1 child
III. The node to be deleted has 2 children
2
Tree Operations
27-Sep-2017 28Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (1st Case)
To delete D:
2.1
Tree Operations
M
E
D
P
B VN
T ZA
27-Sep-2017 29Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (1st Case)
To delete D:
2.1
Tree Operations
M
E
D
P
B VN
T ZA
27-Sep-2017 30Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (1st Case)
To delete D:
2.1
Tree Operations
M
E
D
P
B VN
T ZA
Parent
27-Sep-2017 31Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (1st Case)
To delete D:
2.1
Tree Operations
M
E
D
P
B VN
T ZA
Parent
x
27-Sep-2017 32Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (1st Case)
To delete D:
2.1
Tree Operations
M
E
D
P
B VN
T ZA
Parent
x
Set the right child of Parent as null
27-Sep-2017 33Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (1st Case)
To delete D:
2.1
Tree Operations
M
E
D
P
B VN
T ZA
Parent
x
Set the right child of Parent as null
Free X
27-Sep-2017 34Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (1st Case)
To delete D:
2.1
Tree Operations
M
E P
B VN
T ZA
Parent
x
Set the right child of Parent as null
Free X
27-Sep-2017 35Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (2nd Case)
To delete E:
2.2
Tree Operations
M
E
D
P
B VN
T ZA
27-Sep-2017 36Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (2nd Case)
To delete E:
2.2
Tree Operations
M
E
D
P
B VN
T ZA
27-Sep-2017 37Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (2nd Case)
To delete E:
2.2
Tree Operations
M
E
D
P
B VN
T ZA
x
Parent
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
27-Sep-2017 38Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (2nd Case)
To delete E:
2.2
Tree Operations
M
E
D
P
B VN
T ZA
x
Parent
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
27-Sep-2017 39Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (2nd Case)
To delete E:
2.2
Tree Operations
M
E
D
P
B VN
T ZA
x
Parent
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
Free x
27-Sep-2017 40Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (2nd Case)
To delete E:
2.2
Tree Operations
M
D
P
B VN
T ZA
Parent
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
Free x
27-Sep-2017 41Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (2nd Case)
To delete E:
2.2
Tree Operations
M
D
PB
VN
T Z
A
Parent
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
Free x
27-Sep-2017 42Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (3rd Case)
To delete P:
2.3
Tree Operations
M
E
D
P
B VN
T ZA
27-Sep-2017 43Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (3rd Case)
To delete P:
2.3
Tree Operations
M
E
D
P
B VN
T ZA
Parent
x
27-Sep-2017 44Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (3rd Case)
To delete P:
2.3
Tree Operations
M
E
D
P
B VN
T ZA
Parent
xDetermine the next node
based on Inorder(LNR)
*Usually, the Inorder node has only one child or no children at all
27-Sep-2017 45Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (3rd Case)
To delete P:
2.3
Tree Operations
M
E
D
P
B VN
T ZA
Parent
xDetermine the next node
based on Inorder(LNR)
y
Parent of y
27-Sep-2017 46Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (3rd Case)
To delete P:
2.3
Tree Operations
M
E
D
P
B VN
T ZA
Parent
x
y
Parent of y
Copy
x->data = y->data
27-Sep-2017 47Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (3rd Case)
To delete P:
2.3
Tree Operations
M
E
D
P
B VN
T ZA
Parent
x
y
Parent of y
Copy
x->data = y->data
x = y
27-Sep-2017 48Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (3rd Case)
To delete P:
2.3
Tree Operations
M
E
D
P
B VN
T ZA
Parent
x
y
Parent of y
Delete T
27-Sep-2017 49Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (3rd Case)
To delete P:
2.3
Tree Operations
M
E
D
P
B VN
T ZA
Parent
x
y
Parent of y
Delete T (as in case 1)
parent_y->Lchild = NULL
27-Sep-2017 50Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (3rd Case)
To delete P:
2.3
Tree Operations
M
E
D
P
B VN
T ZA
Parent
y
Parent of y
Delete T (as in case 1)
parent_y->Lchild = NULL
27-Sep-2017 51Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Delete Node (3rd Case)
To delete P:
2.3
Tree Operations
M
E
D
T
B VN
ZA
Parent
Parent of y
Delete T (as in case 1)
parent_y->Lchild = NULL
27-Sep-2017 52Lecturer: Asadullah Tareen Ghazni University Computer Science Department
Your
?
27-Sep-2017 53Lecturer: Asadullah Tareen Ghazni University Computer Science Department

More Related Content

Recently uploaded

How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
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
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
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
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
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
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
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
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
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
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 

Recently uploaded (20)

How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
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
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
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
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
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
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
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
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 

Featured

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Tree Operations

  • 1. TREE-OPERATIONS ASADULLAH TAREEN asadullah.tareen@gmail.com Ghazni University Computer Science Department ©Sem-II-2017 ALGORITHM & DATA- STRUCTURE
  • 2. Outline This Topic Will Cover Tree Operations:  1. Insertion of a Node into a tree  2. Deletion of a Node from a tree  Cases: 2.1 Case: The node to be deleted is a leaf 2.2 Case: The node to be deleted has 1 child 2.3 Case: The node to be deleted has 2 children Tree Operations 27-Sep-2017 2Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 3. Insert Node Given a Sorted List 5 8 9 13 21 44 45 46 1.1 Tree Operations 27-Sep-2017 3Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 4. Insert Node Given a Sorted List 5 8 9 13 21 44 45 46 If 14 is inserted, the list become: 5 8 9 13 14 21 44 45 46 1.1 Tree Operations 27-Sep-2017 4Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 5. Insert Node Given a Sorted List 5 8 9 13 21 44 45 46 If 14 is inserted, the list become: 5 8 9 13 14 21 44 45 46 1.1 Tree Operations 27-Sep-2017 5Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 6. Insert Node Given a Sorted List 5 8 9 13 21 44 45 46 If 14 is inserted, the list become: 5 8 9 13 14 21 44 45 46 FEATURES OF A SORTED LIST IS PRESERVED 1.1 Tree Operations 27-Sep-2017 6Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 7. Insert Node Given a Sorted List 5 8 9 13 21 44 45 46 If 14 is inserted, the list become: 5 8 9 13 14 21 44 45 46 Features of a Sorted List is Preserved So WHAT ARE THE FEATURES OF A SORTED LIST ???? 1.1 Tree Operations 27-Sep-2017 7Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 8. Insert Node Given a BST 1.1 Tree Operations 27-Sep-2017 8Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 9. Insert Node Given a BST 1.1 Tree Operations 64 33 88 7 99 10 27-Sep-2017 9Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 10. Insert Node Given a BST If 14 to be inserted 1.1 Tree Operations 64 33 88 7 99 10 27-Sep-2017 10Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 11. Insert Node Given a BST If 14 to be inserted 1.1 Tree Operations 64 33 88 7 99 10 How does the new BST look like ? 27-Sep-2017 11Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 12. Insert Node Given a BST If 14 to be inserted, 1.1 Tree Operations 64 33 88 7 99 10 How does the new BST look like ? How do you do it? 27-Sep-2017 12Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 13. Insert Node Given a BST Are the features of a BST preserved??? 1.1 Tree Operations 64 33 88 7 99 10 14 27-Sep-2017 13Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 14. Insert Node Algorithm to Insert 14 1.1 Tree Operations 64 33 88 7 99 10 27-Sep-2017 14Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 15. Insert Node Algorithm to Insert 14 1.1 Tree Operations 64 33 88 7 99 10 14 < 64? 27-Sep-2017 15Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 16. Insert Node Algorithm to Insert 14 1.1 Tree Operations 64 33 88 7 99 10 14 < 10? 27-Sep-2017 16Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 17. Insert Node Algorithm to Insert 14 1.1 Tree Operations 64 33 88 7 99 10 14 < 33? 27-Sep-2017 17Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 18. Insert Node Algorithm to Insert 14 1.1 Tree Operations 64 33 88 7 99 10 NULL 27-Sep-2017 18Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 19. Insert Node Algorithm to Insert 14 1.1 Tree Operations 64 33 88 7 99 10 Insert here 14 27-Sep-2017 19Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 20. Insert Node Algorithm to Insert 14 1.1 Tree Operations 64 33 88 7 99 10 14 27-Sep-2017 20Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 21. Insert Node Now try to Insert 99 1.1 Tree Operations 64 33 88 7 99 10 14 99 < 64 ? 27-Sep-2017 21Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 22. Insert Node Now try to Insert 99 1.1 Tree Operations 64 33 88 7 99 10 14 99 < 88? 27-Sep-2017 22Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 23. Insert Node Now try to Insert 99 1.1 Tree Operations 64 33 88 7 99 10 14 99 < 99 ? 27-Sep-2017 23Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 24. Insert Node This algorithm is not so effective Try building a BST using the Same algorithm for the following Sequence: K N G I C U K U C I N G C G I K N U 1.1 Tree Operations 27-Sep-2017 24Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 25. Insert Node ALKHWARIZMI: • Inserting Node in such algorithm is less effective (insert algorithms described above to the following sequence:) K N G I C U K U C I N G C G I K N U 1.1 Tree Operations 27-Sep-2017 25Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 26. Insert Node This algorithm is not so effective Try building a BST using the Same algorithm for the following Sequence: K N G I C U K U C I N G C G I K N U It is difficult to produce a balanced Tree One way is using the AVL Tree algorithm 1.1 Tree Operations 27-Sep-2017 26Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 27. Insert Node • Try to develop PGD ( Proper Generalized Decompositions) use insert algorithms described above to the following sequence: K N G I C U K U C I N G C G I K N U • It is difficult to produce a balanced Tree •  one way is using the AVL Tree algorithm 1.1 Tree Operations 27-Sep-2017 27Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 28. Delete Node 3 Cases: I. The node to be deleted is a leaf II. The node to be deleted has 1 child III. The node to be deleted has 2 children 2 Tree Operations 27-Sep-2017 28Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 29. Delete Node (1st Case) To delete D: 2.1 Tree Operations M E D P B VN T ZA 27-Sep-2017 29Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 30. Delete Node (1st Case) To delete D: 2.1 Tree Operations M E D P B VN T ZA 27-Sep-2017 30Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 31. Delete Node (1st Case) To delete D: 2.1 Tree Operations M E D P B VN T ZA Parent 27-Sep-2017 31Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 32. Delete Node (1st Case) To delete D: 2.1 Tree Operations M E D P B VN T ZA Parent x 27-Sep-2017 32Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 33. Delete Node (1st Case) To delete D: 2.1 Tree Operations M E D P B VN T ZA Parent x Set the right child of Parent as null 27-Sep-2017 33Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 34. Delete Node (1st Case) To delete D: 2.1 Tree Operations M E D P B VN T ZA Parent x Set the right child of Parent as null Free X 27-Sep-2017 34Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 35. Delete Node (1st Case) To delete D: 2.1 Tree Operations M E P B VN T ZA Parent x Set the right child of Parent as null Free X 27-Sep-2017 35Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 36. Delete Node (2nd Case) To delete E: 2.2 Tree Operations M E D P B VN T ZA 27-Sep-2017 36Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 37. Delete Node (2nd Case) To delete E: 2.2 Tree Operations M E D P B VN T ZA 27-Sep-2017 37Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 38. Delete Node (2nd Case) To delete E: 2.2 Tree Operations M E D P B VN T ZA x Parent Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent 27-Sep-2017 38Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 39. Delete Node (2nd Case) To delete E: 2.2 Tree Operations M E D P B VN T ZA x Parent Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent 27-Sep-2017 39Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 40. Delete Node (2nd Case) To delete E: 2.2 Tree Operations M E D P B VN T ZA x Parent Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent Free x 27-Sep-2017 40Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 41. Delete Node (2nd Case) To delete E: 2.2 Tree Operations M D P B VN T ZA Parent Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent Free x 27-Sep-2017 41Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 42. Delete Node (2nd Case) To delete E: 2.2 Tree Operations M D PB VN T Z A Parent Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent Free x 27-Sep-2017 42Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 43. Delete Node (3rd Case) To delete P: 2.3 Tree Operations M E D P B VN T ZA 27-Sep-2017 43Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 44. Delete Node (3rd Case) To delete P: 2.3 Tree Operations M E D P B VN T ZA Parent x 27-Sep-2017 44Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 45. Delete Node (3rd Case) To delete P: 2.3 Tree Operations M E D P B VN T ZA Parent xDetermine the next node based on Inorder(LNR) *Usually, the Inorder node has only one child or no children at all 27-Sep-2017 45Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 46. Delete Node (3rd Case) To delete P: 2.3 Tree Operations M E D P B VN T ZA Parent xDetermine the next node based on Inorder(LNR) y Parent of y 27-Sep-2017 46Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 47. Delete Node (3rd Case) To delete P: 2.3 Tree Operations M E D P B VN T ZA Parent x y Parent of y Copy x->data = y->data 27-Sep-2017 47Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 48. Delete Node (3rd Case) To delete P: 2.3 Tree Operations M E D P B VN T ZA Parent x y Parent of y Copy x->data = y->data x = y 27-Sep-2017 48Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 49. Delete Node (3rd Case) To delete P: 2.3 Tree Operations M E D P B VN T ZA Parent x y Parent of y Delete T 27-Sep-2017 49Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 50. Delete Node (3rd Case) To delete P: 2.3 Tree Operations M E D P B VN T ZA Parent x y Parent of y Delete T (as in case 1) parent_y->Lchild = NULL 27-Sep-2017 50Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 51. Delete Node (3rd Case) To delete P: 2.3 Tree Operations M E D P B VN T ZA Parent y Parent of y Delete T (as in case 1) parent_y->Lchild = NULL 27-Sep-2017 51Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 52. Delete Node (3rd Case) To delete P: 2.3 Tree Operations M E D T B VN ZA Parent Parent of y Delete T (as in case 1) parent_y->Lchild = NULL 27-Sep-2017 52Lecturer: Asadullah Tareen Ghazni University Computer Science Department
  • 53. Your ? 27-Sep-2017 53Lecturer: Asadullah Tareen Ghazni University Computer Science Department