C++ Plus Data Structures

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the.
Heaps CS 308 – Data Structures.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Heaps and Priority Queues CS 3358 – Data Structures.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Data Structure By Amee Trivedi.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
CS 201 Data Structures and Algorithms
C++ Plus Data Structures
Trees Another Abstract Data Type (ADT)
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
Chapter 21: Binary Trees.
C++ Plus Data Structures
Trees Another Abstract Data Type (ADT)
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
C++ Plus Data Structures
Trees Another Abstract Data Type (ADT)
Binary Trees, Binary Search Trees
Priority Queues & Heaps
Chapter 9 Priority Queues and Sets
Heaps and Priority Queues
BINARY TREE CSC248 – Data Structure.
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
C++ Plus Data Structures
Chapter 9 The Priority Queue ADT
Binary Tree Traversal.
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

C++ Plus Data Structures Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus

A Binary Expression Tree is . . . A special kind of binary tree in which: 1. Each leaf node contains a single operand, 2. Each nonleaf node contains a single binary operator, and 3. The left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator at the root of the subtree.

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

A Binary Expression Tree ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ What value does it have? ( 4 + 2 ) * 3 = 18

A Binary Expression Tree ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ What infix, prefix, postfix expressions does it represent?

A Binary Expression Tree ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ Infix: ( ( 4 + 2 ) * 3 ) Prefix: * + 4 2 3 Postfix: 4 2 + 3 * has operators in order used

Inorder Traversal: (A + H) / (M - Y) Print second tree ‘/’ ‘+’ ‘-’ ‘A’ ‘H’ ‘M’ ‘Y’ Print left subtree first Print right subtree last

Preorder Traversal: / + A H - M Y Print first tree ‘/’ ‘+’ ‘-’ ‘A’ ‘H’ ‘M’ ‘Y’ Print left subtree second Print right subtree last

Postorder Traversal: A H + M Y - / Print last tree ‘/’ ‘+’ ‘-’ ‘A’ ‘H’ ‘M’ ‘Y’ Print left subtree first Print right subtree second

Evaluate this binary expression tree ‘*’ ‘-’ ‘/’ ‘+’ ‘4’ ‘3’ ‘2’ ‘8’ ‘5’ What infix, prefix, postfix expressions does it represent?

A binary expression tree ‘*’ ‘-’ ‘8’ ‘5’ ‘/’ ‘+’ ‘4’ ‘3’ ‘2’ Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) ) Prefix: * - 8 5 / + 4 2 3 Postfix: 8 5 - 4 2 + 3 / * has operators in order used

InfoNode has 2 forms OPERATOR ‘+’ OPERAND 7 enum OpType { OPERATOR, OPERAND } ; struct InfoNode { OpType whichType; union // ANONYMOUS union { char operation ; int operand ; } }; . whichType . operation OPERATOR ‘+’ . whichType . operand OPERAND 7

Each node contains two pointers struct TreeNode { InfoNode info ; // Data member TreeNode* left ; // Pointer to left child TreeNode* right ; // Pointer to right child }; NULL 6000 . whichType . operand OPERAND 7 . left . info . right

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

class ExprTree ‘3’ ‘2’ ‘4’ ExprTree ‘*’ ~ExprTree ‘+’ Build Evaluate . private: root

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

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

What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: Its shape must be a complete binary tree. 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.

Are these both heaps? C A T treePtr 50 20 18 30 10

Is this a heap? tree 70 60 12 40 30 8 10

Where is the largest element in a heap always found? tree 70 60 12 40 30 8

We can number the nodes left to right by level this way tree 70 60 1 12 2 40 3 30 4 8 5

And use the numbers as array indexes to store the tree tree.nodes [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 70 60 1 40 3 30 4 12 2 8 5 tree

// Assumes ItemType is either a built-in simple data type // 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 ; }; 25

ReheapDown // 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

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

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

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 MakeEmpty Enqueue Dequeue Observers IsEmpty IsFull change state observe state 30

// CLASS PQTYPE DEFINITION AND MEMBER FUNCTIONS //-------------------------------------------------------- #include "bool.h" #include "ItemType.h" // for ItemType 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

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