Topic Binary Trees (Non-Linear Data Structures)

Similar documents
Binary Trees, Binary Search Trees

March 20/2003 Jayakanth Srinivasan,

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Binary Trees. Height 1

TREES. Trees - Introduction

Binary Trees and Binary Search Trees

Chapter 20: Binary Trees

Tree Data Structures CSC 221

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Trees. (Trees) Data Structures and Programming Spring / 28

CSI33 Data Structures

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Data and File Structures Laboratory

Introduction to Computers and Programming. Concept Question

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Trees, Binary Trees, and Binary Search Trees

Principles of Computer Science

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

An Introduction to Trees

Stacks, Queues and Hierarchical Collections

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

DATA STRUCTURES USING C

CE 221 Data Structures and Algorithms

BBM 201 Data structures

[ DATA STRUCTURES ] Fig. (1) : A Tree

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

12 Abstract Data Types

IX. Binary Trees (Chapter 10)

CS302 - Data Structures using C++

Trees. Trees. CSE 2011 Winter 2007

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Data Structure - Binary Tree 1 -

CS302 - Data Structures using C++

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Binary Trees

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

Topic 14. The BinaryTree ADT

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Postfix (and prefix) notation

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Programming II (CS300)

LECTURE 13 BINARY TREES

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

H.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12)

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

CSE 230 Intermediate Programming in C and C++ Binary Tree

Cpt S 122 Data Structures. Data Structures Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Tree traversals and binary trees

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Largest Online Community of VU Students

CS302 - Data Structures using C++

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Data Structures and Algorithms for Engineers

Tree Applications. Processing sentences (computer programs or natural languages) Searchable data structures

Heaps & Priority Queues. (Walls & Mirrors - Remainder of Chapter 11)

CS200: Trees. Rosen Ch & 11.3 Prichard Ch. 11. CS200 - Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.


Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

CSI33 Data Structures

INF2220: algorithms and data structures Series 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

A set of nodes (or vertices) with a single starting point

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

CS 206 Introduction to Computer Science II

CS61B Lecture #20: Trees. Last modified: Wed Oct 12 12:49: CS61B: Lecture #20 1

tree nonlinear Examples

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

CSCI2100B Data Structures Trees

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

Algorithms and Data Structures CS-CO-412

Trees. A tree is a directed graph with the property

Data Structures And Algorithms

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Advanced Tree Data Structures

Transcription:

Topic Binary Trees (Non-Linear Data Structures) CIS210 1

Linear Data Structures Arrays Linked lists Skip lists Self-organizing lists CIS210 2

Non-Linear Data Structures Hierarchical representation? Trees General Trees Binary Trees Search Trees Balanced Trees Heaps.. CIS210 3

General Trees CIS210 4

A (Rooted) Tree? A finite, nonempty set of nodes and edges s.t. One special node (the root of the tree) Each node may be associated (edge) with one or more different nodes (its children). Each node except the root has exactly one parent. The root node has no parent (no incoming edge). There exists a unique path from the root to any other node! CIS210 5

General Rooted Trees root... CIS210 6

Example: (General Rooted) Trees? CIS210 7

More Terminology Path A sequence of nodes. Level of a node Height of a node The number of nodes in the longest path from that node to a leaf. Height of a tree The height of the root node. CIS210 8

An N-ary Tree? Each node may be associated (edge) with exactly N different nodes (its children). If the set is empty (no node), then an empty N-ary tree. CIS210 9

Example: N-ary Trees (N=3) * Note: = Empty tree! CIS210 10

The World of Trees General Trees Exact N children N-ary Trees CIS210 11

An Ordered Tree? A rooted tree in which the children of each node are ordered. first child, second child, third child, etc. Most practical implementations of trees define an implicit ordering of the subtrees. CIS210 12

Example: Ordered Trees CIS210 13

Different Views on Trees We may view trees as A mathematical construct. A data structure. An abstract data type. CIS210 14

