Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Chapter 15
– 278 –
Chapter 15: Trees
Exercises 15.1
1. Morse is not immediately decodable because its codes do not have unique prefixes.For example, the given bit string 100001100 can be decoded as: 10 000 110 0 ⇒ N S GEand also as 100 00 11 00 ⇒ D I M Iand as several other strings.
2. BAD 3. DEAD 4. CAD 5. BEADED
Note that for #6 and #7, several different codes are possible, depending on whether the nodes areplaced in the left or the right subtrees.
6. int 10 7. a 111main 11 b 100while 000 c 1100if 01 d 1101for 001 e 0
f 1010g 10110h 10111
8. 1010 0 0 1101 111 1101 0 111 1010 111 10110 0 1101 10111 111 10110 F E E D A D E A F A G E D H A G
9. One possible solution is the following:case 000011class 011do 01001else 0101false 00010for 100goto 000010if 101int 110main 111static 010000struct 00000switch 010001true 00011while 001
Chapter 15
– 279 –
Exercises 15.2
1.
LABEL0
0LABEL
CONST
1LABEL
CONST TYPE
1
00LABEL
CONST TYPE
ARRAY
1
1 0
0
LABEL
CONST TYPE
ARRAY PROCEDURE
BEGIN
2
-1
1
1
0
0
1LABEL
CONST TYPE
ARRAY PROCEDURE00
1
0
LABEL
CONST
TYPE
ARRAY PROCEDURE
BEGIN1
000
0
0LABEL
CONST
TYPE
ARRAY PROCEDURE
BEGIN
END
-1
-1
0
00
1
1
public
long long long long
constconst consttypedef typedef
bool
long
const typedef
bool
public
long
const typedef
bool
break
long
const typedef
public
break public
long
const typedef
bool break
else
bool
Chapter 15
– 280 –
2.
ARRAY0
OF
ARRAY RECORD0
0
0ARRAY
OF0
-1ARRAY
OF
RECORD0
-1
-2
OF
ARRAY RECORD
SET
WITH
OF
ARRAY RECORD
SET WITH
OF
ARRAY
RECORD
SET
-1
00
00
0
0
0 -1
-2
-2
-1
-1
0
WITH
OF
ARRAY
RECORD
SET
CASE WITH
OF
ARRAY
RECORD
SET
CASE
END
-2
1
-1
-1
0
0
0 0
000 0 0
WITH
OF
ARRAY RECORD
SETCASE
END
0
0
0
0
0 0 0
bool
boolboolbool
new new
new
return
return
bool
new
return
struct
bool
new
return
struct
while
bool
new
struct
return dowhile
bool
new
struct
return whilecase
bool
new
struct
return whilecase
enum
bool
new
struct
return while
case
enum
Chapter 15
– 281 –
3.
DIV DIV
MOD
NOT
DIV
MOD DIV
MOD
NOT
-2-1
0
0
0
0
0
0
-1
DIV
MOD
NOT
AND
DIV
MOD
NOT
AND OR
1
1
0
0
0
0
0
-11
DIV
MOD
NOT
AND ORIN
DIV
MOD
NOT
AND ORIN NIL000
0
-1
000
00
0
0
0
do do
long
do
long
new
new
long
newdowhile
auto auto operator
do
long
new
long
do
newdowhile
auto operator
long
intwhile
newdowhile
auto operator
long
intwhile
namespace
Chapter 15
– 282 –
4.
2
0
0
0
0
00
1OR OR
NOT
OR
NOT
MOD
MOD
NOT
OR1
2
2
0
0
0
0
0
00
0MOD
NOT
OR
IN
MOD
NOT
OR
IN
DIV
MOD
NOT
ORIN
DIV
1
1
1
1
00
0
0MOD
NOT
ORIN
DIV
AND
MOD
NOT
OR
IN
DIV
AND
11
1
2
0
0
0
0
unsigned unsigned
shortwhile
unsigned
shortwhile
long
long unsigned
shortwhile
double
long unsigned
shortwhile
double
long unsigned
shortwhile
int
unsigned
shortwhile
int
double long
unsigned
shortwhile
int
double long
char
int
char
shortwhile
long unsigned
double
Chapter 15
– 283 –
5.
BEGIN BEGIN
END
BEGIN
END
IF
END
BEGIN IF0
-2
0
0
0
0
0-1
-1
END
BEGIN IF
THENELSE
END
BEGIN IF
THENELSE
CASE
-2
0
1
0
0 00
1END
BEGIN IF
THEN
0
0
-1
-1 -1-1 -1
END
BEGIN
IF
THENELSE
CASE
WHILE
END
BEGIN
IF
THENELSE
CASE0
-2
0
0
0
0
0
0
00 -1
-1
-1
000 0
00
10
WHILE
END
BEGIN IF
THEN
ELSE
CASE
WHILE
END
BEGIN IF
THEN
ELSE
CASE
DO
0
0 0
0
0
-1
1
WHILE
END
BEGIN IF
THEN
ELSE
CASE
DO REPEAT
WHILE
END
BEGIN IF
THEN
ELSE
CASE
DO REPEAT UNTIL
0
0 0
00
-1
1
1 -1
-1
0
0
0
000
1 1
-1
return
if
enum
enumenum if
enum
if
this
enum
if
this
boolbool
else
enum
if
this
bool
else
case
enum
if
this
case
elsebool
enum
ifcase
elsebool this
bool bool bool
bool
enum
case this
ifelsebool
enum
case this
ifelsebool
do return
enum
case this
ifelsebool
do unsigned
enum
case this
ifelsebool
do
void
void void
void void
Chapter 15
– 284 –
-1
2
FOR
WHILE
END
BEGIN IF
THEN
ELSE
CASE
DO REPEAT UNTIL
TO
-1
-1
FOR
WHILE
END
BEGIN IF
THEN
ELSE
CASE
DO REPEAT UNTIL
0
0 000
0
0
0
1 1
-1
0
0
0
0
00 1
1
FOR WHILE
END
BEGIN IF
THEN
ELSE
CASE
DO REPEAT
UNTIL
TO
0
0 01
0
0
0
00
-1
00
FOR WHILE
END
BEGIN IF
THEN
ELSE
CASE
DO REPEAT
UNTIL
TO
DOWNTO
0
1
-1
-2
2 0
0000
0
0
0
FOR WHILE
END
IF
THEN
REPEAT
UNTIL
TOELSE
BEGIN
CASE
DO
DOWNTO
-1
0
0
0
0 0
0
00
0
0 0
0
return
enum
case this
ifelsebool
do unsignedfor return
enum
case this
ifelsebool
do unsignedfor
true
return
enum
case this
ifelsebool
do for
unsigned
true
void void
void
return
enum
case
this
ifelse
bool
do for
unsigned
true void
double
return
this
if
for
unsigned
true void
double
do else
enum
case
bool
Chapter 15
– 285 –
FOR WHILE
END
IF
THEN
REPEAT
UNTIL
TOELSE
BEGIN
CASE
DO
DOWNTO
WITH
-1
-1
-1
-1
0
0 0 0
0
0
0
00
-1
6.
22
22
44
66
8822
44
88
22
44
22
44
880
1-10
-1 -2
0
0 -1
0
0
0
0
22
44
88
66
55
44
8855
6622 66
5511
22
44
88
-2
2
1
-1
0
0 10
0
0
0
0
0
000
77
5511
99
33
6622
44
88
66
5511
99
22
44
88
-1
-1 -1
-1
1 -1
0
0 0 0
00
000
case
bool
return
this
if
for
unsigned
void
double
do else
enum
true
whilewhile
Chapter 15
– 286 –
7.-1-2-1
-111 11
22
11
33
22 33
22
11 33
22
11
44
00
0
0
0
0 0
0
-1
-2
-2
33
22
11
44
55
22
11 44
33 55
22
11 44
33 55
66
33
22
11
55
66
44
0 0
00
0
0
0
0
00
0
0
0
0
-1
-1
-1
-1
-1
-2
-2
33
22
11
55
66
44
77
33 77
66
44
22
11 55
88
33 77
66
44
22
11 550
0
0
0
0
0
0
0
0
0
00
0
00
0
-1
-1-1
-1
-1
-2
-2
-2
88
33 77
66
44
22
11 55
99
33
66
44
22
11 55
99
88
77
00
0
000
0
00
00
-1
-1
-1
8.
2
1
11
1 22
00
0
0
0
0
0
0
99 99
88
99
88
77
9977
88
66
9977
88
55
66
9977
880
0
1
Chapter 15
– 287 –
1
1 2
77
66
88
99
55 77
66
88
99
55
44
44 99
55
66
88
77
0
0
0
0 0
00
1 0
0
0
00
1
2
33
44 99
55
66
88
77 33
66
99
88
77
44
55 33
66
99
88
77
44
55
22
0
0
0 0
0 00
0
0 0
0
1
1
1
1
1
0
00
0
0
1
2
2
2 0
33
66
99
88
77
44
55
22
11
66
99
88
77
44
5522
11 33
0
0
00
0
00 0
00
0
1
1
9.1
1
1
1
2
20
0
55
33
55
77
55
33
22
77
55
33
11
22
77
55
330 0
0
0 0
0
0
2
-1
55
77
11
22
33
5522
11 77
33
44
44
55
77
11
22
33
0
0
0 0
0
0
0
00
0
0
0
1
1
-1
Chapter 15
– 288 –
88
5522
11 77
33
44
66 88
5522
11 77
33
4400
0
0
00
00
1 1 -1
-1
-1
-1
-1
-2
-2
0
55
77
11
22
33
66 9944
88
99
66 88
5522
11 77
33
440
0
00
0
0
0
0
0
1 1
-1
-1-1
-1
10.
50 50
45 75
50
45
65
75
50
45
70
65
75
50
451
-21
2
-1
0
0
0
0
0
00
0
0
-1
70
65 75
45
50
35
70
65 75
45
50
25
35
70
65 75
45
501
1
1 2
0 0 0
0
0
0
0
00
00
0 0
-1
25
35
50
70
45 65 75
15
25
35
50
70
45 65 75
15
25
35
50
70
45 65 75
60
1 00
0 0
0
0
0 00
0
0
0
00 0
0
0
1
1
1
1
1 1
Chapter 15
– 289 –
20
15
25
35
50
70
45 65 75
60 15 25
35
50
70
45 65 75
60
20
2
2 0
0
-1
11 00
0
0
00
0
0
01
1 11
25 6015
20
40
50
7035
756545
30
25 6015
20
40
50
7035
756545
1
1
00
0
0
10
0
-1
-10 0 0
0
0
0
1
1
1
11 0
30
25 6015
20
40
50
7035
756545
30
40
55
70
60
65 75
50
35
45
15
20
25
2
2
-1
1
11
0
0
00
0
0
0
0 0
0
0
-1
-1
-1
1
1
11
1
Chapter 15
– 290 –
30
40 55
70
60
65
75
50
35
45
15
20
25
30
40 55
70
60
65
75
50
35
45
15
20
25
100
0
0
-1
-1
-1
1
1
1
00
0 0
00
00
0
0
00
1
1
1
1
1
1
80
30
40 55
70
60
65
75
50
35
45
15
20
25
10
-1
1
1
-1
0
0 0
0
0
0
0 0
0
1
1
Chapter 15
– 291 –
11.
Chapter 15
– 292 –
12.
Chapter 15
– 293 –
Chapter 15
– 294 –
13.
Chapter 15
– 295 –
Chapter 15
– 296 –
Chapter 15
– 297 –
Chapter 15
– 298 –
14.template <typename ElementType>class AVLTree{ public: /*** FUNCTION MEMBERS // Usual BST operations
private: /*** Node ***/
class AVLNode { short int balanceFactor; ElementType data AVLNode * left; AVLNode * right; }
//-- AVLNode constructors AVLNode() { balanceFactor = 0; left = right = 0; }
AVLNode(ElementType item) { balanceFactor = 0; data = item; left = right = 0; }
Chapter 15
– 299 –
typedef AVLNode * AVLNodePointer;
/*** DATA MEMBERS ***/ AVLNodeoOinter myRoot;};
The insert operation requires extensive modification from that for BSTs in general. Here is analgorithm:
/* Algorithm to insert an item item into an AVL tree */
1. If the BST is emptyCreate a node containing item with both left and right links null and balance factor = 0,and return from this algorithm.
// Search for insertion point; ptrA keeps track of the most recent ancestor with balance factor// ±1 and ptrAParent points to the parent of ptrA. Pointer parent follows locPtr along the
search path.2. Set pointers ptrAParent and parent to null, ptrA and locPtr to the root of the BST,3. While (locPtr != 0) do the following: // Search for insertion point for item
If (locPtr->balanceFactor != 0)a. Set ptrA = locPtr and ptrAParent = parent.b. If (item < locPtr->data) // go left
Set parent = locPtr and locPtr = locPtr->left.Else // go right
Set parent = locPtr and locPtr = locPtr->right.Else // item is in the tree
Display message that item is in the tree and terminate the algorithm.End if.
End if.End while.
// item is not in the tree. Insert it as a child of parent.4. Get a node pointed to by newPtr and containing item in its data part, both left and right
linksset to null pointers, and balance factor = 0.
5. If (item < parent->data) // insert as left childSet parent->left = newPtr.
Else // insert as right childSet parent->right = newPtr.
End if.
// Rebalance the tree by adjusting balance factors of nodes on the path from ptrA to parent;// all of these nodes have balance factor 0, which will change to ±1. direction is +1 or// according as item is inserted in the left subtree or the right subree of ptrA.
Chapter 15
– 300 –
6. If (item < ptrA->data)Set locPtr = ptrA->left, ptrB = p, and direction = +1,
ElseSet locPtr = ptrA->right, ptrB = p, and direction = –1,
End if.
7. While (locPtr != newPtr) do the following:If (item < locPtr->data) // height of left subtree increases by 1
Set locPtr<balanceFactor = +1 and locPtr = locPtr->left.Else // height of right subtree increases by 1
Set locPtr<balanceFactor = –1 and locPtr = locPtr->right.End if.
End while.
// Check if tree is unbalanced8. If (ptrA->balanceFactor == 0) // tree is still balanced
Set ptrA->balanceFactor = d and terminate the algorithm.9, If (ptrA->balanceFactor + d == 0) // tree is still balanced
Set ptrA->balanceFactor = 0 and terminate the algorithm.
// Tree is unbalanced; perform suitable rotation10. If (direction = +1) // left imbalance
If (ptrB->balanceFactor = +1) // LL rotationa, Set ptrA->left = ptrB->right.b. Set ptrB->right = ptrA.c. ptrA->balanceFactor = ptrB->balanceFactor = 0.
Else // LR rotationa. Set ptrC = ptrB->right, cptrLeft = c->left, cptrRight = c->right.b. Set ptrB->right = cptrLeft.c. Set ptrA->left = cptrRight,d. Set ptrC->left = ptrB.e. Set ptrC->right = ptrA.f. If (ptrC->balanceFactor == +1) // LR - case 2
Set ptrA->balanceFactor = –1, ptrB->balanceFactor = 0.Else if (ptrC->balanceFactor == –1) // LR - case 3
Set ptrB->balanceFactor = +1, ptrA->balanceFactor = 0.Else // LR - case 1
i. Set ptrC->balanceFactor = 0.ii.Set ptrB = ptrC ,
End if.End if.
Else// --- Right imbalance -- This is symmetric to the above for left imbalance.// ---
End if.
Chapter 15
– 301 –
// Subtree with root ptrB has been rebalanced and is the new subtree of ptrAParent (for whichthe original
// root was ptrA).
11. If (ptrAParent == 0)Set BST's root equal to ptrB.
Else if (ptrA == ptrAParent->left)Set ptrAParent->left == ptrB.
Else // ptrA = ptrAParent->rightSet ptrAParent->right = ptrB.
End if.
Deletion from an AVL tree is more difficult. If there are not many deletions, one might uselazy deletion: instead of deleting the node and modifying rebalancing the tree, we mark the nodeas deleted. Then, during operations that require searching the tree, we indicate that the item isnot in the tree if the desired node is found but marked. This keeps the balance of a tree frombeing disturbed by deletions.
However, if many deletions will be done, then we can delete the node as for a BST and thenrebalance the tree. A search of the Internet for AVL deletion will turn up several places wherethis deletion + rebalancing is described.
See, for example, http://www.cmcrossroads.com/bradapp/ftp/ for an AVLTree class.
Exercises 15.3
Chapter 15
– 302 –
Chapter 15
– 303 –
C
A
F
HE
G
B
D
I
(c)
F
L
K
RA
E
N
S
C
O
(d)20. 21.
Chapter 15
– 304 –
22. An algorithm to delete an element from a 2-3-4 tree must deal with the rebalancing that may benecessary after an element is removed. Recall that insertion sometimes results in a splitting of anode and that split may propagate up or down the tree, depending on whether one is performing atop-down insertion or a bottom-in insertion. In a like manner, deleting an element from a 2-3-4tree may cause perturbations elsewhere in the tree. There are two main considerations:
(1) Deletion of an element in a 2-node necessitates the replacement of that value with one fromits children.
(2) Deletion of a 3- or 4-node element may necessitate the merging of children (because ineffect, the elements of a 3- or 4-node serve as separator values for their children).
Note that in the latter case, a merge may result in overflow, in which case, we actually need amerge-split operation: merge sibling nodes (causing overflow) and then split the resulting node.
To streamline the deletion process, we reduce the deletion of an arbitrary element to thedeletion of an element that is in a leaf node. If the element to be deleted is in a 3-node or 4-nodeleaf, then after deletion, we have a 2-node or 3-node leaf. No restructuring is then required. Toavoid propagation of restructuring, we need to ensure that the element to be deleted is in a 3-node or 4-node leaf before deletion. That is, restructuring is done as we search for the element tobe deleted.
The restructuring process rests on the search for the item to be deleted as progressing throughonly 3-node and 4-nodes. If we move through a 2-node, then restructuring is required. Thefollowing steps detail the process. Assume that we are at a node p and will next move to a childq. If p is a leaf, the element to be deleted is either in this node or is not in the tree:
Delete it; no restructuring is needed.
If q is a 3-node or 4-node: Move to this node.
If q is a 2-node (x & y) and its nearest sibling r is also a 2-node with children x and y:
o If p is a 2-node with children q and r, merge q, p and r into a 4-node with childrenx, y, v, and w.
o If p is a 3-node (or 4-node), merge q, p, and r into a 4-node with children x, y, v, andw (as before), but this 4-node is a child of the 2-node (or 3-node) containing theremaining elements of p's original node.
If q is a 2-node (x & y) and its nearest sibling r is also a 3-node (or 4-node:Transform q into a 3-node by taking the appropriate separator out of pand one of its children out of r.
Chapter 15
– 305 –
23. An algorithm to delete a node from a red-black tree must deal with the rebalancing that may benecessary after an element is removed. As explored in detail in Exercise 22, restructuring may beconfined if one ensures that the node to be deleted is a leaf node, in this case one with a red linkfrom its parent. Again, rebalancing is done as we search for the element to be deleted. As inExercise 22, the insertion transformations (rotations) are performed in reverse. However, herethe cost is cheaper since color changes take care of most transformations.
24-25. Developing complete and correct classes for red-black trees and RB trees is a nontrivial taskand can be assigned to students who want (or need) a challenge. Source code can be found bysearching the Internet; for example, see http://www.cmcrossroads.com/bradapp/ftp/ .
26. This is an interesting project to challenge students that is not as difficult as those in Exercises 24and 25. They must decide how to have the user enter the structure of the original tree and storethe data in a binary tree as described in the text and then modify the binary tree operations sothey perform the desired tree operations.