Lec9 Balanced BST
Lec9 Balanced BST
D E F
D E F
Player 1
(MAX)
Player 2
(MIN)
5 7 10 11 2 19 14 17 4
• Brute-force
1. Generate the whole game tree to leaves
2. Apply utility (payoff) function to leaves
3. Back-up values from leaves toward the root:
• a Max node computes the max of its child values
• a Min node computes the min of its child values
4. When value reaches the root: choose max value and the
corresponding move.
• Minimax
Search the game-tree in a DFS manner to find the value of the root
Properties of minimax
• Suppose at each step, a player has b choices
• Suppose m steps are needed in the worst case before
the game finishes
• What’s the running time for generating the best move?
– Number of depth 1 nodes:
– Number of depth 2 nodes:
– …
– Number of depth m nodes:
– Total nodes searched:
• Time Complexity? Space Complexity?
• How to reduce the searching time?
– Cut useless branches
– Reduce searching depth
Game Tree Size
• Tic-Tac-Toe
– b ≈ 5 legal actions per state on average, total of 9 plies in game.
– 59 = 1,953,125
• Chess
– b ≈ 35 (approximate average branching factor)
– m ≈ 100 (depth of game tree for “typical” game)
– 35100 ≈ 10154 !!
• It is usually impossible to develop the whole search tree.
α-β pruning
Max
6 Min
3 ? ?
Max ≥3
Min 3
3 12 8
α-β pruning example
Max ≥3
Min 3 ≤1
3 12 8 1
α-β pruning example
Max ≥3 ≥ 4
Min 3 ≤1 ≤12 ≤5 ≤4
3 12 8 1 12 5 4
Alpha-Beta Pruning
- Exploiting the Fact of an Adversary
• Bad = not better than we already know we can achieve
elsewhere
• If a position is provably bad
– It is NO USE expending search time to find out exactly how bad,
if you have a better alternative
• Idea
– Do depth first search to generate partial game tree,
– Give evaluation function to leaves,
– Compute bound on internal nodes.
• Update α-β bounds
– α value for max node means that max real value is at least α.
– β for min node means that min can guarantee a value no more
than β
• Prune whenever α ≥ β
– Prune below a Max node whose alpha value becomes greater
than or equal to the beta value of its ancestors.
– Prune below a Min node whose beta value becomes less than or
equal to the alpha value of its ancestors.
α-β pruning example
3 12 8 1
The α-β Pruning Algorithm
α-β Pruning Exercise 1
Max
X
2 3 5 9 0 1 7 5
α-β Pruning Exercise 2
Max
The α-β Pruning Effectiveness
• Worst-Case
– Branches are ordered so that no pruning takes place. In this
case alpha-beta gives no improvement over exhaustive search
• Best-Case
– Each player’s best move is the left-most alternative (i.e.,
evaluated first)
– In practice, performance is closer to best rather than worst-
case
• In practice it is often b(2m/3)
Reducing searching depth
• For games like chess, it
is hardly possible for you
to search to a finishing
state
hL hR
L R
AVL Trees Example
2 1
Insertion
• Consider insert(u): only nodes along the path from
root to the point of insertion may be unbalanced
• Suppose node v unbalanced, 4 cases:
– Cases 1 & 4 are mirror image symmetries with respect to v
– Cases 2 & 3 are mirror image symmetries with respect to v
The root of v’s left subtree v The root of v’s right subtree
x y
A B C D
Case 1 Case 2 Case 3 Case 4
Insertion Example
Insert Inputs: {10, 20, 30} 30
20
Insert Inputs with order: 30, 20, 10 10
30
Insert Inputs with order: 30, 10, 20 10
20
10
Insert Inputs with order: 10, 30, 20 30
20
10
Insert Inputs with order: 10, 20, 30
20
30
Insertion
• In Case 1 (or Case 4), the insertion occurs on the
“outside” (i.e., left-left or right-right); it is fixed by a
single rotation
• In Case 2 (or Case 3), the insertion occurs on the
“inside” (i.e., left-right or right-left); it is handled by
double rotations
v
left right
left x y
right left right
A B C D
Case 1 Case 2 Case 3 Case 4
(Outside) (Inside) (Inside) (Outside)
Insertion – Case 1
• Before insertion, height of subtree A = h and
height of subtree E = h
• After insertion, height of subtree A = h+1
h+2 (before)
x
(before) h h+3 (after)
h+1 E
(after) (before) h
h+2 h
(after) h+1 A B
insert here
Insertion – Case 1
• Solution: single rotation, i.e., make x as root
v
x
x
E h
h+2
h+1 A B h
A B
insert here E
Insertion – Case 1
• Only A can be x’s left subtree
v
x
x
E h
h+2
h+1 A B h
insert here v
E
Insertion – Case 1
• v must be x’s right subtree
v
x
x
E h
v h+2
h+1 A B h
insert here E
B
Insertion – Case 1
• B must be v’s left subtree
v
x
x
E h
v h+2
h+1 A B h
insert here B E
Insertion – Case 1
• After rotation: height of x = h+2
(= height of subtree rooted at v before insertion)
• The left and right subtrees of x have the same height
x
E h
h+2
x
h+1 A B h
A A
B B
B
C C A
C
x v
x
E h
h+2
v h+1 A B h
h+1
A insert here (before)
h+2
insert here h+1
h h
B E
Insertion – Case 2
• Before insertion, height of subtree B = h and height of
subtree E = h
• After insertion, height of subtree B = h+1, either B1 or B2
has height h, the other h-1
Balancing condition is NOT ok!
insert here
Insertion – Case 2
• Solution: double rotation, i.e., make z as root
• 1st rotation: make z as the root of its parent’s subtree,
i.e., its parent is x
v
x
B
z h h+3
E
h+2
h A h+1
v B1 B2
A B1 B2
Insertion – Case 2
• 1st rotation: x must be z’s left son
v
x
B
z h h+3
E
h+2
h A h+1
v B1 B2
x E
A
B1 B2
Insertion – Case 2
• 1st rotation: B2 must be z’s right subtree
v
x
B
z h h+3
E
h+2
h A h+1
v B1 B2
x E
B2
A
B1
Insertion – Case 2
• 1st rotation: B1 must be x’s right subtree
v
x
B
z h h+3
E
h+2
h A h+1
v B1 B2
v
v
z x
(2) B
z h h+3
E E
h+2
x h A h+1
B1 B2
B2
(1) insert here (before)
B1
A
Insertion – Case 2
• 2nd rotation: make z as the root
v
z
h+2 h
z ≤h E
x
B2
h ≤h
B1
A
(before)
void dbl_rotateL(Node*& s)
{
rotateR(s->lson);
x v rotateL(s);
B2
}
B1
A E
Insertion – Case 2
• 2nd rotation: x must be z’s left subtree
v
z
h+2 h
z ≤h E
x
B2
x h ≤h
B1
A
(before)
B1
A
v
B2
E
Insertion – Case 2
• 2nd rotation: v must be z’s right subtree
v
z
h+2 h
z ≤h E
x
B2
x v
h ≤h
B1
A
(before)
B1 E
A
B2
Insertion – Case 2
• 2nd rotation: B2 must be v’s left subtree
v
z
h+2 h
z ≤h E
x
B2
x v
h+1 h ≤h
B1
h h A
(before)
≤h ≤h
B1 B2
A E
z v
(2)
z
h+2 h
x v ≤h E
(1)
h+1 x
B2
h h
≤h ≤h h ≤h
B2 B1
B1 A
(before)
A E
insert here
Overall Scheme for insert(x)
• Search for x in the tree; insert a new leaf for x (as in
previous BST)
• If parent of x not balanced, perform single or double
rotation as appropriate
– How do we know the height of a subtree?
– Have a “height” attribute in each node
• Set x = parent of x and repeat the above step until x = root
struct Node
{
Node(item x):data(x), height(1),
lson(NULL), rson(NULL){}
item data;
int height; // height of subtree rooted at this
// node
Node* lson;
Node* rson;
};
k equations
T(k-1)=T(k-2)+c
… T(k-2)=T(k-3)+c
= T(0) + kc …
= O(k) T(1)=T(0)+c
T(k)=T(0)+kc
• Worst case time complexity of insert(x)
= worst case time complexity of insertR(root, x)
= O(h)
where h = height of the whole tree
Theorem
• What is h (as a function of n)?
• Theorem: Every AVL-tree of height h has
≥ αh -1 nodes
– where α= (1+sqrt(5))/2 ≈1.618 (Golden ratio)
– Note: α2 = α+1
• Proof: (by induction on h)
Base Case (h=1)
– An AVL tree of height 1 has 1 node
– and n=1 ≥ αh -1
Induction Step
– Assume every AVL-tree of height k has
k ≥ αk -1 nodes for all k<h for some h>1
– Consider an arbitrary AVL tree of height h with
n nodes
By hypothesis, this h
subtree has ≥ (αh-1 -1)
nodes h-1 h-2
n = nL + nR +1 By hypothesis, this
subtree has ≥ (αh-2 -1)
≥ (αh-1 -1) + (αh-2 -1) + 1 nodes
= αh-2 + αh-1 - 1
= αh-2 (α + 1) - 1
= αh-2 (α2) - 1 = αh - 1 (proved)
So,αh ≤ n+1, i.e., h ≤ logα(n+1) = O(logn)
Deletion
a) 2
b) 3
c) 4
d) 5
AVL Trees Exercise 3
Show the AVL tree that results after each of the integer keys
9, 27, 50, 15, 2, 21, and 36 are inserted, in that order, into an
initially empty AVL tree.
Learning Objectives 1
1. Explain the concepts of Game Trees and
Heaps
2. Able to decide the best move on a game
tree
3. Able to insert into and delete from a Min-
heap or Max-heap manually
4. Able to do α-β pruning manually