COMP20010: Algorithms and Imperative Programming Lecture 1 Trees.

Slides:



Advertisements
Similar presentations
Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Trees1 More on Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
Trees1 Recursion Recursion is a concept of defining a method that makes a call to itself.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Trees COMP53 Oct 31, What is a Tree? A tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation.
Trees1 Part-C Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Trees CS /02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Make Money Fast! Stock Fraud Apply shortcuts Bank Robbery Just For Laughs Trees This one is cool eITnotes.com For more notes and topics visit:
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
© 2004 Goodrich, Tamassia Trees1 Text Book: M. T. Goodrich and R. Tamassia, "Data Structures and Algorithms in Java," 4th edition, 2006, Wiley & Sons,
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Compiled by: Dr. Mohammad Omar Alhawarat
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Lecture11: Tree I Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Data Structures TREES.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2013 Goodrich, Tamassia, Goldwasser.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CH 7. TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.
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.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Recursive Objects (Part 4)
University of Palestine
Trees 二○一八年八月二十六日 Part-C Trees Trees.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
CS223 Advanced Data Structures and Algorithms
COMP26120: Algorithms and Imperative Programming
Week nine-ten: Trees Trees.
Trees Palestine Gaza West Bank 48 Stolen Land Trees Trees
Binary Trees, Binary Search Trees
Trees.
CS210- Lecture 9 June 20, 2005 Announcements
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

COMP20010: Algorithms and Imperative Programming Lecture 1 Trees

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

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

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

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

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

Examples Computer disc directory structure /root/user/ courses/personal/ 20010/20081/ photos/music/ holiday/ Lab2 … birthday/ Lab / ex2.javad1.datout.file sara.jpgtim.jpgall.jpgpub.jpg rock.mp3

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 A tree T is a non-empty set of nodes storing useful information in a parent-child relationship with the following properties:  T has a special node r referred to as the root;  Each node v of T different from r has a parent node u; 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. A node is external (a leaf node) if it has no children and internal if it has one or more children.

Definitions 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 FG

Ordered trees 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. Example: a book Book PrefacePart 1Part 2Referen. Ch.1Ch. 2Ch. 3Ch. 4Ch. 5 S 1.1S 1.2 …

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

An example of a binary tree 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 ((((3+1)*3)/((9-5)+2))-((3*(7-4))+6)) - / + * *

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

The tree abstract data type Querry methods for the tree ADT:  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):  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 Complexity of the methods of the tree ADT:  root() and parent(v) take O(1) time;  isInternal(v), isExternal(v), isRoot(v) take O(1) time;  children(v) takes O( ) time, where is the number of children of v;  swapElements(v,w) and replaceElement(v,e) take O(1) time;  elements() and positions() take O(n) time, where n is the number of elements in the tree;

Depth of a node Let v be a node of a tree T. The depth of v is a number of ancestors of v, excluding v itself. The depth of the root is 0; Recursive definition:  If v is a root, then the depth of v is 0;  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 It is equal to the maximum depth of an external node of T. If the previous depth-finding algorithm is applied, the complexity would be. A recursive definition of height of a node v in a tree T:  If v is an external node, the height of v is 0;  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 A traversal of a tree is a sustematic way of accessing (visiting) all the nodes of T. There are two different traversal schemes for trees referred to as preorder and postorder. 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 for each child w of v call preorder(T,w) A B C D E FG

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Postorder traversal of a tree 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 D E FG action

Postorder traversal of a tree The postorder traversal of a tree with n nodes takes O(n) time, assuming that visiting each node takes O(1) time. 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 20081/ 1k photos/ 655k music/ 387k holiday/ 450k Lab2 69k … birthday/ 204k Lab1 1k 20051/ 1k ex2.java 2k d1.dat 50k out.file 16k sara.jpg 120k tim.jpg 146k all.jpg 183k pub.jpg 203k rock.mp3 386k

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

Binary trees Denote all the nodes of a binary tree T at the same depth d as the level d of T; Level 0 has 1 node (the root), level 1 has at most 2 nodes, etc. In general, level d has at most nodes; 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 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.leftChild(v))