a two-level binary expression

36
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL: 8 - 5 has value 3 PREORDER TRAVERSAL: - 8 5 POSTORDER TRAVERSAL: 8 5 -

Upload: favian

Post on 21-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

A Two-Level Binary Expression. treePtr. ‘-’. ‘8’. ‘5’. INORDER TRAVERSAL : 8 - 5 has value 3 PREORDER TRAVERSAL: - 8 5 POSTORDER TRAVERSAL: 8 5 -. Levels Indicate Precedence. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: A Two-Level Binary Expression

1

A Two-Level Binary Expression

‘-’

‘8’ ‘5’

treePtr

INORDER TRAVERSAL: 8 - 5 has value 3

PREORDER TRAVERSAL: - 8 5

POSTORDER TRAVERSAL: 8 5 -

Page 2: A Two-Level Binary Expression

2

Levels Indicate Precedence

When a binary expression tree is used to represent an expression, the levels of the nodes in the tree indicate their relative precedence of evaluation.

Operations at higher levels of the tree are evaluated later than those below them.

The operation at the root is always the last operation performed.

Page 3: A Two-Level Binary Expression

3

A Binary Expression Tree

‘*’

‘+’

‘4’

‘3’

‘2’

What value does it have? ( 4 + 2 ) * 3 = 18

What infix, prefix, postfix expressions does it represent?

Page 4: A Two-Level Binary Expression

4

Inorder Traversal: (A + H) / (M - Y)

‘/’

‘+’

‘A’ ‘H’

‘-’

‘M’ ‘Y’

tree

Print left subtree first Print right subtree last

Print second

Page 5: A Two-Level Binary Expression

5

Preorder Traversal: / + A H - M Y

‘/’

‘+’

‘A’ ‘H’

‘-’

‘M’ ‘Y’

tree

Print left subtree second Print right subtree last

Print first

Page 6: A Two-Level Binary Expression

6

‘/’

‘+’

‘A’ ‘H’

‘-’

‘M’ ‘Y’

tree

Print left subtree first Print right subtree second

Print last

Postorder Traversal: A H + M Y - /

Page 7: A Two-Level Binary Expression

7

Evaluate:

‘*’

‘-’

‘8’ ‘5’

What infix, prefix, postfix expressions does it represent?

‘/’

‘+’

‘4’

‘3’

‘2’

Page 8: A Two-Level Binary Expression

8

Answer

Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) )

Prefix: * - 8 5 / + 4 2 3

Postfix: 8 5 - 4 2 + 3 / * has operators in order used

‘*’

‘-’

‘8’ ‘5’

‘/’

‘+’

‘4’

‘3’

‘2’

Page 9: A Two-Level Binary Expression

9

InfoNode has 2 forms

enum OpType { OPERATOR, OPERAND } ;

struct InfoNode { OpType whichType; union // ANONYMOUS union {

char operation ; int operand ; }

};

. whichType . operation

OPERATOR ‘+’

. whichType . operand

OPERAND 7

Page 10: A Two-Level Binary Expression

10

TreeNodes