Binary Trees CIS210 15

Binary Trees A binary tree is an ordered N-ary tree where N=2! root T left T right CIS210 16

Example: Binary Trees * Note: = Empty tree! CIS210 17

Example: Binary Trees? CIS210 18

Two Different Binary Trees CIS210 19

Quiz How many different binary trees with 3 nodes? 5 How many different binary trees with 4 nodes? 14 CIS210 20

Quiz What is the range of possible heights of a binary tree with 3 nodes? 2 to 3 What is the range of possible heights of a binary tree with 100 nodes? 7 to 100 CIS210 21

The World of Trees General Trees N-ary Trees N=2 Binary Trees CIS210 22

Different Shapes of Binary Trees Short & Fat binary trees A full binary tree A complete binary tree A balanced binary tree Tall & Skinny binary trees A skewed binary tree CIS210 23

A Full Binary Tree A binary tree in which All of the leaves are on the same level. (at level h for the binary tree of height h.) Every nonleaf node has exactly two children. CIS210 24

Shape of A Full Binary Tree The basic shape of a full binary tree is triangular! CIS210 25

Example: Full Binary Trees CIS210 26

A Complete Binary Tree A binary that is Either full binary tree Or full through the next-to-last level, with the leaves on the last level as far to the left as possible (filled in from left to right). A binary tree in which all leaf nodes are at level n or n-1, and all leaves at level n are towards the left. A binary tree of height h that is full to level h-1 and has level h filled from left to right. CIS210 27

Shape of A Complete Binary Tree The basic shape of a complete binary tree is like CIS210 28

Example: Complete Binary Trees? CIS210 29

Example: Complete Binary Trees? CIS210 30

A (Height) Balanced Binary Tree A binary tree in which the left and right subtrees of any node have heights that differ by at most 1. CIS210 31

Example: (Height) Balanced Binary Trees CIS210 32

A Skewed Binary Tree A degenerate binary tree A skewed binary tree is expensive to process! CIS210 33

Example: Skewed Binary Trees CIS210 34

Quiz Is a full binary tree complete? Yes! Is a complete binary tree full? No! Is a full binary tree balanced? Yes! Is a complete binary tree balanced? Yes! CIS210 35

Properties of Binary Trees What is the maximum height of a binary tree with n nodes? n The maximum height of a binary tree with n nodes is n. CIS210 36

Properties of Binary Trees What is the minimum height of a binary tree with n nodes? log (n+1) where the ceiling of log (n+1) = log (n+1) rounded up. The minimum height of a binary tree with n nodes is log (n+1). (The ceiling of log (n+1) = log (n+1) rounded up.) CIS210 37

Properties of Binary Trees The height of a binary tree with n nodes is at least log (n+1) and at most n. CIS210 38

Properties of Binary Trees What is the minimum number of nodes that a binary tree of height h can have? h The minimum number of nodes that a binary tree of height h can have is h. CIS210 39

Properties of Binary Trees What is the maximum number of nodes that a full binary tree of height h can have? 2 h - 1 The maximum number of nodes that a binary tree of height h can have is 2 h - 1. CIS210 40

Properties of Binary Trees Full binary trees and complete binary trees have the minimum height! Skewed (degenerate) binary trees have the maximum height! CIS210 41

Representation of Binary Trees CIS210 42

How to Represent a Binary Tree? An array-based representation An array-based representation for complete binary trees A pointer-based representation CIS210 43

1. An Array-Based Representation Represent a node in the binary tree as a structure A data Two indexes (One for each child) Represent the binary tree as an array of structures. CIS210 44

An Array-Based Representation const int MAX_NODES = 100; struct BTnode { typedef string DataType; DataType Data; int Lchild; int Rchild; }; BTnode BT[MAX_NODES]; int Root; int Free; CIS210 45

