COMP26120: Algorithms and Imperative Programming

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

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
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.
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:
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.
COMP20010: Algorithms and Imperative Programming Lecture 1 Trees.
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)
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
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.
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.
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.
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.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Trees 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
University of Palestine
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Trees 二○一八年八月二十六日 Part-C Trees Trees.
Trees Chuan-Ming Liu Computer Science & Information Engineering
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
Trees.
Week nine-ten: Trees Trees.
Trees Palestine Gaza West Bank 48 Stolen Land Trees Trees
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
CS210- Lecture 9 June 20, 2005 Announcements
Trees.
Binary Trees.
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Trees.
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:

COMP26120: 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 C B E D F G

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

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

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

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

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 C B E D F G

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 Preface Part 1 Part 2 Referen. Ch.1 Ch. 2 Ch. 3 Ch. 4 Ch. 5 … S 1.1 S 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)) - + / * 6 + * - + 3 - 2 3 9 5 7 4 3 1

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E D F G

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 C B E action D F G

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 photos/ 655k music/ 387k 20010/ 71k 20051/ 1k 20081/ 1k rock.mp3 386k … holiday/ 450k birthday/ 204k Lab1 1k Lab2 69k sara.jpg 120k tim.jpg 146k all.jpg 183k pub.jpg 203k ex2.java 2k d1.dat 50k out.file 16k

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