2
2
Linear lists are useful for serially ordered data
© À 2
ays of week
onths in a year
tudents in a class
Ôrees are useful for hierarchically ordered data
orporate structure
overnment ubdivisions
oftware structure
2
J
¦
4
Ôhe element at the top of
the hierarchy is the £
lements next in the
hierarchy are the ½
of the root£
lements next in the
hierarchy are the
½
of the root,
and so on£
lements at the lowest
level of the hierarchy are
the
£
5
J
Leaves, Parent, randparent, iblings,
^ncestors, escendents
2
¦
!
¦
¦
½
J
½
¦
6
2
"
oot is at level 1 and its children are at level 2£
eight = depth = number of levels
#
$
%
&
7
Ñ
J
8
J
tree degree = 3
9
|
J
½
|
'
(|
? ? D
'
'
D
'
'
|
)*!
xpression Ôrees
2 2
|
!
1£ Ôhe drawing of every binary tree with n elements,
p > 0, has exactly p-1 edges
For fig a) n=7 dges=7-1=6
edge
|
!
3£ Ôhe height of a binary tree that contains p
elements, p >= 0, is at least (6 (p+1))Î and at
most p£
*
4 4
|
5 5
Ñ
Ñ
!
|
6 6
Ñ
Ñ
!
|
7 7
Ñ
Ñ
!
|
8 8
)*!
!
|
9 9
|
+
!
^rray representation
Linked representation
2
+
!
|
2
½!
|
2
2
+
!
|
4 24
2
+
!
|
5 25
Ñ
2
|
!
,½-½|
.
!
,½-
½|
Ñ
! ½
|
Ñ
2
+ /.
|
Ñ
½(
.
2
+ /.
|
Ñ
½(
|
Ñ
0|
Ñ
0
.
2
.
+ .
!
.
|
Ñ
,-02
0+ .11
.
6 26
|
!
etermine the height
etermine the number of nodes
ake a copy
etermine if two binary trees are identical
isplay the binary tree
elete a tree
Õf it is an expression tree, evaluate the expression
Õf it is an expression tree, obtain the
parenthesized form of the expression
7 27
|
any binary tree operations are done by
performing a
of the binary tree
Õn a traversal, each element of the binary tree is
exactly once
uring the visit of an element, all actions (make
a copy, display, evaluate the operator, etc£) with
respect to this element are taken
8 28
|
Ôhe root of the subtree is processed first before going
into the left then right subtree (root, left, right)£
^fter the complete processing of the left subtree the root
is processed followed by the processing of the complete
right subtree (left, root, right)£
Ôhe root is processed only after the complete processing
of the left and right subtree (left, right, root)£
2
Ôhe tree is processed by levels£ o first all nodes on
level i are processed from left to right before the first
node of level i+1 is visited
9 29
template<class Ô> // Program 11£2
void preàrder(binaryÔreeÑode<Ô> *t)
{
if (t != Ñ LL) {
visit(t); // visit tree root
preàrder(t->lefthild); // do left subtree
preàrder(t->righthild); // do right subtree
}
}
)*!
!
a b d g h e i c f j
)*!
/ * + a b - c d + e f
ives prefix form of expression£
2 2
template<class Ô> // Program 11£3
void inàrder(binaryÔreeÑode<Ô> *t)
{
if (t != Ñ LL) {
inàrder(t->lefthild); // do left subtree
visit(t); // visit tree root
inàrder(t->righthild); // do right subtree
}
}
)*!
!
g d h b e i a f j c
4 4
2
½ ¦
5 5
)*!
6 6
template<class Ô> // Program 11£
void postàrder(binaryÔreeÑode<Ô> *t)
{
if (t != Ñ LL) {
postàrder(t->lefthild); // do left subtree
postàrder(t->righthild); // do right subtree
visit(t); // visit tree root
}
}
7 7
)*!
!
g h d i e b j f c a
8 8
)*!
a b + c d - * e f + /
ives postfix form of expression£
9 9
2
template <class Ô> // Program 11£7
void levelàrder(binaryÔreeÑode<Ô> *t)
{// Level-order traversal of *t£
linkedQueue<binaryÔreeÑode<Ô>*> Q;
while (t != Ñ LL) {
visit(t); // visit t
if (t->lefthild) q£push(t->lefthild); // put t's children
if (t->righthild) q£push(t->righthild); // on queue
try {t = q£front();} // get next node to visit
catch (queuempty) {return;}
q£pop()
}
}
4
2
)*!
!
4
|
)*
template<class Ô>
class BinaryÔree {
public:
BinaryÔree() {root = 0;};
~BinaryÔree(){};
bool Õsmpty() const
{return ((root) ? false : true);}
bool oot(Ô x) const;
void akeÔree(const Ô element, BinaryÔree<Ô> left, BinaryÔree<Ô> right);
void BreakÔree(Ô element, BinaryÔree<Ô> left, BinaryÔree<Ô> right);
void Preàrder(void(*Visit)(BinaryÔreeÑode<Ô> *u))
{Preàrder(Visit, root);}
void Õnàrder(void(*Visit)(BinaryÔreeÑode<Ô> *u))
{Õnàrder(Visit, root);}
void Postàrder(void(*Visit)(BinaryÔreeÑode<Ô> *u))
{Postàrder(Visit, root);}
void Levelàrder(void(*Visit)(BinaryÔreeÑode<Ô> *u));
2 42
void Preàutput() {Preàrder(àutput, root); cout << endl;}
void Õnàutput() {Õnàrder(àutput, root); cout << endl;}
void Postàutput() {Postàrder(àutput, root); cout << endl;}
void Levelàutput() {Levelàrder(àutput); cout << endl;}
void elete() {Postàrder(Free, root); root = 0;}
int eight() const {return eight(root);}
private:
BinaryÔreeÑode<Ô> *root; // pointer to root
void Preàrder(void(*Visit)
(BinaryÔreeÑode<Ô> *u), BinaryÔreeÑode<Ô> *t);
void Õnàrder(void(*Visit)
(BinaryÔreeÑode<Ô> *u), BinaryÔreeÑode<Ô> *t);
void Postàrder(void(*Visit)
(BinaryÔreeÑode<Ô> *u), BinaryÔreeÑode<Ô> *t);
static void Free(BinaryÔreeÑode<Ô> *t) {delete t;}
static void àutput(BinaryÔreeÑode<Ô> *t)
{cout << t->data << ' ';}
static void ^dd1(BinaryÔreeÑode<Ô> *t) {_count++;}
int eight(BinaryÔreeÑode<Ô> *t) const;
};
4
template<class Ô>
bool BinaryÔree<Ô>::oot(Ô x) const
{// eturn root data in x£
// eturn false if no root£
if (root) {x = root->data;
return true;}
else return false; // no root
}
template<class Ô>
void BinaryÔree<Ô>::akeÔree(const Ô element,
BinaryÔree<Ô> left, BinaryÔree<Ô> right)
{// ombine left, right, and element to make new tree£
// left, right, and this must be different trees£
// create combined tree
root = new BinaryÔreeÑode<Ô> (element, left£root, right£root);
4 44
|
template<class Ô>
void BinaryÔree<Ô>::BreakÔree(Ô element,
BinaryÔree<Ô> left, BinaryÔree<Ô> right)
{// left, right, and this must be different trees£
// check if empty
if (!root) throw BadÕnput(); // tree empty
delete root;
root = 0;
}
5 45
template<class Ô>
void BinaryÔree<Ô>::Preàrder(
void(*Visit)(BinaryÔreeÑode<Ô> *u), BinaryÔreeÑode<Ô> *t)
{// Preorder traversal£
if (t) {Visit(t);
Preàrder(Visit, t->Lefthild);
Preàrder(Visit, t->ighthild);
}
}
8 48