An Array-Based Representation Jane Bob Tom Alan Ellen Nancy BT 0 1 2 3 4 5 6... Data Lchild Rchild Root Jane 1 2 Bob 3 4 Tom 5-1 Alan -1-1 Ellen -1-1 Nancy -1-1 0 Free 6 CIS210 46

2. An Array-Based Representation of a Complete Binary Tree A better array-based representation for a complete binary tree! Represent a node in the binary tree as A data Represent the binary tree as an array. CIS210 47

An Array-Based Representation of a Complete Binary Tree const int MAX_NODES = 100; typedef string DataType; DataType BT[MAX_NODES]; int Root; CIS210 48

An Array-Based Representation of a Complete Binary Tree BT Data Jane Bob Tom Alan Ellen Nancy 0 1 2 3 4 5 6... Jane Bob Tom Alan Ellen Nancy Root BT[0] CIS210 49

An Array-Based Representation of a Complete Binary Tree Any node BT[ i ] Its left child = BT [ 2 * i + 1 ] Its left child = BT [ 2 * i + 2 ] Its parent = BT [ (i -1) / 2 ] Must maintain it as a complete binary tree! Limited delete! CIS210 50

3. A Pointer-Based Representation Represent a node in the binary tree as a structure A data Two pointers (One for each child) Represent the binary tree as a linked structure. CIS210 51

A Pointer-Based Representation Root Jane Jane Bob Tom Bob Tom Alan Ellen Nancy Alan Ellen Nancy CIS210 52

A Pointer-Based Representation struct BTnode { typedef string DataType; DataType Data; BTnode* LchildPtr; BTnode* RchildPtr; }; BTnode* rootbt; CIS210 53

A Pointer-Based Representation using Template template<class DataType> struct BTnode { DataType Data; BTnode<DataType>* LchildPtr; BTnode<DataType>* RchildPtr; }; BTnode<DataType>* rootbt; CIS210 54

A Pointer-Based Representation using Template Class template<class DataType> class BTnode { Public: BTnode(); BTnode(DataType D, BTnode<DataType>* l, BTnode<DataType>* r) :data(d), LchildPtr(l), RchildPtr(r) { } friend class BT<DataType>; private: DataType data; BTnode<DataType>* LchildPtr; BTnode<DataType>* RchildPtr; }; CIS210 55

A Pointer-Based Representation using Template Class template<class DataType> class BT { Public: BT(); private: BTnode<DataType>* rootbt; }; CIS210 56

Binary Trees as ADTs CIS210 57

Operations on Binary Trees Create an empty binary tree. Create a one-node binary tree, given an item. Create a binary tree, given an item for its root and two binary trees for the root s left and right subtrees. Attach a left or right child to the binary tree s root. Attach a left or right subtree to the binary tree s root. Detach a left or right subtree to the binary tree s root. Destroy a binary tree. CIS210 58

More Operations on Binary Trees... Determine whether a binary tree empty? Determine or change the data in the binary tree s root. Return a copy of the left or right subtree of the binary tree s root. Traverse the nodes in a binary tree in preorder, inorder or postorder. CIS210 59

Traversal Operations on Binary Trees It is frequently necessary to examine every node exactly once in a binary tree. Binary tree traversal is the process of Visiting systematically all the nodes of a binary tree and Performing a task (calling a visit procedure like print). CIS210 60

Traversal Operations on Binary Trees Two essential approaches: Depth-first traversal Breadth-first traversal CIS210 61

Possible Depth-First Traversals Six possible ways to arrange those tasks: 1. Process a node, then left-child subtree, then right-child subtree. 2. Process left-child subtree, then a node, then right-child subtree. 3. Process left-child subtree, then right-child subtree, then a node. 4. Process a node, then right -child subtree, then left-child subtree. 5. Process right -child subtree, then a node, then left-child subtree. 6. Process right -child subtree, then left-child subtree, then a node. In almost all cases, the subtrees are analyzed left to right! CIS210 62

