Trees. Carlos Moreno uwaterloo.ca EIT https://ece.uwaterloo.ca/~cmoreno/ece250

Similar documents
11 TREES DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

singly and doubly linked lists, one- and two-ended arrays, and circular arrays.

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Trees. Carlos Moreno uwaterloo.ca EIT

Binary Trees

Data Structures and Algorithms

Heap sort. Carlos Moreno uwaterloo.ca EIT

Binary Search Trees. Carlos Moreno uwaterloo.ca EIT

CSE 214 Computer Science II Introduction to Tree

Heap sort. Carlos Moreno uwaterloo.ca EIT

CSC148 Week 6. Larry Zhang

Binary Search Trees. Carlos Moreno uwaterloo.ca EIT

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Binary Trees, Binary Search Trees

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

Graphs. Carlos Moreno uwaterloo.ca EIT

Minimum spanning trees

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

2. Write style rules for how you d like certain elements to look.

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

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Tree Structures. A hierarchical data structure whose point of entry is the root node

Binary Trees Case-studies

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

Trees. CSE 373 Data Structures

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

birds fly S NP N VP V Graphs and trees

Dijkstra's Algorithm

Design Pattern: Composite

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

CE 221 Data Structures and Algorithms

Why Do We Need Trees?

CS301 - Data Structures Glossary By

(Refer Slide Time: 06:01)

E , Fall 2007 More with stylesheets

Algorithms. Deleting from Red-Black Trees B-Trees

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

To link to an external stylesheet, the link element is placed within the head of the html page:

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Trees. Eric McCreath

Topic 14. The BinaryTree ADT

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

Programming II (CS300)

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

Algorithm Analysis. Carlos Moreno uwaterloo.ca EIT Θ(n) Ω(log n) Θ(1)

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

The Benefits of CSS. Less work: Change look of the whole site with one edit

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

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

Header. Article. Footer

Data and File Structures Laboratory

XML: Extensible Markup Language

Data structures. Priority queues, binary heaps. Dr. Alex Gerdes DIT961 - VT 2018

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

CS 171: Introduction to Computer Science II. Binary Search Trees

Title: Sep 12 10:58 AM (1 of 38)

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

BBM 201 Data structures

One of the main selling points of a database engine is the ability to make declarative queries---like SQL---that specify what should be done while

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

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

TREES Lecture 12 CS2110 Fall 2016

Semi-structured Data. 8 - XPath

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Chapter 1 Getting Started with HTML 5 1. Chapter 2 Introduction to New Elements in HTML 5 21

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 27-1

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Trees. Tree Structure Binary Tree Tree Traversals

TREES Lecture 10 CS2110 Spring2014

CSE 100 Advanced Data Structures

Web Services Week 3. Fall Emrullah SONUÇ. Department of Computer Engineering Karabuk University

TREES Lecture 12 CS2110 Spring 2019

CS 106X Lecture 18: Trees

Data Structures and Algorithms

CSC148H Week 8. Sadia Sharmin. July 12, /29

