binary search trees lecture 6 asst. prof. dr. İlker kocabaş 1
TRANSCRIPT
![Page 1: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/1.jpg)
Binary Search TreesBinary Search Trees
Lecture 6
Asst. Prof. Dr. İlker Kocabaş
1
![Page 2: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/2.jpg)
Binary search tree sortBinary search tree sort
2
![Page 3: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/3.jpg)
Binary-search-tree propertyBinary-search-tree property
xyykeyxkey
xyxkeyykey
x
of subtreeright in the node a is if
of subtereeleft in the node a is if
esearch trebinary ain node a be Let
3
![Page 4: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/4.jpg)
Binary-search-tree propertyBinary-search-tree property
• Binary Search Tree can be implemented as a linked data structure in which each node is an object with three pointer fields.
• The three pointer fields left, right and p point to the nodes corresponding to the left child, right child and the parent respectively.
• NIL in any pointer field signifies that there exists no corresponding child or parent.
• The root node is the only node in the BTS structure with
NIL in its p field.
4
![Page 5: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/5.jpg)
Binary-search-tree propertyBinary-search-tree property
5
![Page 6: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/6.jpg)
Inorder-tree walkInorder-tree walk
)()(
)WALK(-TREE-INORDER 4
print 3
)WALK(-TREE-INORDER 2
NIL 1
)WALK(-TREE-INORDER
nnT
xright
xkey
xleft
x
x
thenif
During this type of walk, we visit the root of a subtree between the left subtree visit and right subtree visit.
6
![Page 7: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/7.jpg)
Preorder-tree walkPreorder-tree walk
)()(
)WALK(-TREE-DER PREOR 4
)WALK(-TREE-DER PREOR 3
print 2
NIL 1
)WALK(-TREE-PREORDER
nnT
xright
xleft
xkey
x
x
thenif
In this case we visit the root node before the nodes in either subtree.
7
![Page 8: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/8.jpg)
PPostostorder-tree walkorder-tree walk
)()(
print 4
)WALK(-TREE-RDER POSTO 3
)WALK(-TREE-RDER POSTO 2
NIL 1
)WALK(-TREE-POSTORDER
nnT
xkey
xright
xleft
x
x
thenif
We visit the root node after the nodes in its subtrees.
8
![Page 9: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/9.jpg)
Sorting by binary-search-treeSorting by binary-search-tree 6
3 7
2 5 8
12
6
3
12
1 NIL
3 PRINT 24
NIL
3 PRINT 3
41 2 NIL3 PRINT 54 NIL
2
1 2 NIL3 PRINT 84 NIL
5
3 PRINT 64
1 2 NIL3 PRINT 74
12
77
8
)WALK(-TREE-INORDER 4
print 3
)WALK(-TREE-INORDER 2
NIL 1
)WALK(-TREE-INORDER
xright
xkey
xleft
x
x
then
if
![Page 10: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/10.jpg)
Sorting by binary-search-tree Sorting by binary-search-tree
WALK-TREE-INORDER execute to timethe reflects where
)1()()(
Then nodes.
1 has right tree whoseand nodes has left tree whose
node aon called is WALK-TREE-INORDERthat Suppose
d
dknTkTnT
knk
x
10
![Page 11: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/11.jpg)
Sorting by binary-search-treeSorting by binary-search-tree (contnd.) (contnd.)
)(
)(
)()(
))1)((())((
)1()()(
)()( that providingby methodon substituti Use
n
cndc
dcdccndc
dckndcckdc
dknTkTnT
cndcnT
11
![Page 12: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/12.jpg)
Searching for a keySearching for a key
15
6 18
3
2 4
7 17
13
20
9
Search for 13
12
![Page 13: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/13.jpg)
Searching for a keySearching for a key
),SEARCH(-TREE 5
),SEARCH(-TREE 4
3
2
or NILL 1
),SEARCH(-TREE
kxright
kxleft
xkeyk
x
xkeykeyx
kx
returnelse
returnthen
if
returnthen
if
)()( hOnT 13
![Page 14: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/14.jpg)
Searching for minimumSearching for minimum
15
6 18
3
2 4
7 17
13
20
9
Minimum
14
![Page 15: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/15.jpg)
Searching for minimumSearching for minimum
x
xleftx
xleft
x
return
do
while
3
][ 2
NILL ][ 1
)MINIMUM(-TREE
)()( hOnT 15
![Page 16: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/16.jpg)
Searching for maximumSearching for maximum
15
6 18
3
2 4
7 17
13
20
9
Maximum
16
![Page 17: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/17.jpg)
Searching for maximumSearching for maximum
x
xrightx
xright
x
return
do
while
3
][ 2
NILL ][ 1
)MAXIMUM(-TREE
)()( hOnT 17
![Page 18: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/18.jpg)
Searching for successorSearching for successor
The successor of a node x is the node with the smallest key greather than key[x]
18
![Page 19: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/19.jpg)
Searching for successorSearching for successor
15
6 18
3
2 4
7 17
13
20
9
Successor of 15
Case 1: The right subtree of node x is nonempty 19
![Page 20: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/20.jpg)
Searching for successorSearching for successor
15
6 18
3
2 4
7 17
13
20
9
Successor of 13
Case 2: The right subtree of node x is empty 20
![Page 21: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/21.jpg)
Searching for successorSearching for successor
15
6 18
3
2 4
7 17
13
20
9
Successor of 4search untily.left = x
Case 2: The right subtree of node x is empty 21
![Page 22: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/22.jpg)
Searching for successorSearching for successor
y
ypy
yx
yrightxy
xpy
xright
xright
x
return
do
while
returnthen
if
7
][ 6
5
][ and NILL 4
][ 3
])[(MINIMUMTREE 2
NILL ][ 1
)SUCCESSOR(-TREE
)()( hOnT 22
Go upper until y.left = x
![Page 23: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/23.jpg)
InsertionInsertion
12
5 18
2
13 17
9 15 19
Inserting an item with key 13 23
![Page 24: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/24.jpg)
InsertionInsertion
][ 13
][ 12
][][ 11
empty was Tree ][ 10
NILL 9
][ 8
][ 7
][ 6
][][ 5
4
NILL 3
][ 2
NILL 1
),(INSERTTREE
zyright
zyleft
ykeyzkey
TzTroot
y
yzp
xrightx
xleftx
xkeyzkey
xy
x
Trootx
y
zT
else
then
ifelse
then
if
else
then
if
do
while
)()( hOnT
24
![Page 25: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/25.jpg)
DeletionDeletion
15
5 16
2
18
12
23
20
Deleting an item with key 13 (z has no children)
10 13 z
5
3
6
7
25
![Page 26: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/26.jpg)
DeletionDeletion
15
5 16
2
18
12
23
20
Deleting an item with key 13 (z has no children)
10
5
3
26
6
7
![Page 27: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/27.jpg)
DeletionDeletion
15
5 16 z
2
18
12
23
20
Deleting a node with key 16 (z has only one child )
10 13
5
3
27
6
7
![Page 28: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/28.jpg)
DeletionDeletion
15
5
2 12
Deleting a node with key 16 (z has only one child )
10 13
5
318 23
20
28
6
7
![Page 29: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/29.jpg)
DeletionDeletion
15
5 16
2
18
12
23
20
Deleting a node with key 5 (6 successor of 5) (z has two children)
10
z 5
3
6 y
7
13
29
![Page 30: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/30.jpg)
DeletionDeletion
15
5 16
2
18
12
23
20
Deleting a node with key 5 (z has two children)
10
z 5
3
6 y
7
30
![Page 31: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/31.jpg)
DeletionDeletion
15
5 16
2
18
12
23
20
Deleting a node with key 5 (z has two children)
10
6
3
7
31
![Page 32: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/32.jpg)
DeletionDeletion
17
into data satellite s'copy 16
][][ 15
14
]][[ 13
]][[ 12
]][[ 11
][ 10
NIL][ 9
][][ 8
NIL 7
][ 6
][ 5
NIL][ 4
)SUCCESSOR(-TREE 3
2
NIL][or NIL][ 1
),(DELETETREE
return
then
if
else
then
ifelse
then
if
then
if
else
then
if
else
then
if
zy
ykeyzkey
zy
xypright
xypleft
yplefty
xTroot
yp
ypxp
x
yrightx
yleftx
yleft
zy
zy
zrightzleft
zT
)()( hOnT
32
![Page 33: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/33.jpg)
Analysis of BST sortAnalysis of BST sort
The expected time to built the tree is asymptotically the same as the running time of quicksort 33
![Page 34: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/34.jpg)
Red Black TreesRed Black Trees
34
![Page 35: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/35.jpg)
Balanced search treesBalanced search trees
35
![Page 36: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/36.jpg)
Red-black treesRed-black trees
36
![Page 37: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/37.jpg)
Example of a red-black treeExample of a red-black tree
37
![Page 38: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/38.jpg)
Example of a red-black treeExample of a red-black tree
38
![Page 39: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/39.jpg)
Example of a red-black treeExample of a red-black tree
39
![Page 40: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/40.jpg)
Example of a red-black treeExample of a red-black tree
40
![Page 41: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/41.jpg)
Example of a red-black treeExample of a red-black tree
41
![Page 42: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/42.jpg)
Height of a red-black treeHeight of a red-black tree
42
![Page 43: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/43.jpg)
Height of a red-black treeHeight of a red-black tree
43
![Page 44: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/44.jpg)
Height of a red-black treeHeight of a red-black tree
44
![Page 45: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/45.jpg)
Height of a red-black treeHeight of a red-black tree
45
![Page 46: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/46.jpg)
Height of a red-black treeHeight of a red-black tree
46
![Page 47: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/47.jpg)
Height of a red-black treeHeight of a red-black tree
47
![Page 48: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/48.jpg)
Proof (continued)Proof (continued)
48
![Page 49: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/49.jpg)
Query operationsQuery operations
49
![Page 50: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/50.jpg)
Modifying operationsModifying operations
50
![Page 51: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/51.jpg)
RotationsRotations
51
![Page 52: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/52.jpg)
Insertion into a red-black treeInsertion into a red-black tree
52
![Page 53: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/53.jpg)
Insertion into a red-black treeInsertion into a red-black tree
53
![Page 54: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/54.jpg)
Insertion into a red-black treeInsertion into a red-black tree
54
![Page 55: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/55.jpg)
Insertion into a red-black treeInsertion into a red-black tree
55
![Page 56: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/56.jpg)
Insertion into a red-black treeInsertion into a red-black tree
56
![Page 57: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/57.jpg)
PseudocodePseudocode
57
![Page 58: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/58.jpg)
Graphical notationGraphical notation
58
![Page 59: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/59.jpg)
Case 1Case 1
59
![Page 60: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/60.jpg)
Case Case 22
60
![Page 61: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/61.jpg)
Case Case 33
61
![Page 62: Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0101a28abf838ccb043/html5/thumbnails/62.jpg)
AnalysisAnalysis
62