Common Binary Tree Traversals Three ways to arrange those tasks: 1. Process a node, then its left-child subtree, then its right-child subtree. Preorder Traversal 2. Process its left-child subtree, then a node, then its right-child subtree. Inorder Traversal 3. Process its left-child subtree, then its right-child subtree, then a node. Postorder Traversal CIS210 63

1. Pre-order Traversal If the tree is not empty then Visit the root Preorder traverse the left subtree recursively Preorder traverse the right subtree recursively CIS210 64

Pre-order Traversal - Processing Order 1 2 5 3 4 6 7 CIS210 65

Example: Preorder traversal A B D A B C D E F G H I C E H F G I CIS210 66

Preorder Traversal & Print void preorder_print(btnode* rootbt) { if (rootbt!= NULL) { cout << rootbt->data << endl; preorder_print(rootbt-> LchildPtr); preorder_print(rootbt-> RchildPtr); } } CIS210 67

Preorder Traversal and Operation typedef void (*ftype)(datatype& AnItem); void preorder(btnode* rootbt, ftype Op) { if (rootbt!= NULL) { Op(rootBT->Data); preorder(rootbt-> LchildPtr); preorder(rootbt-> RchildPtr); } } CIS210 68

2. In-order Traversal If the tree is not empty then Inorder traverse the left subtree recursively Visit the root Inorder traverse the right subtree recursively CIS210 69

In-order Traversal - Processing Order 4 2 6 1 3 5 7 CIS210 70

Example: Inorder traversal A B D B C A F E G D I H C E H F G I CIS210 71

Inorder Traversal & Print void inorder_print(btnode* rootbt) { if (rootbt!= NULL) { inorder_print(rootbt-> LchildPtr); cout << rootbt->data << endl; inorder_print(rootbt-> RchildPtr); } } CIS210 72

Inorder Traversal and Operation typedef void (*ftype)(datatype& AnItem); void inorder(btnode* rootbt, ftype Op) { if (rootbt!= NULL) { inorder(rootbt-> LchildPtr); Op(rootBT->Data); inorder(rootbt-> RchildPtr); } } CIS210 73

3. Post-order Traversal If the tree is not empty then Postorder traverse the left subtree recursively Postorder traverse the right subtree recursively Visit the root CIS210 74

Post-order Traversal - Processing Order 7 3 6 1 2 4 5 CIS210 75

Example: Postorder traversal A B D C B F G E I H D A C E H F G I CIS210 76

Postorder Traversal & Print void postorder_print(btnode* rootbt) { if (rootbt!= NULL) { postorder_print(rootbt-> LchildPtr); postorder_print(rootbt-> RchildPtr); cout << rootbt->data << endl; } } CIS210 77

Postorder Traversal and Operation typedef void (*ftype)(datatype& AnItem); void postorder(btnode* rootbt, ftype Op) { if (rootbt!= NULL) { postorder(rootbt-> LchildPtr); postorder(rootbt-> RchildPtr); Op(rootBT->Data); } } CIS210 78

5. Backward In-order Traversal 5. Process right-child subtree, then a node, then leftchild subtree. If the tree is not empty then Backward Inorder traverse the right subtree recursively Visit the root Backward Inorder traverse the left subtree recursively CIS210 79

Backward In-order Traversal - Processing Order 4 6 2 7 5 3 1 CIS210 80

Example: Backward Inorder Traversal A B D H I D G E F A C B C E H F G I CIS210 81

Example: Backward Inorder Traversal B C F A E G D I H H I D G E F A C B A D B H E C I G F CIS210 82

Example: Backward Inorder traversal B C F A E G D I H A D B H E C I G F CIS210 83

Example: Backward Inorder traversal A B A D B D C F E G I H C E H F G I CIS210 84

Breadth-First Traversal of Binary Trees CIS210 85

Breadth-First (Levelorder)Traversal If the tree is not empty then then visit the nodes in the order of their level (depth) in the tree. Visit all the nodes at depth zero (the root). Then, all the nodes from left to right at depth one Then, all the nodes from left to right at depth two Then, all the nodes from left to right at depth three And so on... CIS210 86

