SlideShare a Scribd company logo
Binary Search Trees
btrees - 2
Binary Trees
 Recursive definition
1. An empty tree is a binary tree
2. A node with two child subtrees is a binary tree
3. Only what you get from 1 by a finite number of
applications of 2 is a binary tree.
56
26 200
18 28 190 213
12 24 27
btrees - 3
Binary Search Trees
 View today as data structures that can support
dynamic set operations.
» Search, Minimum, Maximum, Predecessor,
Successor, Insert, and Delete.
 Can be used to build
» Dictionaries.
» Priority Queues.
 Basic operations take time proportional to the
height of the tree – O(h).
btrees - 4
BST – Representation
 Represented by a linked data structure of nodes.
 root(T) points to the root of tree T.
 Each node contains fields:
» key
» left – pointer to left child: root of left subtree.
» right – pointer to right child : root of right subtree.
» p – pointer to parent. p[root[T]] = NIL (optional).
btrees - 5
Binary Search Tree Property
 Stored keys must satisfy
the binary search tree
property.
»  y in left subtree of x,
then key[y]  key[x].
»  y in right subtree of x,
then key[y]  key[x].
56
26 200
18 28 190 213
12 24 27
btrees - 6
Inorder Traversal
Inorder-Tree-Walk (x)
1. if x  NIL
2. then Inorder-Tree-Walk(left[p])
3. print key[x]
4. Inorder-Tree-Walk(right[p])
The binary-search-tree property allows the keys of a binary search
tree to be printed, in (monotonically increasing) order, recursively.
56
26 200
18 28 190 213
12 24 27
btrees - 7
Correctness of Inorder-Walk
 Must prove that it prints all elements, in order,
and that it terminates.
 By induction on size of tree. Size=0: Easy.
 Size >1:
» Prints left subtree in order by induction.
» Prints root, which comes after all elements in left
subtree (still in order).
» Prints right subtree in order (all elements come after
root, so still in order).
btrees - 8
Querying a Binary Search Tree
 All dynamic-set search operations can be supported in
O(h) time.
 h = (lg n) for a balanced binary tree (and for an
average tree built by adding nodes in random order.)
 h = (n) for an unbalanced tree that resembles a linear
chain of n nodes in the worst case.
btrees - 9
Tree Search
Tree-Search(x, k)
1. if x = NIL or k = key[x]
2. then return x
3. if k < key[x]
4. then return Tree-Search(left[x], k)
5. else return Tree-Search(right[x], k)
Running time: O(h)
Aside: tail-recursion
56
26 200
18 28 190 213
12 24 27
btrees - 10
Iterative Tree Search
Iterative-Tree-Search(x, k)
1. while x  NIL and k  key[x]
2. do if k < key[x]
3. then x  left[x]
4. else x  right[x]
5. return x
The iterative tree search is more efficient on most computers.
The recursive tree search is more straightforward.
56
26 200
18 28 190 213
12 24 27
btrees - 11
Finding Min & Max
Tree-Minimum(x) Tree-Maximum(x)
1. while left[x]  NIL 1. while right[x]  NIL
2. do x  left[x] 2. do x  right[x]
3. return x 3. return x
The binary-search-tree property guarantees that:
» The minimum is located at the left-most node.
» The maximum is located at the right-most node.
btrees - 12
Predecessor and Successor
 Successor of node x is the node y such that key[y] is the
smallest key greater than key[x].
 The successor of the largest key is NIL.
 Search consists of two cases.
» If node x has a non-empty right subtree, then x’s successor is
the minimum in the right subtree of x.
» If node x has an empty right subtree, then:
• As long as we move to the left up the tree (move up through right
children), we are visiting smaller keys.
• x’s successor y is the node that x is the predecessor of (x is the maximum
in y’s left subtree).
• In other words, x’s successor y, is the lowest ancestor of x whose left
child is also an ancestor of x.
btrees - 13
Pseudo-code for Successor
Tree-Successor(x)
 if right[x]  NIL
2. then return Tree-Minimum(right[x])
3. y  p[x]
4. while y  NIL and x = right[y]
5. do x  y
6. y  p[y]
7. return y
Code for predecessor is symmetric.
Running time: O(h)
56
26 200
18 28 190 213
12 24 27
btrees - 14
BST Insertion – Pseudocode
Tree-Insert(T, z)
1. y  NIL
2. x  root[T]
3. while x  NIL
4. do y  x
5. if key[z] < key[x]
6. then x  left[x]
7. else x  right[x]
8. p[z]  y
9. if y = NIL
10. then root[t]  z
11. else if key[z] < key[y]
12. then left[y]  z
13. else right[y]  z
 Change the dynamic set
