Design Patterns for Data Structures. Chapter 8. The Composite State Binary Tree BiTreeCS

Size: px
Start display at page:

Download "Design Patterns for Data Structures. Chapter 8. The Composite State Binary Tree BiTreeCS"

Transcription

1 Design Patterns for Data Structures Chapter 8 The Composite State Binary Tree BiTreeCS

2 Design Patterns for Data Structures Chapter 8 The Composite State Binary Tree BiTreeCS In a nonempty node _left is a binary tree _right is a binary tree

3 _root: AcsNode<T> * BiTreeCS T AcsNode T BiTreeCS(rhs: BiTreeCS<T> const &) BiTreeCS(node: AcsNode<T> *) + BiTreeCS( ) + ~BiTreeCS( ) + clear( ) + contains(data: T const &) const: bool copyroot(rhs: BiTreeCS<T> const &): AcsNode<T> * + equals(rhs: BiTreeCS<T> const &) const: bool equalshelper(data: T const &, left: BiTreeCS<T> const &, right: BiTreeCS<T> const &) const: bool + height( ) const: int + inorder(os: ostream &) const + insertroot(data: T const &) + isempty( ) const: bool + left( ): BiTreeCS<T> & + left( ) const: BiTreeCS<T> & + max( ) const: T const & + numleaves( ) const: int + numnodes( ) const: int + operator=(rhs: BiTreeCS<T> const &): BiTreeCS & + postorder(os: ostream &) const + preorder(os: ostream &) const + remleaves( ) + remroot( ): T + right( ): BiTreeCS<T> & + right( ) const: BiTreeCS<T> const & + root( ): T & + root( ) const: T const & + settree(tree: BiTreeCS<T> &) + tostream(os: ostream &) const tostreamhelper(prright: string, prroot: string prleft: string, os: ostream &) const # ~AcsNode( ) # clear(owner: BiTreeCS<T> &) # contains(data: T const &) const: bool copyroot( ): AcsNode<T> * # equals(rhs: BiTreeCS<T> const &) const: bool # equalshelper(data: T const &, left: BiTreeCS<T> const &, right: BiTreeCS<T> const &) const: bool # height( ) const: int # inorder(os: ostream &) const # insertroot(owner: BiTreeCS<T> &, data: T const &) # isempty( ) const: bool # left( ): BiTreeCS<T> & # max( ) const: T const & # numleaves( ) const: int # numnodes( ) const: int # postorder(os: ostream &) const # preorder(os: ostream &) const # remleaves(owner: BiTreeCS<T> &) # remroot(owner: BiTreeCS<T> &): T # right( ): BiTreeCS<T> & # right( ) const: BiTreeCS<T> const & # root( ): T & # root( ) const: T const & # settree(owner: BiTreeCS<T>, tree: BiTreeCS<T> &) # tostream(os: ostream &) const # tostreamhelper(prright: string, prrootpr: string Left: string, os: ostream &) const Figure 8.24 NEcsNode T MTcsNode T _left: BiTreeCS<T> _data: T _right: BiTreeCS<T> MTcsNode( ) NEcsNode(data: T const &) NEcsNode(leftNode: AcsNode<T> *, data: T, rightnode: AcsNode<T> *) NEcsNode(rhs: const NEcsNode<T> *)