Level-order Traversal - Processing Order 1 2 3 4 5 6 7 CIS210 87

Example: Levelorder traversal A B D A B D C E H F G I C E H F G I CIS210 88

Expression Tree ADT CIS210 89

Algebraic Expressions: Notations Algebraic expressions Fully parenthesized Infix notation Not fully parenthesized Infix notation Postfix notation Prefix notation CIS210 90

1. Fully Parenthesized Infix Notation We need to place parentheses around each pair of operands together with their operator! Examples: (1+2) (1+(2 * 3)) ((1 + 2) * 3) ( (a / b) + ( (c - d) * e ) ) Inconvenient! CIS210 91

2. Not Fully Parenthesized Infix Notation We can omit unnecessary parentheses! Examples: (1+2) 1 + 2 (1+ (2 * 3)) 1+ 2 * 3 ((1 + 2) * 3) (1 + 2) * 3 ( (a / b) + ( (c - d) * e ) ) a / b + (c - d) * e Convenient, BUT, we need rules to interpret correctly. CIS210 92

Not Fully Parenthesized Infix Notation Operator precedence rule * / higher than + - Operator association rule Associate from left to right Examples: (Interpretation using rules) 1 + 2 (1+2) 1+ 2 * 3 (1+ (2 * 3)) (1 + 2) * 3 ((1 + 2) * 3) a / b + (c - d) * e ( (a / b) + ( (c - d) * e ) ) CIS210 93

3. Postfix Notation Postfix Notation: <postfix> := <id> (<postfix> <postfix> <op> ) <op> := + - * / <id> := <variable> <number> Examples: (1+2) 1+ 2 1 2 + (1+ (2 * 3)) 1+ 2 * 3 1 2 3 * + ((1 + 2) * 3) (1 + 2) * 3 1 2 + 3 * ( (a / b) + ( (c - d) * e ) ) a / b + (c - d) * e a b / c d - e * + CIS210 94

4. Prefix Notation Postfix Notation: <prefix> := <id> (<op> <prefix> <prefix>) <op> := + - * / <id> := <variable> <number> Examples: (1+2) 1+ 2 + 1 2 (1+ (2 * 3)) 1+ 2 * 3 + 1 * 2 3 ((1 + 2) * 3) (1 + 2) * 3 * + 1 2 3 ( (a / b) + ( (c - d) * e ) ) a / b + (c - d) * e + / a b * - c d e CIS210 95

Postfix and Prefix Notations Postfix and prefix notations do not need! Parentheses Operator precedence rules Operator association rules CIS210 96

Algebraic Expressions as Expression Trees Algebraic expressions involving binary operations can be represented by labeled binary trees. Expression trees represent algebraic expressions! Op Opd1 Op Opd2 Opd1 Opd2 CIS210 97

Algebraic Expression a / b + (c - d) * e ( (a / b) + ( (c - d) * e ) ) a b / c d - e * + + / a b * - c d e CIS210 98

Expression Tree for a / b + (c - d) * e + / * a b - e c d CIS210 99

Expression Tree ADT // exprtree.h // Class declarations for the linked implementation of the // Expression Tree ADT //-------------------------------------------------------------------- class ExprTree; // Forward declaration of the ExprTree class class ExprTreeNode // Facilitator class for the ExprTree class { private: // Constructor ExprTreeNode ( char elem, ExprTreeNode *leftptr, ExprTreeNode *rightptr ); // Data members char dataitem; // Expression tree data item ExprTreeNode *left, // Pointer to the left child *right; // Pointer to the right child friend class ExprTree; }; CIS210 100

