Chapter 4: Trees (BST)
Text: Read Weiss, §4.3
1
The Search Tree ADT – Binary Search Trees
2
• An important application of binary trees is in searching. Let us
assume that each node in the tree stores an item. Assume for
simplicity that these are distinct integers (deal with duplicates
later).
• The property that makes a binary tree into a binary search
tree is that for every node, X, in the tree, the values of all the
items in the left subtree are smaller than the item in X, and the
values of items in the right subtree are larger than the item in
X.
The tree on the left is a
binary search tree, but the
tree on the right is not. The
tree on the right has a
node with key 7 in the left
subtree of a node with key
6 (which happens to be the
root).
3
Binary Search Trees - Operations
Descriptions and implementations of the
operations that are usually performed on binary
search trees (BST) are given.
Note that because of the recursive definition
of trees, it is common to write these routines
recursively. Because the average depth of a
binary search tree is O(log N), we generally do
not need to worry about running out of stack
space.
Since all the elements can be ordered, we will
assume that the operators <, >, and = can be
applied to them.
BST – Implementation - I
4
typedef int ElementType;
struct TreeNode;
typedef struct TreeNode *Position;
typedef struct TreeNode *SearchTree;
struct TreeNode{
ElementType Element;
SearchTree Left;
SearchTree Right;
};
SearchTree MakeEmpty(SearchTree T){
if( T != NULL ){
MakeEmpty( T->Left );
MakeEmpty( T->Right );
free( T );
}
return NULL;
}
Notice that NULL is
returned at the end.
5
• Find operation generally requires returning a pointer to the node in
the Binary Search Tree pointed by T that has value X, or NULL if there
is no such node. The structure of the tree makes this simple. If T is
NULL , then we can just return . Otherwise, we make a recursive call
on either the left or the right subtree of the node pointed by T,
depending on the relationship of X to the value stored in the node
pointed by T. Otherwise, if the value stored X, we can return T.
Position Find(ElementType X, SearchTree T){
if( T == NULL )
return NULL;
if( X < T->Element )
return Find( X, T->Left );
else if( X > T->Element )
return Find( X, T->Right );
else
return T;
}
BST – Implementation - II
6
Position FindMin(SearchTree T){
if( T == NULL )
return NULL;
else if( T->Left == NULL )
return T;
else
return FindMin( T->Left );
}
Position FindMax(SearchTree T){
if( T != NULL )
while( T->Right != NULL )
T = T->Right;
return T;
}
BST – Implementation - III
•To perform a FindMin, start at
the root and go left as long as
there is a left child. The stopping
point is the smallest element.
•The FindMax routine is the
same, except that branching is to
the right child.
•Notice that the degenerate case
of an empty tree is carefully
handled.
•Also notice that it is safe to
change T in FindMax, since we
are only working with a copy.
Always be extremely careful,
however, because a statement
such as
T->right=T->right->right
will make changes.
7
BST – Implementation – Insertion I
The insertion routine is conceptually simple. To insert X into tree T,
proceed down the tree as you would with a Find. If X is found, do
nothing (or "update" something). Otherwise, insert X at the last spot
on the path traversed. Duplicates can be handled by keeping an extra
field in the node indicating the frequency of occurrence. If the key is
only part of a larger record, then all of the records with the same key
might be kept in an auxiliary data structure, such as a list or another
search tree.
→
Insert node 5
8
SearchTree Insert(ElementType X, SearchTree T){
if( T == NULL ){
/* Create and return a one-node tree */
T = malloc( sizeof( struct TreeNode ) );
if( T == NULL )
FatalError( "Out of space!!!" );
else {
T->Element = X;
T->Left = T->Right = NULL;
}
}
else if( X < T->Element )
T->Left = Insert( X, T->Left );
else if( X > T->Element )
T->Right = Insert( X, T->Right );
/* Else X is in the tree already; do nothing */
return T; /* Do not forget this line!! */
}
BST – Implementation – Insertion II
9
BST – Implementation – Deletion I
• Once we have found the node to be deleted, we need to
consider 3 possibilities.
(1) If the node is a leaf, it can be deleted immediately.
(2) If the node has one child, the node can be deleted after its
parent adjusts a pointer to bypass the node. Notice that the
deleted node is now unreferenced and can be disposed of
only if a pointer to it has been saved.
→
Delete node 4
10
BST – Implementation – Deletion II
(3) The complicated case deals with a node with two
children. The general strategy is to replace the key of this
node with the smallest key of the right subtree (easy) and
recursively delete that node (which is now empty).
Because the smallest node in the right subtree cannot
have a left child, the second delete is an easy one.
→
Delete node 2
11
BST – Implementation – Deletion III
SearchTree Delete(ElementType X, SearchTree T){
Position TmpCell; /* declare a pointer */
if( T == NULL )
Error( "Element not found" );
else if( X < T->Element ) /* Go left */
T->Left = Delete( X, T->Left );
else if( X > T->Element ) /* Go right */
T->Right = Delete( X, T->Right );
else if( T->Left && T->Right ){ /* Found, it has 2 children */
TmpCell = FindMin( T->Right ); /* smallest in the right */
T->Element = TmpCell->Element; /* Replace with smallest */
T->Right = Delete( T->Element, T->Right );
}
else { /* Found, it has one or zero children */
TmpCell = T;
T = (T->Left)?T->Left:T->Right;/* Also handles 0 children */
free( TmpCell );
}
return T;
}
Inefficient, since calls highlighted in yellow
result in two passes down the tree to find and
delete the smallest node in the right subtree.
• We can use stacks to convert an
expression in standart form (otherwise
known as infix) into postfix.
• Example: operators = {+, *, (, )}, usual
precedence rules; a + b * c + (d * e + f) * g
Answer = a b c * + d e * f + g * +
12
BST – Implementation – Deletion IV
SearchTree Delete(ElementType X, SearchTree T){
Position TmpCell, TmpPrevCell; /* declare another pointer */
...
else if( T->Left && T->Right ){ /* Found, it has 2 children */
TmpCell = T->Right; /* to point to smallest in the right */
TmpPrevCell = T->Right; /* to point to parent of TmpCell */
while (TmpCell->Left != NULL){/* find smallest of right */
TmpPrevCell = TmpCell;
TmpCell = TmpCell->Left;
}
T->Element = TmpCell->Element;/* replace with smallest */
if (TmpCell == TmpPrevCell) /* T->Right is smallest */
T->Right = TmpCell->Right; /* skip over TmpCell */
else /* connect Left of TmpPrevCell to Right of TmpCell */
TmpPrevCell->Left = TmpCell->Right;
free (TmpCell);
}
...
}
Efficient Version
BST – Implementation – Lazy Deletion
• If the number of deletions is small, then a popular
strategy to use is lazy deletion: When an element is
to be deleted, it is left in the tree and merely marked
as deleted. This is especially popular if duplicates are
present, because then the field that keeps count of
the items can be decremented.
• If the number of real nodes is the same as the
number of "deleted" nodes, then the depth of the tree
is only expected to go up by a small constant (why?),
so there is a very small time penalty associated with
lazy deletion. Also, if an item is reinserted, the
overhead of allocating a new cell is avoided.
13
Average-Case Analysis - I
14
• All of the operations of BST, except MakeEmpty, take O(d)
time where d is the depth of the node containing the accessed
key. As a result, they are O (depth of tree).
• Why? Because in constant time we descend a level in the
tree, thus operating on a tree that is now roughly half as large.
• MakeEmpty take O(N) time.
• Observation: The average depth over all nodes in a BST is
O(log N) assuming all insertion sequences are equally likely.
• Proof: The sum of the depths of all nodes in a tree is the
internal path length. Let’s calculate the average internal path
length over all possible insertion sequences.
15
Average-Case Analysis - II
• Let D(N) be the internal path length for some BST T of N
nodes. D(1) = 0.
• D(N) = D(i) + D(N-i-1) + N -1 // Subtree nodes are 1 level deeper
• All subtree sizes are equally likely for BSTs, since it depends
only on the rank of the first element inserted into BST. This
does not hold for binary trees though. Let’s, then, average:
1)()/2()(
0),1)1()(()/1()(
1
0
1
0
−+



=
<≤∀−+−−+=
∑
∑
−
=
−
=
NiDNND
NiiNiNDiDNND
N
i
N
i
• If the recurrence is solved, D(N) = O(N log N). Thus, the
expected depth of any node is O(log N).
Derivation of D(N) - 1
16
1)()/2()(
0),1)1()(()/1()(
1
0
1
0
−+





=
<≤∀−+−−+=
∑
∑
−
=
−
=
NiDNND
NiiNiNDiDNND
N
i
N
i
)1(2)1(2)1()1()(
)2)...(2)(1()(2)1()1(
)1..().........1()(2)(
2
0
1
0
−+−=−−−
−−+





=−−
−+





=
∑
∑
−
=
−
=
NNDNDNNND
NNiDNDN
NNiDNND
N
i
N
i
∑=
+
−
+=+
+=
−
−
+−−=−
+
−
+−=+
−+−+=
N
i
ii
i
DNND
DD
NN
N
NNDNND
NN
N
NNDNND
NNDNNND
2
)1(
1
22/)1()1/()(
3*2
1*2
2/)1(3/)2(
...
)1(
)2(2
)1/()2(/)1(
)1(
)1(2
/)1()1/()(
)1(2)1()1()(
...(sum the equations side by side)
.....(divide by N(N+1))
.....(subtract (2) from (1))
17
Derivation of D(N) - 2
∑∑
∑
==
=
+
−
+
=+
+
−
+=+
N
i
N
i
N
i
iii
NND
ii
i
DNND
22
2
)1(
1
2
1
1
2)1/()(
)1(
1
22/)1()1/()(
∑=
+
−−+++++=+
N
i
ii
NNNND
2
)
1
11
(2))1/(1/1...4/13/1(2)1/()(
)1/(21)1/(2)2/3(log2)1/()(
))1/(12/1(2))1/(1)2/3((log2)1/()(
))1/(12/1(2))1/(1)/1...4/13/1((2)1/()(
++−+++−=+
+−−+++−=+
+−−+++++=+
NNNNND
NNNNND
NNNNND
e
e
γ
γ
4)1(2)1(4log)1(2)(
)1/(424log2)1/()(
++++−+=
+++−=+
NNNNND
NNNND
e
e
γ
γ
)log()(
)
log
log
()(
)log()(
4)1(2)1(4log)1(2)(
2
2
NNOND
e
N
NOND
NNOND
NNNNND
e
e
=
=
=
++++−+= γ
18
Average-Case Analysis - III
• As an example, the randomly generated 500 node BST has
nodes at expected depth 9.98.
19
Average-Case Analysis - IV
)( Nθ
After a quarter-million random
insert/remove pairs, right-heavy tree
on the previous slide, looks decidedly
unbalanced and average depth
becomes 12.51.
• Deletion algorithm described favors making left subtrees
deeper than the right (a deleted node is replaced with a node
from the right). The exact effect of this still unknown, but if
insertions and deletions are alternated Ɵ(N2) times, expected
depth is .In the absence of deletions or when lazy
deletion is used; average running times for BST
operations are O(log N).

More Related Content

PPT
binary search tree
PPTX
Trees (data structure)
PDF
Skiena algorithm 2007 lecture05 dictionary data structure trees
PPT
BINARY SEARCH TREE
PDF
Binary Tree
PPT
Binary searchtrees
PDF
On fixed point theorems in fuzzy metric spaces in integral type
binary search tree
Trees (data structure)
Skiena algorithm 2007 lecture05 dictionary data structure trees
BINARY SEARCH TREE
Binary Tree
Binary searchtrees
On fixed point theorems in fuzzy metric spaces in integral type

What's hot (20)

PDF
Tree and binary tree
PPT
Chapter 5 ds
PPT
Review session2
PPTX
Binary Search Tree
PPT
1.5 binary search tree
PPT
1.1 binary tree
PPTX
Binary tree and Binary search tree
PPT
Chapter 9 ds
PPT
lecture 12
PPT
Binary Search Tree
PPTX
Binary Tree in Data Structure
PPT
Chapter 3 ds
PDF
Sienna 7 heaps
PPTX
Trees in data structure
PPT
lecture 11
PPTX
Binary trees1
PDF
Algorithm chapter 6
PPT
Data Structure In C#
DOCX
Master of Computer Application (MCA) – Semester 4 MC0080
PPT
Topic11 sortingandsearching
Tree and binary tree
Chapter 5 ds
Review session2
Binary Search Tree
1.5 binary search tree
1.1 binary tree
Binary tree and Binary search tree
Chapter 9 ds
lecture 12
Binary Search Tree
Binary Tree in Data Structure
Chapter 3 ds
Sienna 7 heaps
Trees in data structure
lecture 11
Binary trees1
Algorithm chapter 6
Data Structure In C#
Master of Computer Application (MCA) – Semester 4 MC0080
Topic11 sortingandsearching

Similar to 8 chapter4 trees_bst (20)

PPTX
DS - BST
PPTX
CS8391-Data Structures Unit 3
PPT
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
PPT
Binary Search Tree
PPT
Binary search tree(bst)
PPTX
Data Strcutres-Non Linear DS-Advanced Trees
PDF
PPTX
Lecture 9 data structures and algorithms
PPTX
BST.pptx this isp used for learning binary search trees
PPTX
BST.pptx this is Good for data structure
PPTX
Lecture_10 - Revised.pptx
DOCX
5220191CS146 Data Structures and AlgorithmsC.docx
PPTX
8.binry search tree
PDF
Lecture notes data structures tree
DOCX
Required to augment the authors Binary Search Tree (BST) code to .docx
PPT
Binary Search Tree and AVL
PPT
Fundamentals of data structures
DOCX
Biary search Tree.docx
PPT
Binary search trees
PPTX
presentation 1 binary search tree in data structures.pptx
DS - BST
CS8391-Data Structures Unit 3
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
Binary Search Tree
Binary search tree(bst)
Data Strcutres-Non Linear DS-Advanced Trees
Lecture 9 data structures and algorithms
BST.pptx this isp used for learning binary search trees
BST.pptx this is Good for data structure
Lecture_10 - Revised.pptx
5220191CS146 Data Structures and AlgorithmsC.docx
8.binry search tree
Lecture notes data structures tree
Required to augment the authors Binary Search Tree (BST) code to .docx
Binary Search Tree and AVL
Fundamentals of data structures
Biary search Tree.docx
Binary search trees
presentation 1 binary search tree in data structures.pptx

More from SSE_AndyLi (17)

PDF
Chapter 07 Digital Alrithmetic and Arithmetic Circuits
PDF
Chapter 06 Combinational Logic Functions
PDF
Chapter 5 introduction to VHDL
PDF
Chapter 03 Boolean Algebra and Combinational Logic
PDF
Chapter 02 Logic Functions and Gates
PDF
Chapter 01 Basic Principles of Digital Systems
PDF
15 chapter9 graph_algorithms_mst
PDF
14 chapter9 graph_algorithmstopologicalsort_shortestpath
PDF
10 chapter6 heaps_priority_queues
PDF
9 chapter4 trees_avl
PDF
7 chapter4 trees_binary
PDF
6 chapter3 list_stackqueuepart3
PDF
5 chapter3 list_stackqueuepart2
PDF
4 chapter3 list_stackqueuepart1
PDF
3 chapter2 algorithm_analysispart2
PDF
2 chapter2 algorithm_analysispart1
PDF
1 chapter1 introduction
Chapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 06 Combinational Logic Functions
Chapter 5 introduction to VHDL
Chapter 03 Boolean Algebra and Combinational Logic
Chapter 02 Logic Functions and Gates
Chapter 01 Basic Principles of Digital Systems
15 chapter9 graph_algorithms_mst
14 chapter9 graph_algorithmstopologicalsort_shortestpath
10 chapter6 heaps_priority_queues
9 chapter4 trees_avl
7 chapter4 trees_binary
6 chapter3 list_stackqueuepart3
5 chapter3 list_stackqueuepart2
4 chapter3 list_stackqueuepart1
3 chapter2 algorithm_analysispart2
2 chapter2 algorithm_analysispart1
1 chapter1 introduction

Recently uploaded (20)

PPTX
UNIT II: Software design, software .pptx
PPTX
Beige and Black Minimalist Project Deck Presentation (1).pptx
PDF
WhatsApp Chatbots The Key to Scalable Customer Support.pdf
PDF
How to Set Realistic Project Milestones and Deadlines
PPTX
Streamlining Project Management in the AV Industry with D-Tools for Zoho CRM ...
PPTX
WJQSJXNAZJVCVSAXJHBZKSJXKJKXJSBHJBJEHHJB
PDF
C language slides for c programming book by ANSI
PDF
OpenImageIO Virtual Town Hall - August 2025
PDF
Mobile App for Guard Tour and Reporting.pdf
PPTX
StacksandQueuesCLASS 12 COMPUTER SCIENCE.pptx
PPTX
FLIGHT TICKET API | API INTEGRATION PLATFORM
PDF
Multiverse AI Review 2025_ The Ultimate All-in-One AI Platform.pdf
PDF
How to Write Automated Test Scripts Using Selenium.pdf
PDF
OpenEXR Virtual Town Hall - August 2025
PPTX
Lesson-3-Operation-System-Support.pptx-I
PPT
3.Software Design for software engineering
PPTX
MCP empowers AI Agents from Zero to Production
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
PDF
MaterialX Virtual Town Hall - August 2025
PDF
Top AI Tools for Project Managers: My 2025 AI Stack
UNIT II: Software design, software .pptx
Beige and Black Minimalist Project Deck Presentation (1).pptx
WhatsApp Chatbots The Key to Scalable Customer Support.pdf
How to Set Realistic Project Milestones and Deadlines
Streamlining Project Management in the AV Industry with D-Tools for Zoho CRM ...
WJQSJXNAZJVCVSAXJHBZKSJXKJKXJSBHJBJEHHJB
C language slides for c programming book by ANSI
OpenImageIO Virtual Town Hall - August 2025
Mobile App for Guard Tour and Reporting.pdf
StacksandQueuesCLASS 12 COMPUTER SCIENCE.pptx
FLIGHT TICKET API | API INTEGRATION PLATFORM
Multiverse AI Review 2025_ The Ultimate All-in-One AI Platform.pdf
How to Write Automated Test Scripts Using Selenium.pdf
OpenEXR Virtual Town Hall - August 2025
Lesson-3-Operation-System-Support.pptx-I
3.Software Design for software engineering
MCP empowers AI Agents from Zero to Production
Understanding the Need for Systemic Change in Open Source Through Intersectio...
MaterialX Virtual Town Hall - August 2025
Top AI Tools for Project Managers: My 2025 AI Stack

8 chapter4 trees_bst

  • 1. Chapter 4: Trees (BST) Text: Read Weiss, §4.3 1
  • 2. The Search Tree ADT – Binary Search Trees 2 • An important application of binary trees is in searching. Let us assume that each node in the tree stores an item. Assume for simplicity that these are distinct integers (deal with duplicates later). • The property that makes a binary tree into a binary search tree is that for every node, X, in the tree, the values of all the items in the left subtree are smaller than the item in X, and the values of items in the right subtree are larger than the item in X. The tree on the left is a binary search tree, but the tree on the right is not. The tree on the right has a node with key 7 in the left subtree of a node with key 6 (which happens to be the root).
  • 3. 3 Binary Search Trees - Operations Descriptions and implementations of the operations that are usually performed on binary search trees (BST) are given. Note that because of the recursive definition of trees, it is common to write these routines recursively. Because the average depth of a binary search tree is O(log N), we generally do not need to worry about running out of stack space. Since all the elements can be ordered, we will assume that the operators <, >, and = can be applied to them.
  • 4. BST – Implementation - I 4 typedef int ElementType; struct TreeNode; typedef struct TreeNode *Position; typedef struct TreeNode *SearchTree; struct TreeNode{ ElementType Element; SearchTree Left; SearchTree Right; }; SearchTree MakeEmpty(SearchTree T){ if( T != NULL ){ MakeEmpty( T->Left ); MakeEmpty( T->Right ); free( T ); } return NULL; } Notice that NULL is returned at the end.
  • 5. 5 • Find operation generally requires returning a pointer to the node in the Binary Search Tree pointed by T that has value X, or NULL if there is no such node. The structure of the tree makes this simple. If T is NULL , then we can just return . Otherwise, we make a recursive call on either the left or the right subtree of the node pointed by T, depending on the relationship of X to the value stored in the node pointed by T. Otherwise, if the value stored X, we can return T. Position Find(ElementType X, SearchTree T){ if( T == NULL ) return NULL; if( X < T->Element ) return Find( X, T->Left ); else if( X > T->Element ) return Find( X, T->Right ); else return T; } BST – Implementation - II
  • 6. 6 Position FindMin(SearchTree T){ if( T == NULL ) return NULL; else if( T->Left == NULL ) return T; else return FindMin( T->Left ); } Position FindMax(SearchTree T){ if( T != NULL ) while( T->Right != NULL ) T = T->Right; return T; } BST – Implementation - III •To perform a FindMin, start at the root and go left as long as there is a left child. The stopping point is the smallest element. •The FindMax routine is the same, except that branching is to the right child. •Notice that the degenerate case of an empty tree is carefully handled. •Also notice that it is safe to change T in FindMax, since we are only working with a copy. Always be extremely careful, however, because a statement such as T->right=T->right->right will make changes.
  • 7. 7 BST – Implementation – Insertion I The insertion routine is conceptually simple. To insert X into tree T, proceed down the tree as you would with a Find. If X is found, do nothing (or "update" something). Otherwise, insert X at the last spot on the path traversed. Duplicates can be handled by keeping an extra field in the node indicating the frequency of occurrence. If the key is only part of a larger record, then all of the records with the same key might be kept in an auxiliary data structure, such as a list or another search tree. → Insert node 5
  • 8. 8 SearchTree Insert(ElementType X, SearchTree T){ if( T == NULL ){ /* Create and return a one-node tree */ T = malloc( sizeof( struct TreeNode ) ); if( T == NULL ) FatalError( "Out of space!!!" ); else { T->Element = X; T->Left = T->Right = NULL; } } else if( X < T->Element ) T->Left = Insert( X, T->Left ); else if( X > T->Element ) T->Right = Insert( X, T->Right ); /* Else X is in the tree already; do nothing */ return T; /* Do not forget this line!! */ } BST – Implementation – Insertion II
  • 9. 9 BST – Implementation – Deletion I • Once we have found the node to be deleted, we need to consider 3 possibilities. (1) If the node is a leaf, it can be deleted immediately. (2) If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node. Notice that the deleted node is now unreferenced and can be disposed of only if a pointer to it has been saved. → Delete node 4
  • 10. 10 BST – Implementation – Deletion II (3) The complicated case deals with a node with two children. The general strategy is to replace the key of this node with the smallest key of the right subtree (easy) and recursively delete that node (which is now empty). Because the smallest node in the right subtree cannot have a left child, the second delete is an easy one. → Delete node 2
  • 11. 11 BST – Implementation – Deletion III SearchTree Delete(ElementType X, SearchTree T){ Position TmpCell; /* declare a pointer */ if( T == NULL ) Error( "Element not found" ); else if( X < T->Element ) /* Go left */ T->Left = Delete( X, T->Left ); else if( X > T->Element ) /* Go right */ T->Right = Delete( X, T->Right ); else if( T->Left && T->Right ){ /* Found, it has 2 children */ TmpCell = FindMin( T->Right ); /* smallest in the right */ T->Element = TmpCell->Element; /* Replace with smallest */ T->Right = Delete( T->Element, T->Right ); } else { /* Found, it has one or zero children */ TmpCell = T; T = (T->Left)?T->Left:T->Right;/* Also handles 0 children */ free( TmpCell ); } return T; } Inefficient, since calls highlighted in yellow result in two passes down the tree to find and delete the smallest node in the right subtree.
  • 12. • We can use stacks to convert an expression in standart form (otherwise known as infix) into postfix. • Example: operators = {+, *, (, )}, usual precedence rules; a + b * c + (d * e + f) * g Answer = a b c * + d e * f + g * + 12 BST – Implementation – Deletion IV SearchTree Delete(ElementType X, SearchTree T){ Position TmpCell, TmpPrevCell; /* declare another pointer */ ... else if( T->Left && T->Right ){ /* Found, it has 2 children */ TmpCell = T->Right; /* to point to smallest in the right */ TmpPrevCell = T->Right; /* to point to parent of TmpCell */ while (TmpCell->Left != NULL){/* find smallest of right */ TmpPrevCell = TmpCell; TmpCell = TmpCell->Left; } T->Element = TmpCell->Element;/* replace with smallest */ if (TmpCell == TmpPrevCell) /* T->Right is smallest */ T->Right = TmpCell->Right; /* skip over TmpCell */ else /* connect Left of TmpPrevCell to Right of TmpCell */ TmpPrevCell->Left = TmpCell->Right; free (TmpCell); } ... } Efficient Version
  • 13. BST – Implementation – Lazy Deletion • If the number of deletions is small, then a popular strategy to use is lazy deletion: When an element is to be deleted, it is left in the tree and merely marked as deleted. This is especially popular if duplicates are present, because then the field that keeps count of the items can be decremented. • If the number of real nodes is the same as the number of "deleted" nodes, then the depth of the tree is only expected to go up by a small constant (why?), so there is a very small time penalty associated with lazy deletion. Also, if an item is reinserted, the overhead of allocating a new cell is avoided. 13
  • 14. Average-Case Analysis - I 14 • All of the operations of BST, except MakeEmpty, take O(d) time where d is the depth of the node containing the accessed key. As a result, they are O (depth of tree). • Why? Because in constant time we descend a level in the tree, thus operating on a tree that is now roughly half as large. • MakeEmpty take O(N) time. • Observation: The average depth over all nodes in a BST is O(log N) assuming all insertion sequences are equally likely. • Proof: The sum of the depths of all nodes in a tree is the internal path length. Let’s calculate the average internal path length over all possible insertion sequences.
  • 15. 15 Average-Case Analysis - II • Let D(N) be the internal path length for some BST T of N nodes. D(1) = 0. • D(N) = D(i) + D(N-i-1) + N -1 // Subtree nodes are 1 level deeper • All subtree sizes are equally likely for BSTs, since it depends only on the rank of the first element inserted into BST. This does not hold for binary trees though. Let’s, then, average: 1)()/2()( 0),1)1()(()/1()( 1 0 1 0 −+    = <≤∀−+−−+= ∑ ∑ − = − = NiDNND NiiNiNDiDNND N i N i • If the recurrence is solved, D(N) = O(N log N). Thus, the expected depth of any node is O(log N).
  • 16. Derivation of D(N) - 1 16 1)()/2()( 0),1)1()(()/1()( 1 0 1 0 −+      = <≤∀−+−−+= ∑ ∑ − = − = NiDNND NiiNiNDiDNND N i N i )1(2)1(2)1()1()( )2)...(2)(1()(2)1()1( )1..().........1()(2)( 2 0 1 0 −+−=−−− −−+      =−− −+      = ∑ ∑ − = − = NNDNDNNND NNiDNDN NNiDNND N i N i ∑= + − +=+ += − − +−−=− + − +−=+ −+−+= N i ii i DNND DD NN N NNDNND NN N NNDNND NNDNNND 2 )1( 1 22/)1()1/()( 3*2 1*2 2/)1(3/)2( ... )1( )2(2 )1/()2(/)1( )1( )1(2 /)1()1/()( )1(2)1()1()( ...(sum the equations side by side) .....(divide by N(N+1)) .....(subtract (2) from (1))
  • 17. 17 Derivation of D(N) - 2 ∑∑ ∑ == = + − + =+ + − +=+ N i N i N i iii NND ii i DNND 22 2 )1( 1 2 1 1 2)1/()( )1( 1 22/)1()1/()( ∑= + −−+++++=+ N i ii NNNND 2 ) 1 11 (2))1/(1/1...4/13/1(2)1/()( )1/(21)1/(2)2/3(log2)1/()( ))1/(12/1(2))1/(1)2/3((log2)1/()( ))1/(12/1(2))1/(1)/1...4/13/1((2)1/()( ++−+++−=+ +−−+++−=+ +−−+++++=+ NNNNND NNNNND NNNNND e e γ γ 4)1(2)1(4log)1(2)( )1/(424log2)1/()( ++++−+= +++−=+ NNNNND NNNND e e γ γ )log()( ) log log ()( )log()( 4)1(2)1(4log)1(2)( 2 2 NNOND e N NOND NNOND NNNNND e e = = = ++++−+= γ
  • 18. 18 Average-Case Analysis - III • As an example, the randomly generated 500 node BST has nodes at expected depth 9.98.
  • 19. 19 Average-Case Analysis - IV )( Nθ After a quarter-million random insert/remove pairs, right-heavy tree on the previous slide, looks decidedly unbalanced and average depth becomes 12.51. • Deletion algorithm described favors making left subtrees deeper than the right (a deleted node is replaced with a node from the right). The exact effect of this still unknown, but if insertions and deletions are alternated Ɵ(N2) times, expected depth is .In the absence of deletions or when lazy deletion is used; average running times for BST operations are O(log N).