ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,

Slides:



Advertisements
Similar presentations
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Advertisements

ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Laplace Transform Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2008 by Douglas Wilhelm Harder.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Topics in Applied Mathematics Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Fixed-Point Iteration Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007 by Douglas Wilhelm.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Proof by Induction.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Binary Numbers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007 by Douglas Wilhelm Harder.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Decimal Numbers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007 by Douglas Wilhelm Harder.
Double-Precision Floating-Point Numbers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 The tree data structure Outline In this topic, we will cover: –Definition of a tree data structure and its components –Concepts of: Root, internal, and.
1 Data Structures and Algorithms Outline This topic will describe: –The concrete data structures that can be used to store information –The basic forms.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Binary search trees Outline This topic covers binary search trees: –Abstract Sorted Lists –Background –Definition and examples –Implementation: Front,
1 N -ary trees This topic quickly looks at a generalization of a binary tree, where each node has up to N children –Definition –Perfect N -ary trees –Complete.
1 Binary trees Outline In this talk, we will look at the binary tree data structure: –Definition –Properties –A few applications Ropes (strings) Expression.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Complete binary trees Outline Introducing complete binary trees –Background –Definitions –Examples –Logarithmic height –Array storage.
The mechanics of recursion
Outline In this topic we will look at:
Open Addressing: Quadratic Probing
AVL Trees.
Outline Introducing perfect binary trees Definitions and examples
Multiway Search Trees.
Presentation transcript:

ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca © by Douglas Wilhelm Harder. Some rights reserved. Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca © by Douglas Wilhelm Harder. Some rights reserved. Abstract Trees

2 Abstract trees Outline This topic discusses the concept of an abstract tree: –Hierarchical ordering –Description of an Abstract Tree –Applications –Implementation –Local definitions

3 Abstract trees Outline A hierarchical ordering of a finite number of objects may be stored in a tree data structure Operations on a hierarchically stored container include: –Accessing the root: –Given an object in the container: Access the parent of the current object Find the degree of the current object Get a reference to a child, Attach a new sub-tree to the current object Detach this tree from its parent 4.2.1

4 Abstract trees General Trees An abstract tree does not restrict the number of nodes –In this tree, the degrees vary: DegreeNodes 0D, F, G, J, K, L, M 1C 2B, E, H 3I 4.2.1

5 Abstract trees General Trees: Design We can implement a general tree by using a class which: –Stores an element –Stores the children in a linked-list 4.2.2

6 Abstract trees Implementation The class definition would be: template class Simple_tree { private: Type element; Simple_tree *parent_node; ece250::Single_list children; public: Simple_tree( Type const & = Type(), Simple_tree * = nullptr ); Type retrieve() const; Simple_tree *parent() const; int degree() const; bool is_root() const; bool is_leaf() const; Simple_tree *child( int n ) const; int height() const; void insert( Type const & ); void attach( Simple_tree * ); void detach(); }; 4.2.2

7 Abstract trees Implementation The tree with six nodes would be stored as follows: 4.2.2

8 Abstract trees Implementation Much of the functionality is similar to that of the Single_list class: template Simple_tree ::Simple_tree( Type const &obj, Simple_tree *p ): element( obj ), parent_node( p ) { // Empty constructor } template Type Simple_tree ::retrieve() const { return element; } template Simple_tree *Simple_tree ::parent() const { return parent_node; }

9 Abstract trees Implementation Much of the functionality is similar to that of the Single_list class: template bool Simple_tree ::is_root() const { return ( parent() == nullptr ); } template int Simple_tree ::degree() const { return children.size(); } template bool Simple_tree ::is_leaf() const { return ( degree() == 0 ); }

10 Abstract trees Implementation Accessing the n th child requires a for loop (  (n) ): template Simple_tree *Simple_tree ::child( int n ) const { if ( n = degree() ) { return nullptr; } ece250::Single_node *ptr = children.head(); for ( int i = 1; i < n; ++i ) { ptr = ptr->next(); } return ptr->retrieve(); }

11 Abstract trees Implementation Attaching a new object to become a child is similar to a linked list: template void Simple_tree ::attach( Type const &obj ) { children.push_back( new Simple_tree( obj, this ) ); }

12 Abstract trees Implementation To detach a tree from its parent: –If it is already a root, do nothing –Otherwise, erase this object from the parent’s list of children and set the parent pointer to zero template void Simple_tree ::detach() { if ( is_root() ) { return; } parent()->children.erase( this ); parent_node = nullptr; }

13 Abstract trees Implementation Attaching an entirely new tree as a sub-tree, however, first requires us to check if the tree is not already a sub-tree of another node: –If so, we must detach it first and only then can we add it template void Simple_tree ::attach( Simple_tree *tree ) { if ( !tree->is_root() ) { tree->detach(); } tree->parent_node = this; children.push_back( tree ); }

14 Abstract trees Implementation Suppose we want to find the size of a tree: –If there are no children, the size is 1 –Otherwise, the size is one plus the size of all the children template int Simple_tree ::size() const { int s = 1; for ( ece250::Single_node *ptr = children.head(); ptr != nullptr; ptr = ptr->next() ) { s += ptr->retrieve()->size(); } return s; }

15 Abstract trees Implementation Suppose we want to find the height of a tree: –If there are no children, the height is 0 –Otherwise, the height is one plus the maximum height of any sub tree #include //... template int Simple_tree ::height() const { int h = 0; for ( ece250::Single_node *ptr = children.head(); ptr != nullptr; ptr = ptr->next() ) { h = std::max( h, 1 + ptr->retrieve()->height() ); } return h; }

16 Abstract trees Implementation Implementing a tree by storing the children in an array is similar, however, we must deal with the full structure –A general tree using an array would have a constructor similar to: template Simple_tree ::Simple_tree( Type const &obj, Simple_tree *p ): element( obj ), parent_node( p ), child_count( 0 ), child_capacity( 4 ), children( new Simple_tree *[child_capacity] ) { // Empty constructor } 4.2.3

17 Abstract trees Locally Defined Orders In many, hierarchical orders are described locally: –One object is defined to be a descendant of another In Java, subclasses are given in the class definitions: public class Matrix { // implicitly extends Object //... } public class SymmetricMatrix extends Matrix { //... } –The parent is said to be the superclass, children are subclasses –The Object class is the root 4.2.4

18 Abstract trees Locally Defined Orders A directory structure is dynamically constructed through local definitions: {eceunix:1} mkdir ece250 –The parent is usually referred to as the parent directory Given the generic name.. E.g., cd.. –Children are referred to as subdirectories In Unix, there is a single root / In Microsoft Windows, each drive has a root, e.g., C:\ –The collection of all drives can be referred to as a forest 4.2.4

19 Abstract trees Summary In this topic, we have looked at one implementation of a general tree: –store the value of each node –store all the children in a linked list –not an easy (  (1) ) way to access children –if we use an array, different problems...

20 Abstract trees References [1]Donald E. Knuth, The Art of Computer Programming, Volume 1: Fundamental Algorithms, 3 rd Ed., Addison Wesley, 1997, §2.2.1, p.238.

21 Abstract trees Usage Notes These slides are made publicly available on the web for anyone to use If you choose to use them, or a part thereof, for a course at another institution, I ask only three things: –that you inform me that you are using the slides, –that you acknowledge my work, and –that you alert me of any mistakes which I made or changes which you make, and allow me the option of incorporating such changes (with an acknowledgment) in my set of slides Sincerely, Douglas Wilhelm Harder, MMath