Lecture10 PDF
Lecture10 PDF
1
Binary search tree
A sorted container
Other terms: Sorted binary tree
Other choices:
• Frequent: elements are identified by a key. Keys
are less than comparable
• .…
-> see sorted map, priority queue
2
Binary search tree (BST)
BST - a binary tree that has the BST Property
BST Property
For each node x :
• if y is a node in the left subtree of x , then
info(y) <= info(x)
• if z is a node in the right subtree of x , then
info(x) <= info(z)
Property:
Inorder traversal ascending order of elements
• can be used to implement a sorting algorithm. insert
all the values we wish to sort into a new BST
3
traverse it in order
BST – definitions (equivalent)
A binary tree where every node's left subtree has keys less
than the node's key, and every right subtree has keys
greater than the node's key.
xlinux.nist.gov/dads/
4
Binary search tree
BST
Average Worst case
Search O(log n) O(n)
Insert O(log n) O(n)
Delete O(log n) O(n)
5
.
TreeNode: record
info: TComparable
left: ^TreeNode
right: ^TreeNode
parent: ^TreeNode
end
6
Delete
7
Delete
Delete a leaf
Deleting a node with one child:
delete it and replace it with its child.
Deleting a node with two children:
replace value with (either)
– its in-order successor
or
– its in-order predecessor
and then delete the succ. or pred.
8
.
9
Subalg. delete(T, z)
@ collect information about nodes involved
z – node to be (logically) deleted
get y - the node to be really deleted (z or its successor)
get x – the child of y (NIL if no children)
get q – the parent of y (NIL if no parent)
@ copy information from y to z
@ remake link over the node y to delete
from child to parent parent(x) <- q (if child exists)
from parent to child
if parent of y does not exist: update root node value
else link from q to x
@delete y 10
(Nearly) Balanced BST tree
(nearly) balanced: no leaf is much farther away from the root
than any other leaf.
Different balancing schemes allow different definitions of "much
farther" and different amounts of work to keep them balanced.
Self-balancing binary search tree :
• a binary search tree
• & keep it balanced
Height-balanced tree
• AVL tree
5/8/2014 2
BST in Java.util and C++ STL
Java.util
• TreeMap
a Red-Black tree based implementation
• TreeSet
implementation based on a TreeMap
C++ STL
• map, multimap
are typically implemented as binary search trees
www.cplusplus.com
maps are usually implemented as red-black trees
en.cppreference.com/w/cpp/container/map
5/8/2014 3
Red-black tree
5/8/2014 4
Red-Black tree
A red-black tree is a binary search tree which
satisfies:
1. Every node is either red or black.
This rule is sometimes omitted,
2. The root is black. since the root can always be
changed from red to black
3. Every leaf NIL is considered black.
4. A red node have two black children.
5. For each node:
all paths from the node to descendant leaves
contain the same number of black nodes.
5/8/2014 5
Red-Black tree
• one extra information per node:
its color, which can be either RED or BLACK.
5/8/2014 6
Red-Black tree
Lemma
A red-black tree with n internal nodes
has height at most 2*log2(n + 1).
5/8/2014 7
AVL tree
An AVL tree
is a binary search tree which satisfies:
the heights of the two subtrees of any node differ by
at most one
?
5/8/2014 8
AVL tree
5/8/2014 9
Balanced trees
Operations
1. Search is O(log n) since the trees are always balanced.
2. Insertion and deletions are also O(log n)
3. Balancing adds a constant factor to the speed of
insertion/deletion.
Remark:
Use left-rotate / right-rotate for rebalance
5/8/2014 10
Rotation
5/8/2014 11
DS
TreeNode:
info: TComparable
left: ^TreeNode
right: ^TreeNode
parent:^TreeNode
end
5/8/2014 12