SlideShare a Scribd company logo
1 of 21
Binary Search Trees (10.1)
CSE 2011
Winter 2011
1
13 December 2022
Dictionary ADT (9.5.1)
• The dictionary ADT models a
searchable collection of key-
element items
• The main operations of a
dictionary are searching,
inserting, and deleting items
• Multiple items with the same key
are allowed
• Applications:
– address book
– credit card authorization
– SIN database
– student database
Dictionary ADT methods:
• get(k): if the dictionary has an
item with key k, returns its
element, else, returns NULL
• getAll(k): returns an iterator of
entries with key k
• put(k, o): inserts item (k, o)
into the dictionary
• remove(k): if the dictionary
has an item with key k,
removes it from the dictionary
and returns its element, else
returns NULL
• removeAll(k): remove all
entries with key k; return an
iterator of these entries.
• size(), isEmpty()
2
Binary Search Trees
• A binary search tree is a
binary tree storing keys (or
key-element pairs) at its
internal nodes and satisfying
the following property:
Let u, v, and w be three
nodes such that u is in the
left subtree of v and w is in
the right subtree of v. We
have
key(u)  key(v)  key(w)
• External nodes (dummies)
do not store items (non-
empty proper binary trees,
for coding simplicity)
• An inorder traversal of a
binary search trees visits the
keys in increasing order
• The left-most child has the
smallest key
• The right-most child has the
largest key
6
9
2
4
1 8
3
4
Example of BST
A binary search tree Not a binary search tree
5
More Examples of BST
• Average depth of a node is O(logN).
• Maximum depth of a node is O(N).
• Where is the smallest key? largest key?
The same set of keys may have different BSTs.
6
Inorder Traversal of BST
• Inorder traversal of BST prints out all the keys in
sorted order.
Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
7
Searching BST
• If we are searching for 15, then we are done.
• If we are searching for a key < 15, then we should
search in the left subtree.
• If we are searching for a key > 15, then we should
search in the right subtree.
8
Search Algorithm
• 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 v (where the key
should be if it will be
inserted)
• Example:
TreeSearch(4, T.root())
• Running time: ?
Algorithm TreeSearch( k, v )
if T.isExternal (v)
return (v); // or return NO_SUCH_KEY
if k < key(v)
return TreeSearch( k, T.left(v) )
else if k = key(v)
return v
else { k > key(v) }
return TreeSearch( k, T.right(v) )
6
9
2
4
1 8
<
>
=
9
Insertion (distinct keys)
• To perform operation
insertItem(k, o), we search
for key k
• Assume k is not already in
the tree, and let w be the
leaf reached by the search
• We insert k at node w and
expand w into an internal
node using
insertAtExternal(w, (k,e))
• Example:
insertAtExternal(w, (5,e))
with e having key 5
• Running time: ?
6
9
2
4
1 8
6
9
2
4
1 8
5
<
>
>
w
w
10
Insertion Algorithm (distinct keys)
Algorithm TreeInsert( k, e, v ) {
w = TreeSearch( k, v );
T.insertAtExternal( w, k, e );
return w;
}
Algorithm insertAtExternal( w, k, e ) {
if ( T.isExternal( w ) {
make w an internal node, store k and e into w;
add two dummy nodes (leaves) as w’s children;
} else { error condition };
}
• First call: TreeInsert( 5, e, T.root( ) ) 11
Insertion (duplicate keys)
Insertion with duplicate keys
• Example: insert(2)
• Call TreeSearch(k,
leftChild(w)) to find the leaf
node for insertion
• Can insert to either the left
subtree or the right subtree
(call TreeSearch(k,
rightChild(w))
Running time: ?
Homework: implement method
getAll(k)
12
Insertion Algorithm (duplicate keys)
Algorithm TreeInsert( k, e, v ) {
w = TreeSearch( k, v );
if k == key(w) // key exists
return TreeInsert( k, e, T.left( w ) ); // ***
T.insertAtExternal( w, k, e );
return w;
}
• First call: TreeInsert( 2, e, T.root() )
***Note: if inserting the duplicate key into the left subtree,
keep searching the left subtree after a key has been found.
13
Deletion
• To perform operation removeElement(k), we
search for key k
• Assume key k is in the tree, and let v be the
node storing k
• Two cases:
– Case 1: v has no children
– Case 2: v has exactly one child
– Case 3: v has two children
Deletion: Case 1
• Case 1: v has no
children
• We simply remove v
and its 2 dummy
leaves.
• Replace v by a dummy
node.
• Example: remove 5
6
9
2
4
1 8
5
6
9
2
1 8
15
Deletion: Case 2
• Case 1: v has exactly
one child
• v’s parent will “adopt”
v’s child.
• We connect v’s parent
to v’s child, effectively
removing v and the
dummy node w from the
tree.
• Done by method
removeExternal(w)
• Example: remove 4
6
9
2
4
1 8
5
v
w
6
9
2
5
1 8
16
Deletion: Case 3
• Case 3: v has two children (and possibly grandchildren,
great-grandchildren, etc.)
• Identify v’s “heir”: either one of the following two nodes:
– the node x that immediately precedes v in an inorder
traversal (right-most node in v’s left subtree)
– the node x that immediately follows v in an inorder
traversal (left-most node in v’s right subtree)
• Two steps:
– copy content of x into node v (heir “inherits” node v);
– remove x from the tree (use either case 1 or case 2
above).
Deletion: Case 3 Example
• Example: remove 3
• Heir = ?
• Running time of deletion
algorithm: ?
• Homework: implement
removeAll(k)
3
1
8
6 9
5
v
x
2
5
1
8
6 9
v
2
18
Notes
• Two steps of case 3:
– copy content of x into node v (heir “inherits” node v);
– remove x from the tree
• if x has no child: call case 1
• if x has one child: call case 2
• x cannot have two children (why?)
• Both cases 1 and 2 can be merged into one and
implemented by method removeExternal().
19
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 get(k) , put()
and remove(k) take O(h)
time
• The height h is O(n) in
the worst case and
O(log n) in the best
case
20
Next time …
• AVL trees (10.2)
• BST Java code: section 10.1.3
21

More Related Content

Similar to s11_bin_search_trees.ppt

Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
Unit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptUnit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptplagcheck
 
Introduction to Ruby Programming Language
Introduction to Ruby Programming LanguageIntroduction to Ruby Programming Language
Introduction to Ruby Programming LanguageNicolò Calcavecchia
 
Review session2
Review session2Review session2
Review session2NEEDY12345
 
lecture 12
lecture 12lecture 12
lecture 12sajinsc
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingMandeep Singh
 
Week09
Week09Week09
Week09hccit
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Getachew Ganfur
 
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docxajoy21
 
ADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_AllocationADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_AllocationHemanth Kumar
 
Dancing Links: an educational pearl
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearlESUG
 
lecture 11
lecture 11lecture 11
lecture 11sajinsc
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docxfredharris32
 

Similar to s11_bin_search_trees.ppt (20)

Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Unit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptUnit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.ppt
 
Introduction to Ruby Programming Language
Introduction to Ruby Programming LanguageIntroduction to Ruby Programming Language
Introduction to Ruby Programming Language
 
02 stackqueue
02 stackqueue02 stackqueue
02 stackqueue
 
Review session2
Review session2Review session2
Review session2
 
lecture 12
lecture 12lecture 12
lecture 12
 
4.2 bst 02
4.2 bst 024.2 bst 02
4.2 bst 02
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented Programming
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Week09
Week09Week09
Week09
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
2 4 Tree
2 4 Tree2 4 Tree
2 4 Tree
 
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
 
ADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_AllocationADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_Allocation
 
Dancing Links: an educational pearl
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearl
 
lecture 11
lecture 11lecture 11
lecture 11
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Queue
QueueQueue
Queue
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
 

Recently uploaded

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 

Recently uploaded (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 

s11_bin_search_trees.ppt

  • 1. Binary Search Trees (10.1) CSE 2011 Winter 2011 1 13 December 2022
  • 2. Dictionary ADT (9.5.1) • The dictionary ADT models a searchable collection of key- element items • The main operations of a dictionary are searching, inserting, and deleting items • Multiple items with the same key are allowed • Applications: – address book – credit card authorization – SIN database – student database Dictionary ADT methods: • get(k): if the dictionary has an item with key k, returns its element, else, returns NULL • getAll(k): returns an iterator of entries with key k • put(k, o): inserts item (k, o) into the dictionary • remove(k): if the dictionary has an item with key k, removes it from the dictionary and returns its element, else returns NULL • removeAll(k): remove all entries with key k; return an iterator of these entries. • size(), isEmpty() 2
  • 3. Binary Search Trees • A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u)  key(v)  key(w) • External nodes (dummies) do not store items (non- empty proper binary trees, for coding simplicity) • An inorder traversal of a binary search trees visits the keys in increasing order • The left-most child has the smallest key • The right-most child has the largest key 6 9 2 4 1 8 3
  • 4. 4 Example of BST A binary search tree Not a binary search tree
  • 5. 5 More Examples of BST • Average depth of a node is O(logN). • Maximum depth of a node is O(N). • Where is the smallest key? largest key? The same set of keys may have different BSTs.
  • 6. 6 Inorder Traversal of BST • Inorder traversal of BST prints out all the keys in sorted order. Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
  • 7. 7 Searching BST • If we are searching for 15, then we are done. • If we are searching for a key < 15, then we should search in the left subtree. • If we are searching for a key > 15, then we should search in the right subtree.
  • 8. 8
  • 9. Search Algorithm • 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 v (where the key should be if it will be inserted) • Example: TreeSearch(4, T.root()) • Running time: ? Algorithm TreeSearch( k, v ) if T.isExternal (v) return (v); // or return NO_SUCH_KEY if k < key(v) return TreeSearch( k, T.left(v) ) else if k = key(v) return v else { k > key(v) } return TreeSearch( k, T.right(v) ) 6 9 2 4 1 8 < > = 9
  • 10. Insertion (distinct keys) • To perform operation insertItem(k, o), we search for key k • Assume k is not already in the tree, and let w be the leaf reached by the search • We insert k at node w and expand w into an internal node using insertAtExternal(w, (k,e)) • Example: insertAtExternal(w, (5,e)) with e having key 5 • Running time: ? 6 9 2 4 1 8 6 9 2 4 1 8 5 < > > w w 10
  • 11. Insertion Algorithm (distinct keys) Algorithm TreeInsert( k, e, v ) { w = TreeSearch( k, v ); T.insertAtExternal( w, k, e ); return w; } Algorithm insertAtExternal( w, k, e ) { if ( T.isExternal( w ) { make w an internal node, store k and e into w; add two dummy nodes (leaves) as w’s children; } else { error condition }; } • First call: TreeInsert( 5, e, T.root( ) ) 11
  • 12. Insertion (duplicate keys) Insertion with duplicate keys • Example: insert(2) • Call TreeSearch(k, leftChild(w)) to find the leaf node for insertion • Can insert to either the left subtree or the right subtree (call TreeSearch(k, rightChild(w)) Running time: ? Homework: implement method getAll(k) 12
  • 13. Insertion Algorithm (duplicate keys) Algorithm TreeInsert( k, e, v ) { w = TreeSearch( k, v ); if k == key(w) // key exists return TreeInsert( k, e, T.left( w ) ); // *** T.insertAtExternal( w, k, e ); return w; } • First call: TreeInsert( 2, e, T.root() ) ***Note: if inserting the duplicate key into the left subtree, keep searching the left subtree after a key has been found. 13
  • 14. Deletion • To perform operation removeElement(k), we search for key k • Assume key k is in the tree, and let v be the node storing k • Two cases: – Case 1: v has no children – Case 2: v has exactly one child – Case 3: v has two children
  • 15. Deletion: Case 1 • Case 1: v has no children • We simply remove v and its 2 dummy leaves. • Replace v by a dummy node. • Example: remove 5 6 9 2 4 1 8 5 6 9 2 1 8 15
  • 16. Deletion: Case 2 • Case 1: v has exactly one child • v’s parent will “adopt” v’s child. • We connect v’s parent to v’s child, effectively removing v and the dummy node w from the tree. • Done by method removeExternal(w) • Example: remove 4 6 9 2 4 1 8 5 v w 6 9 2 5 1 8 16
  • 17. Deletion: Case 3 • Case 3: v has two children (and possibly grandchildren, great-grandchildren, etc.) • Identify v’s “heir”: either one of the following two nodes: – the node x that immediately precedes v in an inorder traversal (right-most node in v’s left subtree) – the node x that immediately follows v in an inorder traversal (left-most node in v’s right subtree) • Two steps: – copy content of x into node v (heir “inherits” node v); – remove x from the tree (use either case 1 or case 2 above).
  • 18. Deletion: Case 3 Example • Example: remove 3 • Heir = ? • Running time of deletion algorithm: ? • Homework: implement removeAll(k) 3 1 8 6 9 5 v x 2 5 1 8 6 9 v 2 18
  • 19. Notes • Two steps of case 3: – copy content of x into node v (heir “inherits” node v); – remove x from the tree • if x has no child: call case 1 • if x has one child: call case 2 • x cannot have two children (why?) • Both cases 1 and 2 can be merged into one and implemented by method removeExternal(). 19
  • 20. 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 get(k) , put() and remove(k) take O(h) time • The height h is O(n) in the worst case and O(log n) in the best case 20
  • 21. Next time … • AVL trees (10.2) • BST Java code: section 10.1.3 21