represented by a BST.
 Ensure the binary-
search-tree property
holds after change.
 Insertion is easier than
deletion.
56
26 200
18 28 190 213
12 24 27
btrees - 15
Analysis of Insertion
 Initialization: O(1)
 While loop in lines 3-7
searches for place to
insert z, maintaining
parent y.
This takes O(h) time.
 Lines 8-13 insert the
value: O(1)
 TOTAL: O(h) time to
insert a node.
Tree-Insert(T, z)
1. y  NIL
2. x  root[T]
3. while x  NIL
4. do y  x
5. if key[z] < key[x]
6. then x  left[x]
7. else x  right[x]
8. p[z]  y
9. if y = NIL
10. then root[t]  z
11. else if key[z] < key[y]
12. then left[y]  z
13. else right[y]  z
btrees - 16
Exercise: Sorting Using BSTs
Sort (A)
for i  1 to n
do tree-insert(A[i])
inorder-tree-walk(root)
btrees - 17
Tree-Delete (T, x)
if x has no children  case 0
then remove x
if x has one child  case 1
then make p[x] point to child
if x has two children (subtrees)  case 2
then swap x with its successor
perform case 0 or case 1 to delete it
 TOTAL: O(h) time to delete a node
btrees - 18
Deletion – Pseudocode
Tree-Delete(T, z)
/* Determine which node to splice out: either z or z’s successor. */
 if left[z] = NIL or right[z] = NIL
 then y  z
 else y  Tree-Successor[z]
/* Set x to a non-NIL child of x, or to NIL if y has no children. */
4. if left[y]  NIL
5. then x  left[y]
6. else x  right[y]
/* y is removed from the tree by manipulating pointers of p[y]
and x */
7. if x  NIL
8. then p[x]  p[y]
/* Continued on next slide */
btrees - 19
Deletion – Pseudocode
Tree-Delete(T, z) (Contd. from previous slide)
9. if p[y] = NIL
10. then root[T]  x
11. else if y  left[p[i]]
12. then left[p[y]]  x
13. else right[p[y]]  x
/* If z’s successor was spliced out, copy its data into z */
14. if y  z
15. then key[z]  key[y]
16. copy y’s satellite data into z.
17. return y
btrees - 20
Binary Search Trees
 View today as data structures that can support
dynamic set operations.
» Search, Minimum, Maximum, Predecessor,
Successor, Insert, and Delete.
 Basic operations take time proportional to the
height of the tree – O(h).
btrees - 21
Red-black trees: Overview
 Red-black trees are a variation of binary search
trees to ensure that the tree is balanced.
» Height is O(lg n), where n is the number of nodes.
 Operations take O(lg n) time in the worst case.
btrees - 22
Red-black Tree
 The red-Black tree is a binary search tree. The prerequisite of
the red-black tree is that we should know about the binary search
tree.
 In a binary search tree, the values of the nodes in the left subtree
should be less than the value of the root node, and the values of
the nodes in the right subtree should be greater than the value of
the root node.
 Each node in the Red-black tree contains an extra bit that
represents a color to ensure that the tree is balanced during any
operations performed on the tree like insertion, deletion, etc.
btrees - 23
Red-black Properties
1. Every node is either red or black.
2. The root is black.
3. Every leaf (nil) is black.
4. If a node is red, then both its children are
black.
5. For each node, all paths from the node to
descendant leaves contain the same number of
black nodes.
btrees - 24
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30
Step 1 Step 2 Step3
Step 4 Step 5 Step 6
btrees - 25
Step 7 Step 8

More Related Content

Similar to UNIT II.ppt

BST.pdf
BST.pdfBST.pdf
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
Dheeraj Singh
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
United International University
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure trees
zukun
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
Suneel61
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
cinterviews
 
lecture 13
lecture 13lecture 13
lecture 13
sajinsc
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
Intro C# Book
 
rbtrees.ppt
rbtrees.pptrbtrees.ppt
rbtrees.ppt
Umesh Hengaju
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
Saurabh Mishra
 
Red black trees
Red black treesRed black trees
Red black trees
Amit Kumar Rathi
 
C12 bst
C12 bstC12 bst
C12 bst
C12 bstC12 bst
Binary search trees
Binary search treesBinary search trees
Binary search trees
Dwight Sabio
 
