10 Binary Trees
10 Binary Trees
Linear search can be used for lists stored in an array as well as for
linked lists. (It's the method used in the find() algorithm in STL.)
For a list of length n, its average search time will be O(n). 1
2. Binary Search
Ordered lists can be searched more efficiently using binary search:
This is true for lists stored in arrays in which step 2b can be done
simply by calculating mid = (start + last ) / 2 and array[mid] is the
middle list element. (It's the method used in the
binary_search() algorithm in STL.)
3
As we have seen, for linked lists, binary search is not practical,
because we only have direct access to the first node, and locating
any other node requires traversing the list until that node is located.
Thus step 2b requires:
i. mid = (start + last) / 2
ii. Set locPtr = first; // location of first node
iii. For loc = start to mid - 1
Set locPtr = next part of node pointed to by locPtr.
iv. locPtr points to the middle node and the data part of the node
pointed to locPtr is the middle list element.
Locating the middle node clearly negates the efficiency of binary
search for array-based lists; the computing time becomes O(n)
instead of O(log2n).
However, perhaps we could modify the linked structure to make a
binary search feasible. What would we need? 4
Direct access to the middle node:
22 33 44 55 66 77 88
and from it to the middle of the first half and to the middle of the
second half:
22 33 44 55 66 77 88
and so on:
22 33 44 55 66 77 88
5
Or if we stretch out the links to give it a tree-like shape
55
33 77
22 44 66 88
6
B. Binary Search Trees
1. Definition and Terminology:
Tree: A finite set of elements called nodes (or vertices)
and a finite set of directed arcs that connect pairs of
nodes.
If the tree is not empty, but
one every
of theother node
nodes, in the
called the
tree can
root, hasbenoreached
incoming from unique
the root
arcs, by a path (a
sequence of consecutive arcs).
7
1 root
2 3
8 9
leaves
8
2. Examples
Game trees
Decision trees
Morse code trees
• –
E T
• – • –
I A N M
9
Huffman codes -- data compression
Parse trees
1 2
M T
3 4 5 6
C E • P U
•
•
60 80
58 65 92
58 65 92
13
b. C++ Implementation:
template <typename BinTreeElement>
class BinaryTree
{
public:
// ... BinaryTree function members
private:
class BinNode // a binary tree node
{
public:
BinTreeElement data;
BinNode * left,
* right;
// ... BinNode member functions
};
typedef BinNode * BinNodePointer;
// BinaryTree data members
BinNodePointer root; // pointer to root node
14
};
5. A Binary Search Tree (BST) is a binary tree in which the
value in each node is greater than all values in its left subtree
and less than all values in its right subtree.
a. We can "binary search" a BST:
1. Set pointer locPtr = root.
2. Repeat the following:
If locPtr is null
Return false
If value < locPtr->data
locPtr = locPtr->left
Else if value > locPtr->data
locPtr = locPtr->right
Else
Return true
Search time: O(log2n) if tree is balanced. 15
b. What about traversing a binary tree?
Most easily done recursively, viewing a binary tree as a
recursive data structure:
16
Now, for traversal, consider the three operations:
V: Visit a node
L: (Recursively) traverse the left subtree of a node
R: (Recursively) traverse the right subtree of a node
We can do these in six different orders:
LVR
VLR
LRV
VRL
RVL
RLV
17
For example, LVR gives the following traversal algorithm:
If the binary tree is empty // anchor
Do nothing.
Else do the following: // inductive step
L: Call traversal to traverse the left subtree.
V: Visit the root.
R: Call traversal to traverse the right subtree.
As a member function in a BinaryTree class:
void Inorder() { InorderAux(root); }
void InorderAux(BinNodePointer r)
{
if (r != 0)
{
InorderAux(r->left); // L
Process(r->data); // V
InorderAux(r->right); // R
}
}
18
Rearranging the steps L, V, and R gives the other traversals.
75 LVR: 58, 60, 65, 75, 80, 92
The first three orders, in which the left subtree is traversed before
the right, are the most important of the six traversals and are
commonly called by other names:
LVR Inorder
VLR Preorder
LRV Postorder
Note: Inorder traversal of a BST visits the nodes
in ascending order. 19
To see why these names are appropriate, recall expression trees,
binary trees used to represent the arithmetic expressions like
a + b * c / d:
/
+ d
a *
b c
T E
M
H H
H O
E M
E T R
O
R M
R
O
22
d. What about deleting a node a BST?
Case 1: A leaf, and Case 2: 1 child
Easy — just reset link from parent
Case 3: 2 children:
1. Replace node with inorder successor X.
2. Delete X (which has 0 or 1 child)
G
A H O
E I M P
C K N
B D L 23
Some Special Kinds of Trees:
Threaded Binary Search Trees (§13.1)
AVL Trees (§13.2)
2-3-4 Trees (§13.3)
B-Trees (§13.3)
Red-Black Trees (map, set, multimap, multiset
in STL) (§13.3)
Tries
Huffman Code Trees (data compression) (§10.5)
24