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,
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.
HTML5 and CSS3 Illustrated Unit B: Getting Started with HTML
Tools for Text Review. Algorithms The heart of computer science Definition: A finite sequence of instructions with the properties that –Each instruction.
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,
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,
Tutorial 1 Developing a Basic Web Page. New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives – Lesson 1 Introduction to the.
Proof by Induction.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
CS261 Data Structures Trees Introduction and Applications.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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,
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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,
HTML: Hyptertext Markup Language Doman’s Sections.
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,
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Data Structures TREES.
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,
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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,
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Tutorial #1 Using HTML to Create Web Pages. HTML, XHTML, and CSS HTML – HyperText Markup Language The tags the browser uses to define the content of the.
IN THIS LESSON, WE WILL BECOME FAMILIAR WITH HTML AND BEGIN CREATING A WEB PAGE IN YOUR COMPUTER HTML – the foundation.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
HTML5 and CSS3 Illustrated Unit B: Getting Started with HTML.
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.
1 Graph theory Outline A graph is an abstract data type for storing adjacency relations –We start with definitions: Vertices, edges, degree and sub-graphs.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
1 Complete binary trees Outline Introducing complete binary trees –Background –Definitions –Examples –Logarithmic height –Array storage.
Web Basics: HTML/CSS/JavaScript What are they?
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
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.
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
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. The Tree Data Structure

2 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 leaf nodes Parents, children, and siblings Paths, path length, height, and depth Ancestors and descendants Ordered and unordered trees Subtrees –Examples XHTML and CSS

3 The tree data structure The Tree Data Structure Trees are the first data structure different from what you’ve seen in your first-year programming courses

4 The tree data structure Trees A rooted tree data structure stores information in nodes –Similar to linked lists: There is a first node, or root Each node has variable number of references to successors Each node, other than the root, has exactly one node pointing to it 4.1.1

5 The tree data structure Terminology All nodes will have zero or more child nodes or children –I has three children: J, K and L For all nodes other than the root node, there is one parent node –H is the parent I

6 The tree data structure Terminology The degree of a node is defined as the number of its children: deg( I ) = 3 Nodes with the same parent are siblings –J, K, and L are siblings

7 The tree data structure Terminology Phylogenetic trees have nodes with degree 2 or 0:

8 The tree data structure Terminology Nodes with degree zero are also called leaf nodes All other nodes are said to be internal nodes, that is, they are internal to the tree

9 The tree data structure Terminology Leaf nodes:

10 The tree data structure Terminology Internal nodes:

11 The tree data structure Terminology These trees are equal if the order of the children is ignored –unordered trees They are different if order is relevant (ordered trees) –We will usually examine ordered trees (linear orders) –In a hierarchical ordering, order is not relevant

12 The tree data structure Terminology The shape of a rooted tree gives a natural flow from the root node, or just root

13 The tree data structure Terminology A path is a sequence of nodes (a 0, a 1,..., a n ) where a k + 1 is a child of a k is The length of this path is n E.g., the path (B, E, G) has length

14 The tree data structure Terminology Paths of length 10 (11 nodes) and 4 (5 nodes) Start of these paths End of these paths

15 The tree data structure Terminology For each node in a tree, there exists a unique path from the root node to that node The length of this path is the depth of the node, e.g., –E has depth 2 –L has depth

16 The tree data structure Terminology Nodes of depth up to

17 The tree data structure Terminology The height of a tree is defined as the maximum depth of any node within the tree The height of a tree with one node is 0 –Just the root node For convenience, we define the height of the empty tree to be –

18 The tree data structure Terminology The height of this tree is

19 The tree data structure Terminology If a path exists from node a to node b : – a is an ancestor of b – b is a descendent of a Thus, a node is both an ancestor and a descendant of itself –We can add the adjective strict to exclude equality: a is a strict descendent of b if a is a descendant of b but a ≠ b The root node is an ancestor of all nodes

20 The tree data structure Terminology The descendants of node B are B, C, D, E, F, and G: The ancestors of node I are I, H, and A:

21 The tree data structure Terminology All descendants (including itself) of the indicated node

22 The tree data structure Terminology All ancestors (including itself) of the indicated node

23 The tree data structure Terminology Another approach to a tree is to define the tree recursively: –A degree- 0 node is a tree –A node with degree n is a tree if it has n children and all of its children are disjoint trees (i.e., with no intersecting nodes) Given any node a within a tree with root r, the collection of a and all of its descendants is said to be a subtree of the tree with root a 4.1.2

24 The tree data structure Example: XHTML and CSS The XML of XHTML has a tree structure Cascading Style Sheets (CSS) use the tree structure to modify the display of HTML 4.1.3

25 The tree data structure Example: XHTML and CSS Consider the following XHTML document Hello World! This is a Heading This is a paragraph with some underlined text

26 The tree data structure Example: XHTML and CSS Consider the following XHTML document Hello World! This is a Heading This is a paragraph with some underlined text. heading underlining paragraph body of page title 4.1.3

27 The tree data structure Example: XHTML and CSS The nested tags define a tree rooted at the HTML tag Hello World! This is a Heading This is a paragraph with some underlined text

28 The tree data structure Web browsers render this tree as a web page Example: XHTML and CSS 4.1.3

29 The tree data structure Example: XHTML and CSS XML tags... must be nested For example, to get the following effect: you may use You may not use:

30 The tree data structure Example: XHTML and CSS Cascading Style Sheets (CSS) make use of this tree structure to describe how HTML should be displayed –For example: h1 { color:blue; } indicates all text/decorations descendant from an h1 header should be blue

31 The tree data structure Example: XHTML and CSS For example, this style renders as follows: h1 { color:blue; }

32 The tree data structure Example: XHTML and CSS For example, this style renders as follows: h1 { color:blue; } u { color:red; }

33 The tree data structure Example: XHTML and CSS Suppose you don’t want underlined items in headers ( h1 ) to be red –More specifically, suppose you want any underlined text within paragraphs to be red That is, you only want text marked as text to be underlined if it is a descendant of a tag

34 The tree data structure Example: XHTML and CSS For example, this style renders as follows: h1 { color:blue; } p u { color:red; }

35 The tree data structure Example: XHTML and CSS You can read the second style h1 { color:blue; } p u { color:red; } as saying “text/decorations descendant from the underlining tag ( ) which itself is a descendant of a paragraph tag should be coloured red”

36 The tree data structure Example: XML In general, any XML can be represented as a tree –All XML tools make use of this feature –Parsers convert XML into an internal tree structure –XML transformation languages manipulate the tree structure E.g., XMLT

37 The tree data structure MathML: x 2 + y 2 = z 2 x 2 + y 2 = z 2 x 2 y 2 z 2 x^2+y^2 = z^

38 The tree data structure MathML: x 2 + y 2 = z 2 The tree structure for the same MathML expression is

39 The tree data structure MathML: x 2 + y 2 = z 2 Why use 500 characters to describe the equation x 2 + y 2 = z 2 which, after all, is only twelve characters (counting spaces)? The root contains three children, each different codings of: –How it should look (presentation), –What it means mathematically (content), and –A translation to a specific language (Maple)

40 The tree data structure Summary In this topic, we have: –Introduced the terminology used for the tree data structure –Discussed various terms which may be used to describe the properties of a tree, including: root node, leaf node parent node, children, and siblings ordered trees paths, depth, and height ancestors, descendants, and subtrees –We looked at XHTML and CSS

41 The tree data structure 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.

42 The tree data structure 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