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

PPT
Data Structure: TREES
PDF
Heaps.pdf
PPT
Tree
PPT
Binary trees
PPT
Data structures lecture 04
PDF
Tree Data Structure by Daniyal Khan
PPT
Tree data structure
PPT
BINARY TREE REPRESENTATION.ppt
Data Structure: TREES
Heaps.pdf
Tree
Binary trees
Data structures lecture 04
Tree Data Structure by Daniyal Khan
Tree data structure
BINARY TREE REPRESENTATION.ppt

Similar to Tree Operations (20)

PPSX
data structure(tree operations)
PPTX
Binary tree operations in data structures
PPT
1.1 binary tree
PPT
Trees - Non Linear Data Structure
PPTX
Binary tree
PDF
Chapter 7 - Binary Search Tree in the context of DSA.pdf
PPTX
Binary Search Tree
PPTX
Data structure multiple choice qn REVISION+hashing.pptx
PPTX
Binary tree and operations
PPTX
PPTX
Trees in data structure
PPT
B trees and_b__trees
PPTX
trees in data structure
PPT
Introduction to data structure by anil dutt
PPT
b-tree.ppt
DOCX
Biary search Tree.docx
PPTX
unit 4 for trees data structure notes it is
PPT
part4-trees.ppt
data structure(tree operations)
Binary tree operations in data structures
1.1 binary tree
Trees - Non Linear Data Structure
Binary tree
Chapter 7 - Binary Search Tree in the context of DSA.pdf
Binary Search Tree
Data structure multiple choice qn REVISION+hashing.pptx
Binary tree and operations
Trees in data structure
B trees and_b__trees
trees in data structure
Introduction to data structure by anil dutt
b-tree.ppt
Biary search Tree.docx
unit 4 for trees data structure notes it is
part4-trees.ppt
Ad

Recently uploaded (20)

PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
Classroom Observation Tools for Teachers
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Anesthesia in Laparoscopic Surgery in India
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Yogi Goddess Pres Conference Studio Updates
STATICS OF THE RIGID BODIES Hibbelers.pdf
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
RMMM.pdf make it easy to upload and study
Microbial diseases, their pathogenesis and prophylaxis
Final Presentation General Medicine 03-08-2024.pptx
Microbial disease of the cardiovascular and lymphatic systems
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
Paper A Mock Exam 9_ Attempt review.pdf.
Chinmaya Tiranga quiz Grand Finale.pdf
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Classroom Observation Tools for Teachers
Ad

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