struct TreeNode {

InfoNode info ; // Data member

TreeNode* left ; // Pointer to left child

TreeNode* right ; // Pointer to right child};

. left . info . right

NULL 6000

. whichType . operand

OPERAND 7

Page 11: A Two-Level Binary Expression

11

class ExprTree

‘*’

‘+’

‘4’

‘3’

‘2’

ExprTree

~ExprTree

Build

Evaluate . . .

private:

root

Page 12: A Two-Level Binary Expression

int Eval ( TreeNode* ptr )

// Pre: ptr is a pointer to a binary expression tree.// Post: Function value = the value of the expression represented// by the binary tree pointed to by ptr.

{ switch ( ptr->info.whichType ) {

case OPERAND : return ptr->info.operand ;case OPERATOR : switch ( tree->info.operation ) { case ‘+’ : return ( Eval ( ptr->left ) + Eval ( ptr->right ) ) ;

case ‘-’ : return ( Eval ( ptr->left ) - Eval ( ptr->right ) ) ;

case ‘*’ : return ( Eval ( ptr->left ) * Eval ( ptr->right ) ) ;

case ‘/’ : return ( Eval ( ptr->left ) / Eval ( ptr->right ) ) ; } }}

12

Page 13: A Two-Level Binary Expression

13

A Full Binary Tree

A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children.

SHAPE OF A FULL BINARY TREE

Page 14: A Two-Level Binary Expression

14

A Complete Binary Tree

A complete binary tree is a binary tree that is either full or full through the next-to-last level, with the leaves on the last level as far to the left as possible.

SHAPE OF A COMPLETE BINARY TREE

Page 15: A Two-Level Binary Expression

15

What is a Heap?

A heap is a binary tree that satisfies these

special SHAPE and ORDER properties:

Shape: a complete binary tree.

Order: the value stored in each node is greater than or equal to the value in each of its children.

Page 16: A Two-Level Binary Expression

16

Are These Both Heaps?

C

A T

treePtr

50

20

18

30

10

Page 17: A Two-Level Binary Expression

17

70

60

40 30

12

8 10

tree

Is This a Heap?

Page 18: A Two-Level Binary Expression

18

70

60

40 30

12

8

tree

Where is the Largest Element in a Heap?

Page 19: A Two-Level Binary Expression

19

70

0

60

1

40

3

30

4

12

2

8

5

tree

Number the Nodes Left to Right by Level

Page 20: A Two-Level Binary Expression

20

70

0

60

1

40

3

30

4

12

2

8

5

tree

Use the Numbers as Array Indexes to Store the Tree

[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

70

60

12

40

30

8

tree.nodes

Page 21: A Two-Level Binary Expression

// HEAP SPECIFICATION

// Assumes ItemType is either a built-in simple data type// or a class with overloaded realtional operators.

template< class ItemType >struct HeapType

{ void ReheapDown ( int root , int bottom ) ;

void ReheapUp ( int root, int bottom ) ;

ItemType* elements ; // ARRAY to be allocated dynamically

int numElements ;

};

21

Page 22: A Two-Level Binary Expression

22

Observations

Heap is a struct with member functions

Why not a class? To allow access to its components Heaps are usually used in conjunction

with other classes which want access to the components but will hide them from the application

Page 23: A Two-Level Binary Expression

23

Reheap Down For heaps with all but the top element

in heap position How:

Swap root with left or right child Reheap (down) with that child

Used to remove the highest element: Remove the root Put last element in root position Reheap down to restore the order

Page 24: A Two-Level Binary Expression

24

Reheap Up

For a node that violates a heap up: swap with parent until it is a heap

So, if we added a node to the “end” of the tree, reheap up would put it in the right spot

To build a heap: start with empty heap add element to the end and reheap up

Page 25: A Two-Level Binary Expression

25

Exercises

Make a heap out of elements

D H A K P R Remove the top element and reheap.

Page 26: A Two-Level Binary Expression

26

// IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS

template< class ItemType >void HeapType<ItemType>::ReheapDown ( int root, int bottom )

// Pre: root is the index of the node that may violate the heap // order property// Post: Heap order property is restored between root and bottom

{ int maxChild ; int rightChild ; int leftChild ;

leftChild = root * 2 + 1 ; rightChild = root * 2 + 2 ; 26

ReheapDown

Page 27: A Two-Level Binary Expression

if ( leftChild <= bottom ) // ReheapDown continued {

if ( leftChild == bottom ) maxChild = leftChild ;else{ if (elements [ leftChild ] <= elements [ rightChild ] )

maxChild = rightChild ; else

maxChild = leftChild ;}if ( elements [ root ] < elements [ maxChild ] ){ Swap ( elements [ root ] , elements [ maxChild ] ) ; ReheapDown ( maxChild, bottom ) ;}

}}

27

Page 28: A Two-Level Binary Expression

// IMPLEMENTATION continued

template< class ItemType >void HeapType<ItemType>::ReheapUp ( int root, int bottom )

// Pre: bottom is the index of the node that may violate the heap // order property. The order property is satisfied from root to // next-to-last node.// Post: Heap order property is restored between root and bottom

{ int parent ;

if ( bottom > root ) {

parent = ( bottom - 1 ) / 2;if ( elements [ parent ] < elements [ bottom ] ){

Swap ( elements [ parent ], elements [ bottom ] ) ;ReheapUp ( root, parent ) ;

} }} 28

Page 29: A Two-Level Binary Expression

29

Priority Queue

A priority queue is an ADT with the property that only the highest-priority element can be accessed at any time.

Example: homework assignments are ordered by priority.

Like a sorted list except for the access restriction.

Goal: be able to insert in order and to access the top element efficiently.

Page 30: A Two-Level Binary Expression

ADT Priority Queue Operations

Transformers MakeEmpty Enqueue Dequeue

Observers IsEmpty IsFull

change state

observe state

30

Page 31: A Two-Level Binary Expression

// CLASS PQTYPE DEFINITION AND MEMBER FUNCTIONS //----------------------------------------------------

template<class ItemType>

class PQType {

public:

PQType( int );

~PQType ( );

void MakeEmpty( );

bool IsEmpty( ) const;

bool IsFull( ) const;

void Enqueue( ItemType item );

void Dequeue( ItemType& item );

private:

int numItems;

HeapType<ItemType> items;

int maxItems;

};

31

Page 32: A Two-Level Binary Expression

PQType

~PQType

Enqueue

Dequeue . . .

class PQType<char>

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

‘X’ ‘C’ ‘J’

Private Data:

numItems

3

maxItems

10

items

.elements .numElements

Page 33: A Two-Level Binary Expression

33

Graphs

A data structure that consists of a set of nodes (vertices) and arcs (edges) that relate the nodes to each other.

G = (V, E)

E.g. a map with V = cities and E = roads

Undirected versus directed: some of the streets may be one-way.

Weighted: each edge has a value (miles).

Page 34: A Two-Level Binary Expression

Applications

Traversal: Find a route (path) from Boston to

Chicago.

Find the shortest path.

Approaches:Depth-first search

Breadth-first search

34

Page 35: A Two-Level Binary Expression

35

Implementation viaAdjacency Matrices

For a graph with N vertices:numVertices = the number of nodes

vertices[N] = name of vertex N

edges[N] [M] = weight from vertex N to M

See p. 554. How do you add a vertex? An edge? How do you find a path?

Page 36: A Two-Level Binary Expression

36

Implementation viaAdjacency Lists

An array of vertices. Each vertex has a linked list of the

nodes extending directly from it. See p. 559. When would you use this? When not?