Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
-
Upload
agatha-tyler -
Category
Documents
-
view
217 -
download
0
Transcript of Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
Binary Search TreesBinary Search Trees
Lecture 6
Asst. Prof. Dr. İlker Kocabaş
1
Binary search tree sortBinary search tree sort
2
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
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
Binary-search-tree propertyBinary-search-tree property
5
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
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
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
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
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
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
Searching for a keySearching for a key
15
6 18
3
2 4
7 17
13
20
9
Search for 13
12
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
Searching for minimumSearching for minimum
15
6 18
3
2 4
7 17
13
20
9
Minimum
14
Searching for minimumSearching for minimum
x
xleftx
xleft
x
return
do
while
3
][ 2
NILL ][ 1
)MINIMUM(-TREE
)()( hOnT 15
Searching for maximumSearching for maximum
15
6 18
3
2 4
7 17
13
20
9
Maximum
16
Searching for maximumSearching for maximum
x
xrightx
xright
x
return
do
while
3
][ 2
NILL ][ 1
)MAXIMUM(-TREE
)()( hOnT 17
Searching for successorSearching for successor
The successor of a node x is the node with the smallest key greather than key[x]
18
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
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
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
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
InsertionInsertion
12
5 18
2
13 17
9 15 19
Inserting an item with key 13 23
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
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
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
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
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
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
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
DeletionDeletion
15
5 16
2
18
12
23
20
Deleting a node with key 5 (z has two children)
10
6
3
7
31
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
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
Red Black TreesRed Black Trees
34
Balanced search treesBalanced search trees
35
Red-black treesRed-black trees
36
Example of a red-black treeExample of a red-black tree
37
Example of a red-black treeExample of a red-black tree
38
Example of a red-black treeExample of a red-black tree
39
Example of a red-black treeExample of a red-black tree
40
Example of a red-black treeExample of a red-black tree
41
Height of a red-black treeHeight of a red-black tree
42
Height of a red-black treeHeight of a red-black tree
43
Height of a red-black treeHeight of a red-black tree
44
Height of a red-black treeHeight of a red-black tree
45
Height of a red-black treeHeight of a red-black tree
46
Height of a red-black treeHeight of a red-black tree
47
Proof (continued)Proof (continued)
48
Query operationsQuery operations
49
Modifying operationsModifying operations
50
RotationsRotations
51
Insertion into a red-black treeInsertion into a red-black tree
52
Insertion into a red-black treeInsertion into a red-black tree
53
Insertion into a red-black treeInsertion into a red-black tree
54
Insertion into a red-black treeInsertion into a red-black tree
55
Insertion into a red-black treeInsertion into a red-black tree
56
PseudocodePseudocode
57
Graphical notationGraphical notation
58
Case 1Case 1
59
Case Case 22
60
Case Case 33
61
AnalysisAnalysis
62