Title: Binary Search Trees (Continued)
1Binary Search Trees (Continued)
- Study Project 3 Solution
- Balanced Binary Search Trees
- Balancing Operations
- Implementing Balancing Operations
- AVL Trees
- Red/Black Trees
- Reading LC 10.1 10.9
2Study Project 3 Solution
- Project 3 was due before class today
- Discuss solution
3Balanced Binary Search Trees
- The balance of a binary search tree is important
for obtaining its efficiency - If we add 3, 5, 9, 12, 18, and 20 to a binary
search tree, we get a degenerate tree - This is less efficient than a singly linked list
because our code needs to check the null left
pointer at each level while traversing it - Operations are O(n) instead of O(log n)
- We want our binary search trees balanced
4Balanced Binary Search Trees
- Degenerate tree for a binary search tree
3
5
9
12
18
20
5Balancing Operations
- Brute force balancing methods work but are
unnecessarily time consuming - We could use an in-order traversal of the tree
and move everything out to an array - Then, we could use a recursive method to insert
the middle element of the array as the root and
subdivide the array into two halves - Eventually, we will rebuild a balanced tree
6Balancing Operations
- We prefer to use balancing operations after each
add or remove element operation - Semantics of balancing operations
- Right rotation
- Left rotation
- Rightleft rotation
- Leftright rotation
7Balancing Operations
- Semantics of Right Rotation
- A. Make the left child of the root the new root
- B. Make former root the right child of the new
root - C. Make right child of the former left child of
the former root the new left child of the former
root
13
7
7
7
15
7
13
5
13
5
13
5
10
5
3
15
10
3
15
3
15
10
10
Initial Tree
Step C
3
Step A
Step B
8Balancing Operations
- Semantics of Left Rotation
- A. Make the right child of the root the new root
- B. Make former root the left child of the new
root - C. Make left child of the former right child of
the former root the new right child of the former
root
5
10
10
10
10
3
5
13
5
13
5
13
7
13
3
15
3
15
3
7
15
7
7
Initial Tree
Step A
Step B
Step C
15
9Balancing Operations
- Semantics of Rightleft Rotation
- A. Right rotation around right child of root
- B. Left rotation around root
5
5
10
13
3
10
3
13
5
10
15
7
13
7
15
3
7
15
Initial Tree
After Right Rotation
After Left Rotation
10Balancing Operations
- Semantics of Leftright Rotation
- A. Left rotation around left child of root
- B. Right rotation around root
13
13
7
15
5
15
7
13
5
7
3
10
5
10
3
15
10
3
Initial Tree
After Left Rotation
After Right Rotation
11Implementing Balancing Operations
- Now that we know how to rebalance the tree when
needed, we must know how to detect that the tree
needs to be rebalanced - AVL trees (after Adelson-Velski and Landis)
keep a balance factor attribute in each node that
equals the height of the right sub-tree minus the
height of the left sub-tree - If a nodes balance factor is gt 1 or lt -1, the
sub-tree with that node as root needs to be
rebalanced
12Implementing Balancing Operations
- There are 2 ways for tree to become unbalanced
- By insertion of a node
- By deletion of a node
- Each time one of those occurs
- The balance factors must be updated
- The balance of the tree must be checked from the
point of change up to and including the root - It is best for AVL trees to have a parent
reference in each child node to backtrack up the
tree easily - We can now detect the need for making any one of
the balancing rotations we have already studied
13Implementing Balancing Operations
- If the balance factor of a node is -2
- If the balance factor of its left child is -1
- Perform a right rotation
- If the balance factor of its left child is 1
- Perform a leftright rotation
- If the balance factor of a node is 2
- If the balance factor of its right child is 1
- Perform a left rotation
- If the balance factor of its right child is -1
- Perform a rightleft rotation
14AVL Tree Right Rotation
Initial Tree
After Add 1
Node is -2 Left Child is -1
7 (-1)
7 (-2)
5 (0)
9 (0)
5 (-1)
9 (0)
6 (0)
3 (0)
6 (0)
3 (-1)
1 (0)
15AVL Tree Right Rotation
After Right Rotation
5 (0)
3 (-1)
7 (0)
6 (0)
1 (0)
9 (0)
16AVL Tree Rightleft Rotation
Initial Tree
After Remove 3
10 (1)
10 (2)
Node is 2 Right Child is -1
5 (-1)
15 (-1)
5 (0)
15 (-1)
Remove
13 (-1)
3 (0)
17 (0)
13 (-1)
17 (0)
11 (0)
11 (0)
17AVL Tree Rightleft Rotation
After Right Rotation
After Left Rotation
10 (2)
13 (0)
5 (0)
13 (1)
10 (0)
15 (1)
11 (0)
15 (1)
11 (0)
17 (0)
5 (0)
17 (0)
18Implementing Balancing Operations
- There is another way to detect the need to
rebalance a binary search tree - Red/Black Trees (developed by Bayer and extended
by Guibas and Sedgewick) keep a color red or
black for each node in the tree - The root is black
- All children of a red node are black
- Every path from the root to a leaf contains the
same number of black nodes
19Implementing Balancing Operations
- As with AVL trees
- The only time we need to rebalance and recolor is
after the insertion or deletion of a node - It is best for Red/Black trees to have a parent
reference in each child node to backtrack up the
tree easily - The maximum height of a Red/Black tree is roughly
2log n (not as well controlled as an AVL tree),
but traversal of the longest path is still O(log
n) - The rest of this topic is left to the student to
study
20Introduction to Project 4
- Study use of these Java Collections APIs
- HashMapltK,Vgt
- Map.EntryltK,Vgt
- PriorityQueueltTgt
- Study the concept of Huffman coding based on
character frequency - A tree with the most frequent characters in
leaves closer to the root - Following left child adds a 0 to the code
- Following right child adds a 1 to the code
21Sample Huffman Coding Tree
Symbol/Frequency E / 0.40 T / 0.35 I /
0.20 Others / 0.05
Binary Code Sequence (Read right to left)
0
10
1 110 11 111
An E which occurs 40 of the time takes only 1
binary digit Zero A T which occurs 35 of
the time takes only 2 binary digits One and
Zero An I which occurs 20 of the time takes
only 2 binary digits One and One All other
characters which only occur 5 of the time take 3
or more digits
22UML Diagram for Project 4
HuffmanTree
ltltInterfacegtgt ComparableltHuffmanNodegt
- encodeTable HashMap - decodeTable
HashMap - compressionRatio double
main(String args) void HuffmanTree
(message String) encode (message String)
String decode (message String) String
getCompressionRatio (void) double
HuffmanNode
- value String - frequency int - zero
HuffmanNode - one HuffmanNode HuffmanNode
(message String) compareTo(that
HuffmanNode) int toString(void) String
getValue(void) String getFrequency (void)
int increment (void) void getZero (void)
HuffmanNode getOne (void) HuffmanNode
java.util.HashMap java.util.Set java.util.Map.Entr
y java.util.PriorityQueue