Expression Tree ADT class ExprTree { public: // Constructor ExprTree (); // Destructor ~ExprTree (); // Expression tree manipulation operations void build (); // Build tree from prefix expression void expression () const; // Output expression in infix form float evaluate () const; // Evaluate expression void clear (); // Clear tree CIS210 101

Expression Tree ADT // Output the tree structure -- used in testing/debugging void showstructure () const; ExprTree ( const ExprTree &valuetree ); // Copy constructor CIS210 102

Expression Tree ADT private: // Recursive partners of the public member functions -- insert // prototypes of these functions here. void buildsub ( ExprTreeNode *&p ); void exprsub ( ExprTreeNode *p ) const; float evaluatesub ( ExprTreeNode *p ) const; void clearsub ( ExprTreeNode *p ); void showsub ( ExprTreeNode *p, int level ) const; // Data member ExprTreeNode *root; // Pointer to the root node }; CIS210 103

Expression Tree ADT //-------------------------------------------------------------------- // exprtree.cpp //-------------------------------------------------------------------- ExprTreeNode:: ExprTreeNode ( char nodedataitem, ExprTreeNode *leftptr, ExprTreeNode *rightptr ) // Creates an expreesion tree node containing // data item nodedataitem, // left child pointer leftptr, and right child pointer rightptr. : dataitem(nodedataitem), left(leftptr), right(rightptr) {} CIS210 104

Expression Tree ADT ExprTree:: ExprTree () // Creates an empty expression tree. : root(0) {} CIS210 105

Expression Tree ADT //-------------------------------------------------------------------- ExprTree:: ~ExprTree () // Frees the memory used by an expression tree. { clear(); } void ExprTree:: clear () // Removes all the nodes from an expression tree. { } clearsub(root); root = 0; CIS210 106

Expression Tree ADT void ExprTree:: clearsub ( ExprTreeNode *p ) // Recursive partner of the clear() function. Clears the subtree // pointed to by p. { } if ( p!= 0 ) { clearsub(p->left); clearsub(p->right); delete p; } CIS210 107

Expression Tree ADT void ExprTree:: build () // Reads a prefix expression (consisting of single-digit, nonnegative // integers and arithmetic operators) from the keyboard and // builds the corresponding expression tree. { buildsub(root); } CIS210 108

Expression Tree ADT void ExprTree:: buildsub ( ExprTreeNode *&p ) // Recursive partner of the build() function. Builds a subtree and // sets p to point to its root. { char ch; // Input operator or number cin >> ch; p = new ExprTreeNode(ch,0,0); // Link in node if (!isdigit(ch) ) // Operator -- construct subtrees } { } buildsub(p->left); buildsub(p->right); CIS210 109

Expression Tree ADT void ExprTree:: expression () const // Outputs the corresponding arithmetic expression in fully // parenthesized infix form. { exprsub(root); } CIS210 110

Expression Tree ADT void ExprTree:: exprsub ( ExprTreeNode *p ) const // Recursive partner of the expression() function. // Outputs the subtree pointed to by p. { if ( p!= 0 ) { if (!isdigit(p->dataitem) ) cout << '('; exprsub(p->left); cout << p->dataitem; exprsub(p->right); if (!isdigit(p->dataitem) ) cout << ')'; } } CIS210 111

Expression Tree ADT float ExprTree:: evaluate () // Returns the value of the corresponding arithmetic expression. { // Requires that the tree is not empty return evaluatesub(root); } CIS210 112

Expression Tree ADT float ExprTree:: evaluatesub ( ExprTreeNode *p ) const // Recursive partner of the evaluate() function. Returns the value of // subtree pointed to by p. { float l, r, // Intermediate results result; // Result returned if ( isdigit(p->dataitem) ) result = p->dataitem - '0'; // Convert from char to number else { l = evaluatesub(p->left); // Evaluate subtrees r = evaluatesub(p->right); switch ( p->dataitem ) // Combine results { case '+' : result = l + r; break; case '-' : result = l - r; break; case '*' : result = l * r; break; case '/' : result = l / r; } } return result; CIS210 113 }