Anda di halaman 1dari 48

| 

 


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
    

 ^ tree ÷ is a finite nonempty set of elements


 àne of these elements is called the root
 Ôhe remaining elements, if any, are partitioned
into trees, which are called the subtrees of ÷£


¦ 

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


 Ñode degree is the number of children it has

8


J


 Ôree degree is the maximum of node degrees

tree degree = 3

9
|  

 ^ finite (possibly empty) collection of elements


 ^ nonempty binary tree has a root element and
the remaining elements (if any) are partitioned into
two binary trees
 Ôhey are called the left and right subtrees of the
binary tree

 
J


|
'

 

(|  

 ^ binary tree may be empty; a tree cannot be empty£


 Ño node in a binary tree may have a degree more than 2,
whereas there is no limit on the degree of a node in a tree£
 Ôhe subtrees of a binary tree are ordered; those of a tree
are not ordered£

? ? 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

2£ ^ binary tree of height , >= 0, has at least


and at most  -1 elements in it£

 
|  

!


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
‰ |  

 ^ full binary tree of height has exactly  -1 nodes£


 Ñumbering the nodes in a full binary tree
Ñumber the nodes 1 through  -1
Ñumber by levels from top to bottom
ithin a level, number from left to right (see Fig£ )

5 5
Ñ
Ñ 
!
 ‰ |  

 Parent of node Y is node ü(i/2) , unless i = 1


 Ñode 1 is the root and has no parent

6 6
Ñ
Ñ 
!
 ‰ |  

 Left child of node Y is node Y, unless Y  p,


where p is the total number of nodes£
 Õf Y  p, node Y has no left child£

7 7
Ñ
Ñ 
!
 ‰ |  

 ’ight child of node Y is node Y+1, unless Y+1 > p,


where n is the total number of nodes£
 Õf Y  p, node Y has no right child£

8 8
)*!
 !

|  

 omplete binary tree with 10 nodes£


 ame node number properties (as in full binary
tree) also hold here£

9 9
|  

+
!

 
 ^rray representation
 Linked representation

 2
+
!

  |  

 Ôhe binary tree is represented in an array by


storing each element at the array position
corresponding to the number assigned to it£

 2
½!

|  

Õncomplete binary trees

 omplete binary tree with some missing


elements
2 22
+  D¦
'
|  

 ^n p node binary tree needs an array whose


length is between p  and p£
 ’ight-skewed binary tree wastes the most space

 2
2 
+
!

  |  

 Ôhe most popular way to present a binary tree


 ach element is represented by a node that has
two link fields (lefthild and righthild) plus an
element field
 Ôhe space required by an p node binary tree is
p  sizeof(binaryÔreeÑode)

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

 )*!
 

 ives infix form of expression, which is how we


normally write math expressions£

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


)*!
 ! 

 ^dd and delete nodes from a queue


 àutput: a b c d e f g h i j

 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);

// deny access from trees left and right


left£root = right£root = 0;
}

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

// break the tree


element = root->data;
left£root = root->Lefthild;
right£root = root->’ighthild;

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);
}
}

template <class Ô>


void BinaryÔree<Ô>::Õnàrder(
void(*Visit)(BinaryÔreeÑode<Ô> *u), BinaryÔreeÑode<Ô> *t)
{// Õnorder traversal£
if (t) {Õnàrder(Visit, t->Lefthild);
Visit(t);
Õnàrder(Visit, t->’ighthild);
}
}
46
6
 
2



template <class Ô>
void BinaryÔree<Ô>::Postàrder(
void(*Visit)(BinaryÔreeÑode<Ô> *u), BinaryÔreeÑode<Ô> *t)
{// Postorder traversal£
if (t) {Postàrder(Visit, t->Lefthild);
Postàrder(Visit, t->’ighthild);
Visit(t);
}
}

template <class Ô>


void BinaryÔree<Ô>::Levelàrder( void(*Visit)(BinaryÔreeÑode<Ô> *u))
{// Level-order traversal£
LinkedQueue<BinaryÔreeÑode<Ô>*> Q;
BinaryÔreeÑode<Ô> *t;
t = root;
while (t) {
Visit(t);
if (t->Lefthild) Q£^dd(t->Lefthild);
if (t->’ighthild) Q£^dd(t->’ighthild);
try {Q£elete(t);}
catch (àutàfBounds) {return;}
}
7 47
}
"
 
template <class Ô>
int BinaryÔree<Ô>:: eight(BinaryÔreeÑode<Ô> *t) const
{// ’eturn height of tree *t£
if (!t) return 0; // empty tree
int hl = eight(t->Lefthild); // height of left
int hr = eight(t->’ighthild); // height of right
if (hl > hr) return ++hl;
else return ++hr;
}

8 48

Anda mungkin juga menyukai