Trees, Part 1: Unbalanced Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Binary Trees. Height 1

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Hash Tables (Cont'd) Carlos Moreno uwaterloo.ca EIT

CHAPTER 2 MARKUP LANGUAGES: XHTML 1.0

Balanced BST and AVL Trees

Garbage Collection: recycling unused memory

CSI 3140 WWW Structures, Techniques and Standards. Browsers and the DOM

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

Review of HTML. Chapter Pearson. Fundamentals of Web Development. Randy Connolly and Ricardo Hoar

Introduction to Web Development

Algorithms and Data Structures

Data Structure - Binary Tree 1 -

March 20/2003 Jayakanth Srinivasan,

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

CS 206 Introduction to Computer Science II

ITEC2620 Introduction to Data Structures

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

The World Wide Web is a technology beast. If you have read this book s

Transcription:

Carlos Moreno cmoreno @ uwaterloo.ca EIT-4103 https://ece.uwaterloo.ca/~cmoreno/ece250

Standard reminder to set phones to silent/vibrate mode, please!

Announcements Part of assignment 3 posted additional questions on trees will be posted by Friday or possibly Monday. But you can get started with the questions on topics already covered.

Announcements Part of assignment 3 posted additional questions on trees will be posted by Friday or possibly Monday. But you can get started with the questions on topics already covered. From what I'm told, Friday next week we can move our lecture to 3:30 (if everyone agrees).

Today's class: Definition of the tree data structure and its components Related concepts: Types of nodes, relations between nodes, paths and their attributes, subtrees We'll look at one example HTML/CSS as a hierarchical structure that can be represented with trees.

As already mentioned earlier in the course, trees are the structure to be used to represent hierarchical data.

As already mentioned earlier in the course, trees are the structure to be used to represent hierarchical data. Perhaps a slight twist is that we will see some cases in which some characteristics of the tree will make them suitable for other situations as well, where the data is not hierarchical at all.

A rooted tree data structure stores information in nodes If we look at it by analogy with linked lists: There is a first node, or root Each node has variable number of next pointers Each node, other than the root, has exactly one node pointing to it

A rooted tree data structure stores information in nodes If we look at it by analogy with linked lists: There is a first node, or root Each node has variable number of next pointers Each node, other than the root, has exactly one node pointing to it If we extend the analogy to doubly-linked lists: Each node could have a previous pointer, pointing to the node immediately above it in the hierarchy.

A rooted tree data structure stores information in nodes

Terminology: Parent and child nodes: Respectively, the node immediately above and nodes immediately below in the hierarchy

Terminology: Parent and child nodes: Respectively, the node immediately above and nodes immediately below in the hierarchy Every tree has exactly one node that has no parent (why just one?)

Terminology: Parent and child nodes: Respectively, the node immediately above and nodes immediately below in the hierarchy Every tree has exactly one node that has no parent (why just one? we recall that this is one of the aspects that distinguishes hierarchical from partial order; more than one can happen for partial orderings, but not for hierarchical orderings)

Terminology: Parent and child nodes: Respectively, the node immediately above and nodes immediately below in the hierarchy Every tree has exactly one node that has no parent (the top of the hierarchy) This node is called the root node.

Terminology: Parent and child nodes: Respectively, the node immediately above and nodes immediately below in the hierarchy Every tree has exactly one node that has no parent (the top of the hierarchy) This node is called the root node. Every other node has: Exactly one parent node, or just parent. Zero or more child nodes, or just children.

Example: Node H is the root node (the node containing H) Node I has three children, and its parent is node H

Terminology: The degree of a node is defined as the number of children it has example: deg(i) is 3.

Terminology: Nodes with the same parent are sibling nodes, or just siblings example: nodes J, K, L are siblings.

Terminology: Nodes with degree zero are called leaf nodes. The others are called internal nodes (or informally, non-leaf nodes)

Terminology: These trees are equal if the order of the children is ignored (unordered trees) They are different if order is relevant (ordered trees)

Terminology: A path is a sequence of nodes (n 0, n 1,, n L ) where n k+1 is a child of n k 0 k < L The length of this path is L notice that the length is NOT the number of nodes involved; a good analogy would be that of the measured distance between the extremes.

Example: The path (B, E, G) has length 2

For each node in a tree, there exists a unique path (why unique?) from the root to that node.

For each node in a tree, there exists a unique path (why unique?) from the root to that node. The length of this path is the depth of the node Node E has depth 2 Node L has depth 3

For each node in a tree, there exists a unique path (why unique?) from the root to that node. The length of this path is the depth of the node Node E has depth 2 Node L has depth 3 The root node has depth 0 (right?)

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 For convenience, we define the height of an empty tree to be 1

Terminology: If a path exists from node a to node b, then: a is an ancestor of b b is a descendent of a

Terminology: If a path exists from node a to node b, then: a is an ancestor of b b is a descendent of a Thus, every node is both an ancestor and a descendant of itself

Terminology: If a path exists from node a to node b, then: a is an ancestor of b b is a descendent of a Thus, every node is both an ancestor and a descendant of itself We can add the qualifier strict to exclude equality: a is a strict descendant of b if a is a descendant of b but a b

Terminology: If a path exists from node a to node b, then: a is an ancestor of b b is a descendent of a Thus, every node is both an ancestor and a descendant of itself We can add the qualifier strict to exclude equality: a is a strict descendant of b if a is a descendant of b but a b The root node is an ancestor of all nodes

Example: The descendants of node B are B, C, D, E, F, and G: The ancestors of node I are I, H, and A:

Terminology: Given a node N within a tree with root R, the collection of all of the descendants of N (along with the associations) is said to be a subtree of the given tree, with root node N.

Terminology: Given a node N within a tree with root R, the collection of all of the descendants of N (along with the associations) is said to be a subtree of the given tree, with root node N. In that spirit, we could provide a recursive definition of a tree: A single node is a tree (node with degree 0) A node with degree n is the root of a tree if all of its n children are the root of disjoint trees (no common nodes with the other subtrees)

Example/Case-Study A somewhat obvious example of when Tree data structures may be useful is HTML (or XHTML, XML, whatever they're calling it these days :-) ) Writing (or reading) HTML of course does not require explicit use of tree data structures. But how about a program that needs to process it? Obvious example: the rendering engine of a web browser. It needs to take into account the hierarchical structure of the code to be able to determine the actual graphical look (rendering).

Example/Case-Study A somewhat obvious example of when Tree data structures may be useful is HTML (or XHTML, XML, whatever they're calling it these days :-) ) Even more: the document can be dynamically modified (for example, through JavaScript, which is a client-side, browser-powered scripting language) So, the browser software better have efficient mechanisms to determine the new rendering!

Example/Case-Study A somewhat obvious example of when Tree data structures may be useful is HTML (or XHTML, XML, whatever they're calling it these days :-) ) Again, we keep in mind: HTML/XML is hierarchical by nature, so a data structure that represents this hierarchy has to be the right solution!

Example/Case-Study The main idea (this is way oversimplified, BTW) with HTML is the use of tags that define a component (usually something that will display on the browser area). Tags can enclose other (nested) tags. Not unlike blocks in a program. Tags usually have opening and closing components, enclosed in angle brackets (< >), with the closing one having a slash before the name: Example: <title>ece-250</title>

Example/Case-Study Consider a more general example: <html> <head> <title>hello World!</title> </head> <body> <h1>this is a <u>heading</u></h1> <p>this is a paragraph with some <u>underlined</u> text.</p> </body> </html>

Example/Case-Study The hierarchy of nested tags defines a tree with root being the <html> tag: <html> <head> </head> <body> <title>hello World!</title> <h1>this is a <u>heading</u></h1> <p>this is a paragraph with some <u>underlined</u> text.</p> </body> </html>

Example/Case-Study The hierarchy of nested tags defines a tree with root being the <html> tag: <html> <head> </head> <body> <title>hello World!</title> <h1>this is a <u>heading</u></h1> <p>this is a paragraph with some <u>underlined</u> text.</p> </body> </html>

Example/Case-Study The rendering software defines a tree structure where the nodes store the attributes of the tag, and the child nodes are the nested tags. This allows it to efficiently propagate (inherit) attributes (font, size, color, border, etc.) to all nested tags (a behaviour specified by HTML)

Example/Case-Study For example, if the <body> tag has some attributes associated, then the tree allows the browser software to efficiently determine where these attributes should propagate:

Example/Case-Study Perhaps even more important, when dynamically changing things, the browser can efficiently determine what elements are affected, just following links in the tree:

Example/Case-Study Notice that this includes the fact that children nodes will (may) have an effect on the parents, repetitively all the way up to the root: If the font size of the second tag <u> is dynamically changed, we need be able to efficiently determine that this will make the <p> tag's rendering increase, making the <body> tag affected as well.

Example/Case-Study An observation: this is an example of an ordered tree. In HTML, tags are graphically rendered according to the order in which they appear (by default if we use CSS to define absolute positioning, things change; still, that's usually done, if at all, just for some of the tags)

Bottom line: Trees are a very fundamental and very powerful data structure: You will see them again in the near future (and I mean your future courses, not the future lessons in our course! :-) )

Bottom line: Trees are a very fundamental and very powerful data structure: You will see them again in the near future (and I mean your future courses, not the future lessons in our course! :-) ) That you take a path in Control systems, Telecomm and Digital signal processing, Image processing or Computer graphics, embedded systems no matter what, you'll be seeing heavy use of trees as a powerful tool to efficiently solve many of the domain-specific problems.

Bottom line: Trees are a very fundamental and very powerful data structure: That you take a path in Control systems, Telecomm and Digital signal processing, Image processing or Computer graphics no matter what, you'll be seeing heavy use of trees as a powerful tool to efficiently solve many of those problems. HTML will seem like a toy example by comparison (we'll go over some of these additional case-studies as we progress with our coverage of trees)

Summary During today's class, we discussed: Definition of trees and related components Root, internal, and lead nodes. Parent, children, and sibling nodes. Path, path length, height, depth. Ancestors and descendants. Ordered and unordered trees. Subtrees. An example/case-study: HTML rendering.