# 1 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.

## Presentation on theme: "1 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."— Presentation transcript:

1 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

2 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

3 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: n Its shape must be a complete binary tree. n For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.

// HEAP SPECIFICATION // Assumes ItemType is either a built-in simple data type // or a class with overloaded realtional operators. template struct HeapType { void ReheapDown ( int root, int bottom ) ; void ReheapUp ( int root, int bottom ) ; ItemType* elements ; // ARRAY to be allocated dynamically int numElements ; }; 4

5 // IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS template void HeapType ::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 ; 5 ReheapDown

if ( leftChild <= bottom )// ReheapDown continued { if ( leftChild == bottom ) maxChild = leftChld ; else { if (elements [ leftChild ] <= elements [ rightChild ] ) maxChild = rightChild ; else maxChild = leftChild ; } if ( elements [ root ] < elements [ maxChild ] ) { Swap ( elements [ root ], elements [ maxChild ] ) ; ReheapDown ( maxChild, bottom ) ; } 6

// IMPLEMENTATIONcontinued template void HeapType ::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 ) ; } 7

8 What is a Queue? l Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). l A queue is a FIFO “first in, first out” structure.

Queue ADT Operations l MakeEmpty -- Sets queue to an empty state. l IsEmpty -- Determines whether the queue is currently empty. l IsFull -- Determines whether the queue is currently full. l Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue. l Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item. 9

ADT Queue Operations Transformers n MakeEmpty n Enqueue n Dequeue Observers n IsEmpty n IsFull change state observe state 10

11 DYNAMIC ARRAY IMPLEMENTATION QueType ~QueType Enqueue Dequeue. class QueType Private Data: front 1 rear 4 maxQue 5 items ‘C’ ‘X’ ‘J’ items [0] [1] [2] [3] [4] RESERVED

//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE #include "ItemType.h" // for ItemType template class QueType { public: QueType( ); QueType( int max );// PARAMETERIZED CONSTRUCTOR ~QueType( ) ;// DESTRUCTOR... bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); private: int front; int rear; int maxQue; ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION }; 12

//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d //-------------------------------------------------------- template QueType ::QueType( int max ) // PARAMETERIZED { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; // dynamically allocates } template bool QueType ::IsEmpty( ) { return ( rear == front ) } 13

//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d //-------------------------------------------------------- template QueType ::~QueType( ) { delete [ ] items; // deallocates array }. template bool QueType ::IsFull( ) {// WRAP AROUND return ( (rear + 1) % maxQue == front ) } 14

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

ADT Priority Queue Operations Transformers n MakeEmpty n Enqueue n Dequeue Observers n IsEmpty n IsFull change state observe state 16

ADT Queue Operations Transformers n MakeEmpty n Enqueue n Dequeue Observers n IsEmpty n IsFull change state observe state 17

// CLASS PQTYPE DEFINITION AND MEMBER FUNCTIONS //-------------------------------------------------------- #include "bool.h" #include "ItemType.h" // for ItemType template 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 items; int maxItems; }; 18

PQType ~PQType Enqueue Dequeue. class PQType [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ‘X’ ‘C’ ‘J’ Private Data: numItems 3 maxItems 10 items.elements.numElements

Download ppt "1 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."

Similar presentations