B trees
B treesB trees
BTree.pptx
BTree.pptxBTree.pptx
BTree.pptx
ssuser4fbfbf
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
ssuser034ce1
 

Similar to UNIT II.ppt (20)

BST.pdf
BST.pdfBST.pdf
BST.pdf
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure trees
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
lecture 13
lecture 13lecture 13
lecture 13
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
rbtrees.ppt
rbtrees.pptrbtrees.ppt
rbtrees.ppt
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Red black trees
Red black treesRed black trees
Red black trees
 
C12 bst
C12 bstC12 bst
C12 bst
 
C12 bst
C12 bstC12 bst
C12 bst
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
B trees
B treesB trees
B trees
 
BTree.pptx
BTree.pptxBTree.pptx
BTree.pptx
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 

Recently uploaded

Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
PuktoonEngr
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
PauloRodrigues104553
 

Recently uploaded (20)

Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
 

UNIT II.ppt

  • 2. btrees - 2 Binary Trees  Recursive definition 1. An empty tree is a binary tree 2. A node with two child subtrees is a binary tree 3. Only what you get from 1 by a finite number of applications of 2 is a binary tree. 56 26 200 18 28 190 213 12 24 27
  • 3. btrees - 3 Binary Search Trees  View today as data structures that can support dynamic set operations. » Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete.  Can be used to build » Dictionaries. » Priority Queues.  Basic operations take time proportional to the height of the tree – O(h).
  • 4. btrees - 4 BST – Representation  Represented by a linked data structure of nodes.  root(T) points to the root of tree T.  Each node contains fields: » key » left – pointer to left child: root of left subtree. » right – pointer to right child : root of right subtree. » p – pointer to parent. p[root[T]] = NIL (optional).
  • 5. btrees - 5 Binary Search Tree Property  Stored keys must satisfy the binary search tree property. »  y in left subtree of x, then key[y]  key[x]. »  y in right subtree of x, then key[y]  key[x]. 56 26 200 18 28 190 213 12 24 27
  • 6. btrees - 6 Inorder Traversal Inorder-Tree-Walk (x) 1. if x  NIL 2. then Inorder-Tree-Walk(left[p]) 3. print key[x] 4. Inorder-Tree-Walk(right[p]) The binary-search-tree property allows the keys of a binary search tree to be printed, in (monotonically increasing) order, recursively. 56 26 200 18 28 190 213 12 24 27
  • 7. btrees - 7 Correctness of Inorder-Walk  Must prove that it prints all elements, in order, and that it terminates.  By induction on size of tree. Size=0: Easy.  Size >1: » Prints left subtree in order by induction. » Prints root, which comes after all elements in left subtree (still in order). » Prints right subtree in order (all elements come after root, so still in order).
  • 8. btrees - 8 Querying a Binary Search Tree  All dynamic-set search operations can be supported in O(h) time.  h = (lg n) for a balanced binary tree (and for an average tree built by adding nodes in random order.)  h = (n) for an unbalanced tree that resembles a linear chain of n nodes in the worst case.
  • 9. btrees - 9 Tree Search Tree-Search(x, k) 1. if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return Tree-Search(left[x], k) 5. else return Tree-Search(right[x], k) Running time: O(h) Aside: tail-recursion 56 26 200 18 28 190 213 12 24 27
  • 10. btrees - 10 Iterative Tree Search Iterative-Tree-Search(x, k) 1. while x  NIL and k  key[x] 2. do if k < key[x] 3. then x  left[x] 4. else x  right[x] 5. return x The iterative tree search is more efficient on most computers. The recursive tree search is more straightforward. 56 26 200 18 28 190 213 12 24 27
  • 11. btrees - 11 Finding Min & Max Tree-Minimum(x) Tree-Maximum(x) 1. while left[x]  NIL 1. while right[x]  NIL 2. do x  left[x] 2. do x  right[x] 3. return x 3. return x The binary-search-tree property guarantees that: » The minimum is located at the left-most node. » The maximum is located at the right-most node.
  • 12. btrees - 12 Predecessor and Successor  Successor of node x is the node y such that key[y] is the smallest key greater than key[x].  The successor of the largest key is NIL.  Search consists of two cases. » If node x has a non-empty right subtree, then x’s successor is the minimum in the right subtree of x. » If node x has an empty right subtree, then: • As long as we move to the left up the tree (move up through right children), we are visiting smaller keys. • x’s successor y is the node that x is the predecessor of (x is the maximum in y’s left subtree). • In other words, x’s successor y, is the lowest ancestor of x whose left child is also an ancestor of x.
  • 13. btrees - 13 Pseudo-code for Successor Tree-Successor(x)  if right[x]  NIL 2. then return Tree-Minimum(right[x]) 3. y  p[x] 4. while y  NIL and x = right[y] 5. do x  y 6. y  p[y] 7. return y Code for predecessor is symmetric. Running time: O(h) 56 26 200 18 28 190 213 12 24 27
  • 14. btrees - 14 BST Insertion – Pseudocode Tree-Insert(T, z) 1. y  NIL 2. x  root[T] 3. while x  NIL 4. do y  x 5. if key[z] < key[x] 6. then x  left[x] 7. else x  right[x] 8. p[z]  y 9. if y = NIL 10. then root[t]  z 11. else if key[z] < key[y] 12. then left[y]  z 13. else right[y]  z  Change the dynamic set represented by a BST.  Ensure the binary- search-tree property holds after change.  Insertion is easier than deletion. 56 26 200 18 28 190 213 12 24 27
  • 15. btrees - 15 Analysis of Insertion  Initialization: O(1)  While loop in lines 3-7 searches for place to insert z, maintaining parent y. This takes O(h) time.  Lines 8-13 insert the value: O(1)  TOTAL: O(h) time to insert a node. Tree-Insert(T, z) 1. y  NIL 2. x  root[T] 3. while x  NIL 4. do y  x 5. if key[z] < key[x] 6. then x  left[x] 7. else x  right[x] 8. p[z]  y 9. if y = NIL 10. then root[t]  z 11. else if key[z] < key[y] 12. then left[y]  z 13. else right[y]  z
  • 16. btrees - 16 Exercise: Sorting Using BSTs Sort (A) for i  1 to n do tree-insert(A[i]) inorder-tree-walk(root)
  • 17. btrees - 17 Tree-Delete (T, x) if x has no children  case 0 then remove x if x has one child  case 1 then make p[x] point to child if x has two children (subtrees)  case 2 then swap x with its successor perform case 0 or case 1 to delete it  TOTAL: O(h) time to delete a node
  • 18. btrees - 18 Deletion – Pseudocode Tree-Delete(T, z) /* Determine which node to splice out: either z or z’s successor. */  if left[z] = NIL or right[z] = NIL  then y  z  else y  Tree-Successor[z] /* Set x to a non-NIL child of x, or to NIL if y has no children. */ 4. if left[y]  NIL 5. then x  left[y] 6. else x  right[y] /* y is removed from the tree by manipulating pointers of p[y] and x */ 7. if x  NIL 8. then p[x]  p[y] /* Continued on next slide */
  • 19. btrees - 19 Deletion – Pseudocode Tree-Delete(T, z) (Contd. from previous slide) 9. if p[y] = NIL 10. then root[T]  x 11. else if y  left[p[i]] 12. then left[p[y]]  x 13. else right[p[y]]  x /* If z’s successor was spliced out, copy its data into z */ 14. if y  z 15. then key[z]  key[y] 16. copy y’s satellite data into z. 17. return y
  • 20. btrees - 20 Binary Search Trees  View today as data structures that can support dynamic set operations. » Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete.  Basic operations take time proportional to the height of the tree – O(h).
  • 21. btrees - 21 Red-black trees: Overview  Red-black trees are a variation of binary search trees to ensure that the tree is balanced. » Height is O(lg n), where n is the number of nodes.  Operations take O(lg n) time in the worst case.
  • 22. btrees - 22 Red-black Tree  The red-Black tree is a binary search tree. The prerequisite of the red-black tree is that we should know about the binary search tree.  In a binary search tree, the values of the nodes in the left subtree should be less than the value of the root node, and the values of the nodes in the right subtree should be greater than the value of the root node.  Each node in the Red-black tree contains an extra bit that represents a color to ensure that the tree is balanced during any operations performed on the tree like insertion, deletion, etc.
  • 23. btrees - 23 Red-black Properties 1. Every node is either red or black. 2. The root is black. 3. Every leaf (nil) is black. 4. If a node is red, then both its children are black. 5. For each node, all paths from the node to descendant leaves contain the same number of black nodes.
  • 24. btrees - 24 Let's understand the insertion in the Red-Black tree. 10, 18, 7, 15, 16, 30 Step 1 Step 2 Step3 Step 4 Step 5 Step 6
  • 25. btrees - 25 Step 7 Step 8