CH 10
CH 10
BINARY TREES
There is one empty binary tree, one binary tree with one node,
and two with two nodes:
and
These are different from each other. We never draw any part of a
binary tree to look like
These three names are chosen according to the step at which the
given node is visited.
+ log !
a b x n
a+b log x n!
– or
a × < <
b c a b c d
a – (b × c) (a < b) or (c < d)
0.5)/(2 × a)
/
0.5
x := (−b + (b 2 − 4 × a × c)
c
×
a
×
–
4
2
:=
b
b
–
x
Expression tree of the quadratic formula Data Structures and Program Design In C++
Transp. 5, Sect. 10.1, Introduction to Binary Trees 247 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Linked Binary Trees
Comparison tree:
Jim
Dot Ron
Jim
Dot Ron
// data members:
Entry data;
Binary node< Entry > *left;
Binary node< Entry > *right;
// constructors:
Binary node( );
Binary node(const Entry &x);
};
Linked Binary Tree Specifications Data Structures and Program Design In C++
Transp. 7, Sect. 10.1, Introduction to Binary Trees 249 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Constructor:
Empty:
Binary Tree Class Specification Data Structures and Program Design In C++
Transp. 10, Sect. 10.1, Introduction to Binary Trees 252 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Binary Search Trees
We always require:
No two entries in a binary search tree may have equal keys.
The binary search tree class will be derived from the binary
tree class; hence all binary tree methods are inherited.
The Binary Search Tree Class Data Structures and Program Design In C++
Transp. 12, Sect. 10.2, Binary Search Trees 254 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Tree Search
Binary node< Record > *Search tree< Record > :: search for node(
Binary node< Record >* sub root, const Record &target) const;
Pre: sub root is NULL or points to a subtree of a Search tree
Post: If the key of target is not in the subtree, a result of NULL
is returned. Otherwise, a pointer to the subtree node
containing the target is returned.
Nonrecursive version:
Auxiliary functions, tree search Data Structures and Program Design In C++
Transp. 14, Sect. 10.2, Binary Search Trees 256 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Public method for tree search:
d e
b f b f
a c e g a d g
(a) c
(b)
a a a
g b
b
e g
c
b f f
d
d c
e
c e
(c) d f
(d)
g
(e)
Binary Search Trees with the Same Keys Data Structures and Program Design In C++
Transp. 16, Sect. 10.2, Binary Search Trees 258 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Analysis of Tree Search
e e e
b b
d
(a) Insert e (b) Insert b (c) Insert d
e e
b f b f
d a d
(d) Insert f (e) Insert a
e e
b f b f
a d g a d g
c
(f) Insert g (g) Insert c
Insertion into a Binary Search Tree Data Structures and Program Design In C++
Transp. 18, Sect. 10.2, Binary Search Trees 260 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Method for Insertion
The method insert can usually insert a new node into a ran-
dom binary search tree with n nodes in O(log n) steps. It
is possible, but extremely unlikely, that a random tree may
degenerate so that insertions require as many as n steps.
If the keys are inserted in sorted order into an empty tree,
however, this degenerate case will occur.