COMP26120: Algorithms and Imperative Programming. Lecture 1 Trees

Similar documents
COSC 2011 Section N. Trees: Terminology and Basic Properties

csci 210: Data Structures Trees

Chapter. Basic Data Structures. Contents. 2.1 StacksandQueues Lists Trees Exercises... 84

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

Trees. CSE 373 Data Structures

Binary Trees

Binary Trees, Binary Search Trees

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

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

March 20/2003 Jayakanth Srinivasan,

Trees. Tree Structure Binary Tree Tree Traversals

Chapter 20: Binary Trees

interface Position<E> { E getelement() throws IllegalStateExcept..; }

Data Structures Lecture 6

Programming II (CS300)

Why Do We Need Trees?

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

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

Trees. Truong Tuan Anh CSE-HCMUT

CE 221 Data Structures and Algorithms

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

TREES. Trees - Introduction

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

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

BBM 201 Data structures

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

CSI33 Data Structures

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

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

Trees. Data structures and Algorithms. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme

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

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

Algorithms and Data Structures

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

Garbage Collection: recycling unused memory

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

Trees. Trees. CSE 2011 Winter 2007

Topic 14. The BinaryTree ADT

CSI33 Data Structures

Trees: examples (Family trees)

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2)

Trees. Eric McCreath

A Hierarchical Structure. Lecture11: Tree I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

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

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

Introduction to Computers and Programming. Concept Question

A Hierarchical Structure. Lecture11: Trees I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

CSC148 Week 6. Larry Zhang

Binary trees. Binary trees. Binary trees

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

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

Successor/Predecessor Rules in Binary Trees

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

INF2220: algorithms and data structures Series 1

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

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

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

Data Structures and Algorithms for Engineers

Binary Trees Fall 2018 Margaret Reid-Miller

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

tree nonlinear Examples

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

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

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

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

Trees. 9/21/2007 8:11 AM Trees 1

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

CSE 214 Computer Science II Introduction to Tree

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

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

Trees, Binary Trees, and Binary Search Trees

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

Binary search trees (BST) Binary search trees (BST)

Trees and Binary Trees

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

Programming II (CS300)

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees

Data Structures and Algorithms

Data Structures and Algorithms

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

Trees. Make Money Fast! Stock Fraud. Bank Robbery. Ponzi Scheme. Trees Goodrich, Tamassia

Stock Fraud. Nancy Amato. Parasol Lab, Dept. CSE, Texas A&M University

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

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

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

Lecture 26. Introduction to Trees. Trees

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

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

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


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

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

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

Trees Algorhyme by Radia Perlman

Transcription:

COMP26120: Algorithms and Imperative Programming Lecture 1 Trees

Lecture outline n Motivation n Definitions n Ordered trees n Generic methods for tree operations n Tree traversal (preorder, postorder, inorder) n Binary trees tree traversal

Definitions n An abstract data type for hierarchical storage of information; A B C D E F G

Definitions n The top element of a tree is referred to as the root of a tree; A root B C D E F G

Definitions n Each element in a tree (except the root) A B C D E an arbitrary element F G

Definitions n Each element in a tree (except the root) has a parent and zero or more children elements; A B C a parent of E D E an arbitrary element F G children of E

Examples Computer disc directory structure /root/user/ courses/ personal/ 20010/ 20081/ photos/ music/ 20051/ Lab1 Lab2 holiday/ birthday/ rock.mp3 ex2.java d1.dat out.file sara.jpg tim.jpg all.jpg pub.jpg

Examples A family tree Peter and Sara Jones Steven Prichard Mary Jones (Prichard) Claire Jones (Riley) Peter Riley Mark Prichard Catherine Prichard John Prichard Emma Riley

Definitions n A tree T is a non-empty set of nodes storing useful information in a parent-child relationship with the following properties: q T has a special node r referred to as the root; q Each node v of T different from r has a parent node u; n If the node u is the parent (ancestor) node of v, then v is a child (descendent) of u. Two children of the same parent are siblings. n A node is external (a leaf node) if it has no children and internal if it has one or more children.

Definitions n A sub-tree of T rooted at the node v is a tree consisting of all the descendants of v in T, including v itself. A B C D E F G

Ordered trees n A tree is ordered if a linear ordering relation is defined for the children of each node, that is, we can define an order among them. n Example: a book Book Preface Part 1 Part 2 Referen. Ch.1 Ch. 2 Ch. 3 Ch. 4 Ch. 5 S 1.1 S 1.2

Binary trees n A binary tree is an ordered tree in which each node has at most two children. n A binary tree is proper if each internal node has two children. n For each internal node its children are labelled as a left child and a right child. n The children are ordered so that a left child comes before a right child.

An example of a binary tree n Representing an arithmetic expression by a binary tree in which the external nodes are associated with variables or constants and the internal nodes are associated with 4 arithmetic operations.

An example of a binary tree n ((((3+1)*3)/((9-5)+2))-((3*(7-4))+6)) - / + * + * 6 + 3-2 3-3 1 9 5 7 4