4 Design Patterns for Data Structures BiTreeCS class BiTreeCS { private: AcsNode<T> *_root; class NEcsNode : public AcsNode<T> { private: BiTreeCS<T> _left; T _data; BiTreeCS<T> _right;

5 Design Patterns for Data Structures Figure 8.25 mytree mytree._root->_left mytree._root->_data mytree._root->_right mytree._root->_right._root *mytree._root mytree._root (a) A detailed rendering of a binary tree and a node. (b) An abbreviated rendering.

6 Design Patterns for Data Structures BiTreeCS Methods for output and characterization

7 Design Patterns for Data Structures Figure 8.26 // ========= preorder ========= void BiTreeCS<T>::preOrder(ostream &os) const { _root->preorder(os); void MTcsNode<T>::preOrder(ostream &os) const { void NEcsNode<T>::preOrder(ostream &os) const { os << _data << ; _left.preorder(os); _right.preorder(os);

8 Design Patterns for Data Structures Figure 8.27 // ========= isempty ========= bool BiTreeCS<T>::isEmpty() const { return _root->isempty(); bool MTcsNode<T>::isEmpty() const { return true; bool NEcsNode<T>::isEmpty() const { return false; _root _left _data _right 40 (a) The environment of a binary tree. (b) The environment of an empty node. (c) The environment of a nonempty node.

9 Design Patterns for Data Structures Figure 8.27 // ========= isempty ========= bool BiTreeCS<T>::isEmpty() const { return _root->isempty(); bool MTcsNode<T>::isEmpty() const { return true; bool NEcsNode<T>::isEmpty() const { return false; _root _left _data _right 40 (a) The environment of a binary tree. (b) The environment of an empty node. (c) The environment of a nonempty node.

10 Design Patterns for Data Structures Figure 8.28 // ========= left ========= BiTreeCS<T> &BiTreeCS<T>::left() { return _root->left(); BiTreeCS<T> &MTcsNode<T>::left() { cerr << left precondition violated: << An empty tree has has no left subtree. << endl; throw -1; BiTreeCS<T> &NEcsNode<T>::left() { return _left;

11 Design Patterns for Data Structures Figure 8.29 // ========= numnodes ========= int BiTreeCS<T>::numNodes() const { return _root->numnodes(); int MTcsNode<T>::numNodes() const { return 0; _left.numnodes() 1 50 _right.numnodes() int NEcsNode<T>::numNodes() const { return 1 + _left.numnodes() + _right.numnodes();

12 Design Patterns for Data Structures Figure 8.30 // ========= max ========= T const &BiTreeCS<T>::max() const { return _root->max(); T const &MTcsNode<T>::max() const { cerr << max precondition violated: An empty tree has no maximum. << endl; throw -1; T const &NEcsNode<T>::max() const { T const *datatemp = &_data; // To avoid restrictions on T const &_data. T const *leftmax = (_left.isempty())? datatemp : &_left.max(); T const *rightmax = (_right.isempty())? datatemp : &_right.max(); return (*leftmax > *rightmax)? ((*leftmax > *datatemp)? *leftmax : *datatemp) : ((*rightmax > *datatemp)? *rightmax : *datatemp);

13 Design Patterns for Data Structures Figure 8.31 // ========= equals ========= // Post: true is returned if this tree equals tree rhs; // otherwise, false is returned. // Two trees are equal if they contain the same number // of equal elements with the same shape. bool BiTreeCS<T>::equals(const BiTreeCS<T> &rhs) const { cerr << BiTreeCS<T>::equals: Exercise for the student. << endl; throw -1; bool MTcsNode<T>::equals(const BiTreeCS<T> &rhs) const { cerr << MTcsNode<T>::equals: Exercise for the student. << endl; throw -1; bool NEcsNode<T>::equals(const BiTreeCS<T> &rhs) const { cerr << NEcsNode<T>::equals: Exercise for the student. << endl; throw -1;

14 Design Patterns for Data Structures Figure 8.31 bool BiTreeCS<T>::equals(const BiTreeCS<T> &rhs) const { cerr << BiTreeCS<T>::equals: Exercise for the student. << endl; throw -1; _root rhs (a) The environment of a binary tree.

15 Design Patterns for Data Structures Figure 8.31 bool MTcsNode<T>::equals(const BiTreeCS<T> &rhs) const { cerr << MTcsNode<T>::equals: Exercise for the student. << endl; throw -1; rhs (b) The environment of an empty node.

16 Design Patterns for Data Structures Figure 8.31 bool NEcsNode<T>::equals(const BiTreeCS<T> &rhs) const { cerr << NEcsNode<T>::equals: Exercise for the student. << endl; throw -1; _left _data _right rhs (c) The environment of a nonempty node.

17 Design Patterns for Data Structures Figure 8.32 // equalshelper // Post: true is returned if root equals this->root(), // left equals this->left(), and right equals this->right(); // otherwise, false is returned. bool BiTreeCS<T>::equalsHelper(T const &data, const BiTreeCS<T> &left, const BiTreeCS<T> &right) const { cerr << BiTreeCS<T>::equalsHelper: Exercise for the student. << endl; throw -1; bool MTcsNode<T>::equalsHelper(T const &, const BiTreeCS<T> &, const BiTreeCS<T> &) const { cerr << MTcsNode<T>::equalsHelper: Exercise for the student. << endl; throw -1; bool NEcsNode<T>::equalsHelper(T const &data, const BiTreeCS<T> &left, const BiTreeCS<T> &right) const { cerr << NEcsNode<T>::equalsHelper: Exercise for the student. << endl; throw -1;

18 Design Patterns for Data Structures Figure 8.32 bool BiTreeCS<T>::equalsHelper(T const &data, const BiTreeCS<T> &left, const BiTreeCS<T> &right) const { cerr << BiTreeCS<T>::equalsHelper: Exercise for the student. << endl; throw -1; _root data left right (a) The environment of a binary tree.

19 Design Patterns for Data Structures Figure 8.32 bool MTcsNode<T>::equalsHelper(T const &, const BiTreeCS<T> &, const BiTreeCS<T> &) const { cerr << MTcsNode<T>::equalsHelper: Exercise for the student. << endl; throw -1; (b) The environment of an empty node.

20 Design Patterns for Data Structures Figure 8.32 bool NEcsNode<T>::equalsHelper(T const &data, const BiTreeCS<T> &left, const BiTreeCS<T> &right) const { cerr << NEcsNode<T>::equalsHelper: Exercise for the student. << endl; throw -1; _left _data _right data left right (c) The environment of a nonempty node.

21 Design Patterns for Data Structures BiTreeCS Methods for construction and insertion

22 Design Patterns for Data Structures Figure 8.33 Two constructors for BiTreeCS // ========= Constructors ========= BiTreeCS<T>::BiTreeCS(): _root(new MTcsNode<T>()) { // Post: _root points to node with no allocation. BiTreeCS<T>::BiTreeCS(AcsNode<T> *node): _root(node) { Empty node The empty tree created by BiTreeCS().

23 Design Patterns for Data Structures Figure 8.34 Three constructors for NEcsNode // Post: _data is data. NEcsNode<T>::NEcsNode(T const &data): _data(data) {

24 Design Patterns for Data Structures Figure 8.34 Three constructors for NEcsNode // Post: _left._root and _right._root point to leftnode // and rightnode, and _data is data. // _left and _right own their nodes and are responsible // for garbage collection. NEcsNode<T>::NEcsNode(AcsNode<T> *leftnode, T data, AcsNode<T> *rightnode): _data(data) { _left._root = leftnode; _right._root = rightnode;

25 Design Patterns for Data Structures Figure 8.34 Three constructors for NEcsNode // Post: _left is rhs->_left, _data is rhs->_data, // and _right is rhs->_right. NEcsNode<T>::NEcsNode(const NEcsNode *rhs): _left(rhs->_left), _data(rhs->_data), _right(rhs->_right) {

26 Design Patterns for Data Structures Figure 8.35 // ========= insertroot ========= void BiTreeCS<T>::insertRoot(T const &data) { _root->insertroot(*this, data); void MTcsNode<T>::insertRoot(BiTreeCS<T> &owner, T const &data) { owner._root = new NEcsNode<T>(data); delete this; void NEcsNode<T>::insertRoot(BiTreeCS<T> &owner, T const &data) { cerr << insertroot precondition violated: << Cannot insert root into a nonempty tree << endl; throw -1; mytree

27 Design Patterns for Data Structures Figure 8.35 mytree mytree 80 Empty node Empty node Empty node (a) Initial tree. (b) mytree.insertroot(80);

28 Design Patterns for Data Structures Figure 8.36 // ========= settree ========= void BiTreeCS<T>::setTree(BiTreeCS<T> &tree) { _root->settree(*this, tree); void MTcsNode<T>::setTree(BiTreeCS<T> &owner, BiTreeCS<T> &tree) { delete owner._root; owner._root = tree._root; tree._root = new MTcsNode<T>(); void NEcsNode<T>::setTree(BiTreeCS<T> &owner, BiTreeCS<T> &tree) { delete owner._root; owner._root = tree._root; tree._root = new MTcsNode<T>();

29 Design Patterns for Data Structures Figure 8.36 mytree yourtree 20 mytree 20 yourtree (a) Initial trees. (b) mytree.settree(yourtree);

30 Design Patterns for Data Structures Figure 8.37 // ========= operator= ========= BiTreeCS<T> &BiTreeCS<T>::operator=(const BiTreeCS<T> &rhs) { if (this!= &rhs) { // In case someone writes mytree = mytree; delete _root; _root = copyroot(rhs); return *this;

31 Design Patterns for Data Structures Figure 8.37 // ========= copyroot ========= AcsNode<T> *BiTreeCS<T>::copyRoot(const BiTreeCS<T> &rhs) { return rhs._root->copyroot(); AcsNode<T> *MTcsNode<T>::copyRoot() { return new MTcsNode<T>(); AcsNode<T> *NEcsNode<T>::copyRoot() { return new NEcsNode<T>( _left._root->copyroot(), _data, _right._root->copyroot());

32 Design Patterns for Data Structures Figure 8.37 mytree 60 yourtree mytree yourtree (a) Initial trees. (b) mytree = yourtree;

33 Design Patterns for Data Structures BiTreeCS Methods for destruction and removal

34 Design Patterns for Data Structures Figure 8.38 // ========= Destructors ========= // Recursively deletes this tree. BiTreeCS<T>::~BiTreeCS() { delete _root; virtual ~AcsNode() { // Virtual destructor necessary for subclassing.

35 Design Patterns for Data Structures Figure 8.39 // ========= clear ========= void BiTreeCS<T>::clear() { cerr << BiTreeCS<T>::clear: Exercise for the student. << endl; throw -1; void MTcsNode<T>::clear(BiTreeCS<T> &) { cerr << MTcsNode<T>::clear: Exercise for the student. << endl; throw -1; void NEcsNode<T>::clear(BiTreeCS<T> &owner) { cerr << NEcsNode<T>::clear: Exercise for the student. << endl; throw -1;

36 Design Patterns for Data Structures Figure 8.39 The clear() operation for BiTreeCS owner owner owner (a) Original. (b) Clear left. (c) Clear right. owner owner Empty node Empty node (d) Change owner. (e) Delete node.

37 Design Patterns for Data Structures Figure 8.40 The remroot() operation for BiTreeCS owner owner owner owner Empty node Empty node Empty node (a) Original. (b) Link owner. (c) Set to nil. (d) Delete node.

The Composite State Visitor Pattern

The Composite State Visitor Pattern Design Patterns for Data Structures Chapter 8 he Composite State Visitor Pattern Design Patterns for Data Structures Chapter 8 he Composite State Binary ree with the Visitor Pattern BireeCSV Design Patterns

More information

An empty list is a list. A nonempty list has two parts: a data value of some type T, and the rest of the list, which is a list.

An empty list is a list. A nonempty list has two parts: a data value of some type T, and the rest of the list, which is a list. Chapter 6 The composite pattern for a list The definition of a list: An empty list is a list. A nonempty list has two parts: a data value of some type T, and the rest of the list, which is a list. Chapter

More information

Design Patterns for Data Structures. Chapter 10. Balanced Trees

Design Patterns for Data Structures. Chapter 10. Balanced Trees Capter 10 Balanced Trees Capter 10 Four eigt-balanced trees: Red-Black binary tree Faster tan AVL for insertion and removal Adelsen-Velskii Landis (AVL) binary tree Faster tan red-black for lookup B-tree

More information

Design Patterns for Data Structures. Chapter 10. Balanced Trees

Design Patterns for Data Structures. Chapter 10. Balanced Trees Capter 10 Balanced Trees Capter 10 Four eigt-balanced trees: Red-Black binary tree Faster tan AVL for insertion and removal Adelsen-Velskii Landis (AVL) binary tree Faster tan red-black for lookup B-tree

More information

template<class T> class LNode { private: T _data; LNode *_next; template<class T> class ListL { private: LNode<T> *_head;

template<class T> class LNode { private: T _data; LNode *_next; template<class T> class ListL { private: LNode<T> *_head; Chapter 6 Mutable Lists Chapter 5 describes immutable lists, that is, lists that cannot change. If you want a new list that is some alteration of a given list you must make a new list from the old list

More information

The Composite State Visitor Pattern

The Composite State Visitor Pattern Chapter 6 The Composite State Visitor Pattern Chapter 6 The visitor pattern Represent an operation to be performed on elements of an object structure. Visitor lets you define a new operation without changing

More information

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

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad SCJ2013 Data Structure & Algorithms Binary Search Tree Nor Bahiah Hj Ahmad Binary Search Tree A binary search tree has the following properties: For every node n in the tree Value of n is greater than

More information

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

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals Binary Trees 1 Binary Tree Node Relationships 2 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the

More information

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

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

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

selectors, methodsinsert() andto_string() the depth of a tree and a membership function Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using a tree of integer numbers 2 Header Files defining a node struct defining a tree class 3 Definition of Methods selectors, methodsinsert()

More information

Binary Tree Implementation

Binary Tree Implementation Binary Tree Implementation Lecture 31 Sections 12.2-12.3 Robb T. Koether Hampden-Sydney College Mon, Apr 5, 2010 Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 5, 2010 1 /

More information

Chapter 12: Searching: Binary Trees and Hash Tables. Exercises 12.1

Chapter 12: Searching: Binary Trees and Hash Tables. Exercises 12.1 Chapter 12: Searching: Binary Trees and Hash Tables Exercises 12.1 1. 4, 6 2. 4, 1, 2, 3 3. 4, 6, 5 4. 4, 1, 0 5. 4, 6, 7, 8 6. template linearsearch(elementtype x[], ElementType

More information

Example Final Questions Instructions

Example Final Questions Instructions Example Final Questions Instructions This exam paper contains a set of sample final exam questions. It is for practice purposes only. You ll most likely need longer than three hours to answer all the questions.

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms First Semester 2017/2018 Linked Lists Eng. Anis Nazer Linked List ADT Is a list of nodes Each node has: data (can be any thing, int, char, Person, Point, day,...) link to

More information

CMSC 341 Lecture 10 Binary Search Trees

CMSC 341 Lecture 10 Binary Search Trees CMSC 341 Lecture 10 Binary Search Trees John Park Based on slides from previous iterations of this course Review: Tree Traversals 2 Traversal Preorder, Inorder, Postorder H X M A K B E N Y L G W UMBC CMSC

More information

Binary Tree Implementation

Binary Tree Implementation Binary Tree Implementation Lecture 32 Section 19.1 Robb T. Koether Hampden-Sydney College Mon, Apr 16, 2018 Robb T. Koether (Hampden-Sydney College) Binary Tree Implementation Mon, Apr 16, 2018 1 / 24

More information

Topic 7: Algebraic Data Types

Topic 7: Algebraic Data Types Topic 7: Algebraic Data Types 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11,

More information

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 8 EECE.3220 Data Structures Fall 2017

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 8 EECE.3220 Data Structures Fall 2017 UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering Program 8 EECE.3220 Data Structures Fall 2017 Binary Search Trees and Class Templates Word Counter Application The object

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2305/lab06.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 2 Extend the IntegerSet class from Lab 04 to provide the following

More information

Design Patterns for Data Structures. Chapter 9.5. Hash Tables

Design Patterns for Data Structures. Chapter 9.5. Hash Tables Hash Tables Binary information representation 0 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111 (a) Hexadecimal abbreviation for binary values.

More information

Lecture 37 Section 9.4. Wed, Apr 22, 2009

Lecture 37 Section 9.4. Wed, Apr 22, 2009 Preorder Inorder Postorder Lecture 37 Section 9.4 Hampden-Sydney College Wed, Apr 22, 2009 Outline Preorder Inorder Postorder 1 2 3 Preorder Inorder Postorder 4 Preorder Inorder Postorder Definition (Traverse)

More information

CMSC 341. Binary Search Trees CMSC 341 BST

CMSC 341. Binary Search Trees CMSC 341 BST CMSC 341 Binary Search Trees CMSC 341 BST Announcements Homework #3 dues Thursday (10/5/2017) Exam #1 next Thursday (10/12/2017) CMSC 341 BST A Generic Tree CMSC 341 BST Binary Tree CMSC 341 BST The Binary

More information

void insert( Type const & ) void push_front( Type const & )

void insert( Type const & ) void push_front( Type const & ) 6.1 Binary Search Trees A binary search tree is a data structure that can be used for storing sorted data. We will begin by discussing an Abstract Sorted List or Sorted List ADT and then proceed to describe

More information

Lab 2: ADT Design & Implementation

Lab 2: ADT Design & Implementation Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted

More information

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University C++ Review CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 Purpose of Review Review some basic C++ Familiarize us with

More information

Design Patterns for Data Structures. Chapter Nguyen-Wong B-Trees

Design Patterns for Data Structures. Chapter Nguyen-Wong B-Trees Chapter 10.3 Nguyen-Wong B-Trees Chapter 10.3 A B-tree is an n-way tree with the following characteristics It is balanced. It is ordered. It has a node size restriction. Chapter 10.3 A balanced n-way tree

More information

Example Final Questions Instructions

Example Final Questions Instructions Example Final Questions Instructions This exam paper contains a set of sample final exam questions. It is for practice purposes only. You ll most likely need longer than three hours to answer all the questions.

More information

The number of nodes, N, in a binary tree of height, h is: 2 N 2 The height of a binary tree is: h =

The number of nodes, N, in a binary tree of height, h is: 2 N 2 The height of a binary tree is: h = 1. Structure Property A heap is a binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. B Figure 1. A complete binary tree. Figure

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

Design Patterns for Data Structures. Chapter 9. Dictionaries

Design Patterns for Data Structures. Chapter 9. Dictionaries Design Patterns for Data Structures Chapter 9 Dictionaries Design Patterns for Data Structures Chapter 9 The key-value pair A dictionary provides storage based on the key retrieval based on the key access

More information

Design Patterns for Data Structures. Chapter 9. Dictionaries

Design Patterns for Data Structures. Chapter 9. Dictionaries Design Patterns for Data Structures Chapter 9 Dictionaries Design Patterns for Data Structures Chapter 9 The key-value pair A dictionary provides storage based on the key retrieval based on the key access

More information

Binary Search Trees Part Two

Binary Search Trees Part Two Binary Search Trees Part Two Recap from Last Time Binary Search Trees A binary search tree (or BST) is a data structure often used to implement maps and sets. The tree consists of a number of nodes, each

More information

ECE 244 Programming Fundamentals Fall Lab Assignment #5: Binary Search Trees

ECE 244 Programming Fundamentals Fall Lab Assignment #5: Binary Search Trees ECE 244 Programming Fundamentals Fall 2012 1. Objectives Lab Assignment #5: Binary Search Trees The objectives of this assignment are to provide you with more practice on the use of the various C++ concepts/constructs

More information

Chapter 7: Stacks. Exercises 7.2

Chapter 7: Stacks. Exercises 7.2 hapter 7: Stacks Exercises 7.2 1. mytop == 0; myrray contains 3 elements: 10, 22, 37,?,? but note that only element 10 is considered to be in the stack. 2. mytop == 1; myrray contains 3 elements: 10, 9,

More information

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 10 EECE.3220 Data Structures Fall 2017.

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 10 EECE.3220 Data Structures Fall 2017. UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering Program 10 EECE.3220 Data Structures Fall 2017 Hash Tables Program 10 is a repeat of Program 9, but in Program 10 the

More information

EECE.3220: Data Structures Spring 2017

EECE.3220: Data Structures Spring 2017 EECE.3220: Data Structures Spring 2017 Lecture 14: Key Questions February 24, 2017 1. Describe the characteristics of an ADT to store a list. 2. What data members would be necessary for a static array-based

More information

What property of a C# array indicates its allocated size? What keyword in the base class allows a method to be polymorphic?

What property of a C# array indicates its allocated size? What keyword in the base class allows a method to be polymorphic? What property of a C# array indicates its allocated size? a. Size b. Count c. Length What property of a C# array indicates its allocated size? a. Size b. Count c. Length What keyword in the base class

More information

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

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS DATA STRUCTURES AND ALGORITHMS 13 BINARY TREES INORDER, PREORDER, POSTORDER TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD WWW.IMRANIHSAN.COM LECTURES ADAPTED FROM: DANIEL KANE,

More information

This examination has 11 pages. Check that you have a complete paper.

This examination has 11 pages. Check that you have a complete paper. MARKING KEY The University of British Columbia MARKING KEY Computer Science 252 2nd Midterm Exam 6:30 PM, Monday, November 8, 2004 Instructors: K. Booth & N. Hutchinson Time: 90 minutes Total marks: 90

More information

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Review: C++ Basic Concepts. Dr. Yingwu Zhu Review: C++ Basic Concepts Dr. Yingwu Zhu Outline C++ class declaration Constructor Overloading functions Overloading operators Destructor Redundant declaration A Real-World Example Question #1: How to

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Implementation Kostas Alexis s Definition: A (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction

More information

Priority Queues (Heaps)

Priority Queues (Heaps) Priority Queues (Heaps) October 11, 2016 CMPE 250 Priority Queues October 11, 2016 1 / 29 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full

More information

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT... Steven J. Zeil July 11, 2013 Contents 1 Definition: Binary Search Trees 2 1.1 The Binary Search Tree ADT.................................................... 3 2 Implementing Binary Search Trees 7 2.1 Searching

More information

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

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example. Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except

More information

Name CPTR246 Spring '17 (100 total points) Exam 3

Name CPTR246 Spring '17 (100 total points) Exam 3 Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -

More information

Expression Trees and the Heap

Expression Trees and the Heap Expression Trees and the Heap 1 Binary Expression Trees evaluating expressions splitting strings in operators and operands 2 C++ Binary Tree of Strings header files defining the methods 3 the Heap or Priority

More information

CS 216 Exam 1 Fall SOLUTION

CS 216 Exam 1 Fall SOLUTION CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT

More information

the pointer range [first, last) into the tree

the pointer range [first, last) into the tree 1 #ifndef BINARY_SEARCH_TREE_CLASS 2 #define BINARY_SEARCH_TREE_CLASS 3 4 #ifndef NULL 5 #include 6 #endif // NULL 7 8 #include // for setw() 9 #include // for format conversion

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/04/lab04.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 3 In this assignment create an IntegerSet class that will provide

More information

LECTURE 13 BINARY TREES

LECTURE 13 BINARY TREES DATA STRUCTURES AND ALGORITHMS LECTURE 13 BINARY TREES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD DEFINITION The arbitrary number of children in general trees is often unnecessary many real-life

More information

Priority Queues and Huffman Trees

Priority Queues and Huffman Trees Priority Queues and Huffman Trees 1 the Heap storing the heap with a vector deleting from the heap 2 Binary Search Trees sorting integer numbers deleting from a binary search tree 3 Huffman Trees encoding

More information

[CS302-Data Structures] Homework 2: Stacks

[CS302-Data Structures] Homework 2: Stacks [CS302-Data Structures] Homework 2: Stacks Instructor: Kostas Alexis Teaching Assistants: Shehryar Khattak, Mustafa Solmaz, Bishal Sainju Fall 2018 Semester Section 1. Stack ADT Overview wrt Provided Code

More information

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements B-Trees 1 B-Trees nodes with many children a type node a class for B-trees 2 manipulating a B-tree an elaborate example the insertion algorithm removing elements MCS 360 Lecture 35 Introduction to Data

More information

Binary Trees. Chapter 22

Binary Trees. Chapter 22 B B Chapter 22 Binary Trees Like stacks and lists, binary trees are structures that store values. Stacks and lists are linear. That is, you can visualize them as consisting of a sequential row of values,

More information

CpSc212 Goddard Notes Chapter 10. Linked Lists

CpSc212 Goddard Notes Chapter 10. Linked Lists CpSc212 Goddard Notes Chapter 10 Linked Lists 10.1 Links and Pointers The linked list is not an ADT in its own right; rather it is a way of implementing many data structures. It is designed to replace

More information

"apple" "grape" "grape" "grape" "apple"

apple grape grape grape apple Test 1: CPS 100 Owen Astrachan and Dee Ramm February 21, 1997 Name: Honor code acknowledgment (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 TOTAL: value 10 pts. 9 pts. 21 pts.

More information

CSC 210, Exam Two Section February 1999

CSC 210, Exam Two Section February 1999 Problem Possible Score 1 12 2 16 3 18 4 14 5 20 6 20 Total 100 CSC 210, Exam Two Section 004 7 February 1999 Name Unity/Eos ID (a) The exam contains 5 pages and 6 problems. Make sure your exam is complete.

More information

Programming Assignment #4 Binary Trees in C++

Programming Assignment #4 Binary Trees in C++ Programming Assignment #4 Binary Trees in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie,

More information

END TERM EXAMINATION

END TERM EXAMINATION END TERM EXAMINATION THIRD SEMESTER [BCA] DECEMBER 2007 Paper Code: BCA 209 Subject: Object Oriented Programming Time: 3 hours Maximum Marks: 75 Note: Attempt all questions. Internal choice is indicated.

More information

Assignment of Objects

Assignment of Objects Copying Objects 1 Assignment of Objects 2 Slides 1. Table of Contents 2. Assignment of Objects 3. Dynamic Content 4. Shallow Copying 5. Deep Copying 6. this Pointer 7. Improved Deep Copy 8. Passing an

More information

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions: VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted

More information

Trees, Binary Trees, and Binary Search Trees

Trees, Binary Trees, and Binary Search Trees COMP171 Trees, Binary Trees, and Binary Search Trees 2 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

More information

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std; More Group HW The following code is contained in the file ex1stck.h. Fill in the blanks with the C++ statement(s) that will correctly finish the method. Each blank may be filled in with more than one statement.

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/11/lab11.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 1 The purpose of this assignment is to become more familiar with

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure

More information

2003 AP COMPUTER SCIENCE AB FREE-RESPONSE QUESTIONS

2003 AP COMPUTER SCIENCE AB FREE-RESPONSE QUESTIONS COMPUTER SCIENCE AB SECTION II Time 1 hour and 45 minutes Number of questions 4 Percent of total grade 50 Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN C++. Note:

More information

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

for (int i = 1; i <= 3; i++) { do { cout << Enter a positive integer: ; cin >> n; // Workshop 1 #include using namespace std; int main () int n, k; int sumdigits; for (int i = 1; i n; cin.clear (); cin.ignore (100,

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/06/lab06.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 2 Extend the IntegerSet class from Lab 04 to provide the following

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Queues and Priority Queues Kostas Alexis Implementations of the ADT Queue Like stacks, queues can have Array-based or Link-based implementation Can also use implementation

More information

1) Holiday Lights 30 Points

1) Holiday Lights 30 Points 1) Holiday Lights 30 Points It's the holiday season, which means one thing; holiday lights! You have unboxed your string of lights to hang, but have one problem; though they are multicolored, they are

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 10 / 10 / 2016 Instructor: Michael Eckmann Today s Topics Questions? Comments? A few comments about Doubly Linked Lists w/ dummy head/tail Trees Binary trees

More information

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Tree Implementations Kostas Alexis Nodes in a Binary Tree Representing tree nodes Must contain both data and pointers to node s children Each node will be an object

More information

Topics. bool and string types input/output library functions comments memory allocation templates classes

Topics. bool and string types input/output library functions comments memory allocation templates classes C++ Primer C++ is a major extension of c. It is similar to Java. The lectures in this course use pseudo-code (not C++). The textbook contains C++. The labs involve C++ programming. This lecture covers

More information

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

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence: Multiple choice questions Answer on Scantron Form 4 points each (100 points) 1. Which is NOT a reasonable conclusion to this sentence: Multiple constructors for a class... A. are distinguished by the number

More information

The State Design Pattern

The State Design Pattern B B Chapter 24 The State Design Pattern The state design pattern is an object-oriented technique that uses inheritance and class composition. It is applicable to a variety of software design problems where

More information

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT Containers: Stack The Stack ADT A stack is a list of objects in which insertions and deletions can only be performed at the top of the list. Also known as LIFO Last In, First Out) push insert an element

More information

Object oriented programming

Object oriented programming Exercises 7 Version 1.0, 11 April, 2017 Table of Contents 1. Inheritance.................................................................. 1 1.1. Tennis Player...........................................................

More information

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science Containers: Stack Jordi Cortadella and Jordi Petit Department of Computer Science The Stack ADT A stack is a list of objects in which insertions and deletions can only be performed at the top of the list.

More information

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy CS 61B, Spring 1996 Midterm #1 Professor M. Clancy Problem 0 (1 point, 1 minute) Put your login name on each page. Also make sure you have provided the information requested on the first page. Problem

More information

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211 ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B 2011-02-15/13:30-14:50 MC-4021/RCH-211 Instructions: There are 63 marks. It will be marked out of 55. No aides. Turn off all electronic media

More information

Intermediate Programming & Design (C++) Classes in C++

Intermediate Programming & Design (C++) Classes in C++ Classes in C++ A class is a data type similar to a C structure. It includes various local data (called data members) together with constructors, destructors and member functions. All of them are called

More information

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different. Chapter 6 Trees In a hash table, the items are not stored in any particular order in the table. This is fine for implementing Sets and Maps, since for those abstract data types, the only thing that matters

More information

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1 CSE 143 Linked Lists [Chapter 4; Chapter 6, pp. 265-271] Linked Lists A linked list is a collection of dynamically allocated nodes Each node contains at least one member (field) that points to another

More information

Object-Oriented Programming. Lecture 4 Dr Piotr Cybula

Object-Oriented Programming. Lecture 4 Dr Piotr Cybula Object-Oriented Programming Lecture 4 Dr Piotr Cybula Inheritance enrichment of existing classes with additional functionality ability of modifying the action of methods without

More information

Topic 14. The BinaryTree ADT

Topic 14. The BinaryTree ADT Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree

More information

CE 221 Data Structures and Algorithms

CE 221 Data Structures and Algorithms CE 2 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, 6.1 6.3 Izmir University of Economics 1 A kind of queue Priority Queue (Heap) Dequeue gets element with the

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will

More information

An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include:

An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include: 4.2 Abstract Trees Having introduced the tree data structure, we will step back and consider an Abstract Tree that stores a hierarchical ordering. 4.2.1 Description An abstract tree stores data that is

More information

Topic Binary Trees (Non-Linear Data Structures)

Topic Binary Trees (Non-Linear Data Structures) 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

More information

Data Structures (INE2011)

Data Structures (INE2011) Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam Lecture 4 1 Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting

More information

Priority Queues (Heaps)

Priority Queues (Heaps) Priority Queues (Heaps) 1 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the

More information

Design Patterns for Data Structures. Chapter 1. Abstraction

Design Patterns for Data Structures. Chapter 1. Abstraction Chapter 1 Abstraction Figure 1.1 Type abstraction for type double. 2.0 0.8-43.7 12.8 4.0 5.2-13.9 double Figure 1.2 Structure abstraction )$*+#$,!"#$%&'(",-,,,,./+0(",("&'$12,,,,./+0(",34.$12 52 67!"#$%&'("8("&'$1,9,:8;2

More information

Data Structures And Algorithms

Data Structures And Algorithms Data Structures And Algorithms Stacks Eng. Anis Nazer First Semester 2017-2018 Stack An Abstract data type (ADT) Accessed only on one end Similar to a stack of dishes you can add/remove on top of stack

More information