The tree abstract data type n Elements of a tree are stored at positions (tree nodes) which are defined relative to neighbouring positions (parent-child relationships). n Accessor methods for the tree ADT: q root() returns the root of the tree; q parent(v) returns the parent node of v (an error if v is the root); q children(v) returns an iterator of the children of the node v (if v is a leaf node it returns an empty iterator);

n n The tree abstract data type Querry methods for the tree ADT: q q q isinternal(v) tests whether the node v is internal; isexternal(v) tests whether the node v is external; isroot(v) tests whether the node v is the root; Generic methods (not necessarily related to the tree structure): q q q q q size() returns the number of nodes of the tree; elements() returns the an iterator of all the elements stored at the nodes of the tree; positions() returns an iterator of all the nodes of the tree; swapelements(v,w) swaps the elements stored at the nodes v and w; replaceelement(v,e) returns the element v and replaces it with the element e;

Tree traversal n Complexity of the methods of the tree ADT: q q root() and parent(v) take O(1) time; isinternal(v), isexternal(v), isroot(v) take O(1) time; q children(v) takes O( cv) time, where cv is the number of children of v; q swapelements(v,w) and replaceelement(v,e) take O(1) time; q elements() and positions() take O(n) time, where n is the number of elements in the tree;

Depth of a node n Let v be a node of a tree T. The depth of v is a number of ancestors of v, excluding v itself. n The depth of the root is 0; n Recursive definition: q If v is a root, then the depth of v is 0; q Otherwise, the depth of v is one plus the depth of its parent; Algorithm depth(t,v) if T.isroot(v) then return 0 else return 1+depth(T,T.parent(v))

The height of a tree n It is equal to the maximum depth of an external node of T. n If the previous depth-finding algorithm is applied, the complexity would be. n A recursive definition of height of a node v in a tree T: q If v is an external node, the height of v is 0; q O( n 2 ) Otherwise, the height of v is one plus the maximum height of a child of v; Algorithm height(t,v) if T.isExternal(v) then return 0 else h=0 for each w in T.children(v) do h=max(h,height(t,w) return 1+h

A traversal of a tree n A traversal of a tree is a sustematic way of accessing (visiting) all the nodes of T. n There are two different traversal schemes for trees referred to as preorder and postorder. n In a preorder traversal of a tree T the root is visited first, and then the subtrees rooted at its children are traversed recursively. Algorithm preorder(t,v) perform the action on the node v for each child w of v call preorder(t,w)

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree call preorder(t,t.root) Algorithm preorder(t,v) perform the action on the node v A for each child w of v call preorder(t,w) B C D E F G

Preorder traversal of a tree n It is useful for producing a linear ordering of the nodes in a tree where parents are always before their children. n If a document is represented as a tree, the preorder traversal examines the document sequentially. n The overall running time of the preorder traversal is O(n).

Postorder traversal of a tree n It is a complementary algorithm to preorder traversal, as it traverses recursively the subtrees rooted at the children of the root first before visiting the root. Algorithm postorder(t,v) for each child w of v do call postorder(t,w) perform the action on the node v

Postorder traversal of a tree A B C action D E F G

Postorder traversal of a tree n The postorder traversal of a tree with n nodes takes O(n) time, assuming that visiting each node takes O(1) time. n The algorithm is useful if computing a certain property of a node in a tree requires that this property is previously computed for all its children.

Postorder traversal of a tree /root/user/ 1117k courses/ 74k personal/ 1042k 20010/ 71k 20051/ 1k 20081/ 1k photos/ 655k music/ 387k Lab1 1k Lab2 69k holiday/ 450k birthday/ 204k rock.mp3 386k ex2.java 2k d1.dat 50k out.file 16k sara.jpg 120k tim.jpg 146k all.jpg 183k pub.jpg 203k

Binary trees n A proper binary tree is an ordered tree in which each internal node has exactly two children. n As an ADT, a binary tree supports 3 additional accessor methods: q leftchild(v) returns the left child of v; if v is an external node, an error occurs; q rightchild(v) returns the right child of v; if v is an external node, an error occurs; q sibling(v) returns the sibling of v; an error occurs if v is the root;

Binary trees n Denote all the nodes of a binary tree T at the same depth d as the level d of T; n Level 0 has 1 node (the root), level 1 has at most 2 d nodes, etc. In general, level d has at most 2 nodes; n In a proper binary tree the number of external nodes is 1 more than the number of internal nodes;

Binary trees Level 0 (1) Level 1 (2) Level 2 (4) Level 3 (8)

Preorder traversal of a binary tree Algorithm binarypreorder(t,v) perform the action on the node v if v is an internal node then call binarypreorder(t,t.leftchild(v)) call binarypreorder(t,t.rightchild(v))

Postorder traversal of a binary tree Algorithm binarypostorder(t,v) if v is an internal node then call binarypostorder(t,t.leftchild(v)) call binarypostorder(t,t.rightchild(v)) perform the action on the node v

Inorder traversal of a binary tree n n In this method the action on a node v is performed in between the recursive traversals on its left and right subtrees. The inorder traversal of a binary tree can be viewed as visiting the nodes from left to right. Algorithm binaryinorder(t,v) if v is an internal node then call binaryinorder(t,t.leftchild(v)) perform the action on the node v if v is an internal node then call binaryinorder(t,t.rightchild(v))