Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 (4.1-4.2) Weiss.

Slides:



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

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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 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.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Trees.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
Trees CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
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.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Tree Data Structures.
1 CSC 211 Data Structures Lecture 24 Dr. Iftikhar Azim Niaz 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Trees trees binary trees traversals of trees template method pattern
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
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.
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees (Unit 7).
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.
1 CMSC 341 Introduction to Trees Textbook sections:
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.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
CSCE 3110 Data Structures & Algorithm Analysis
Trees 5/2/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,
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,
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Trees 二○一八年八月二十六日 Part-C Trees Trees.
CMSC 341 Introduction to Trees.
Binary Trees, Binary Search Trees
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
Introduction to Trees IT12112 Lecture 05.
Trees.
Week nine-ten: Trees Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Tree.
CS210- Lecture 9 June 20, 2005 Announcements
Binary Trees.
Binary Trees, Binary Search Trees
Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss

Books

More Trees Examples Unix / Windows file structure

Definition of Tree A tree is a finite set of one or more nodes such that: There is a specially designated node called the root. The remaining nodes are partitioned into n>=0 disjoint sets T 1,..., T n, where each of these sets is a tree. We call T 1,..., T n the subtrees of the root.

Level and Depth Level node (13) degree of a node leaf (terminal) nonterminal parent children sibling degree of a tree (3) ancestor level of a node height of a tree (4)

Terminology The degree of a node is the number of subtrees of the node The degree of A is 3; the degree of C is 1. The node with degree 0 is a leaf or terminal node. A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the children of the node. Children of the same parent are siblings. The ancestors of a node are all the nodes along the path from the root to the node.

Tree Properties A B C D G EF IH PropertyValue Number of nodes Height Root Node Leaves Interior nodes Number of levels Ancestors of H Descendants of B Siblings of E Right subtree

Representation of Trees List Representation ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) The root comes first, followed by a list of sub-trees datalink 1link 2...link n How many link fields are needed in such a representation?

A Tree Node Every tree node: object – useful information children – pointers to its children nodes O OO O O

Left Child - Right Sibling ABCD E F G H I J K LM data left child right sibling

Tree ADT Objects: any type of objects can be stored in a tree Methods: accessor methods root() – return the root of the tree parent(p) – return the parent of a node children(p) – returns the children of a node query methods size() – returns the number of nodes in the tree isEmpty() - returns true if the tree is empty elements() – returns all elements isRoot(p), isInternal(p), isExternal(p)

Tree Traversal Two main methods: Preorder Postorder Recursive definition PREorder: visit the root traverse in preorder the children (subtrees) POSTorder traverse in postorder the children (subtrees) visit the root

Trees Tree traversals: Inorder traversal – prints the node values in ascending order 1. Traverse the left subtree with an inorder traversal 2. Process the value in the node (i.e., print the node value) 3. Traverse the right subtree with an inorder traversal Preorder traversal 1. Process the value in the node 2. Traverse the left subtree with a preorder traversal 3. Traverse the right subtree with a preorder traversal Postorder traversal 1. Traverse the left subtree with a postorder traversal 2. Traverse the right subtree with a postorder traversal 3. Process the value in the node

Preorder preorder traversal Algorithm preOrder(v) “visit” node v for each child w of v do recursively perform preOrder(w)

Postorder postorder traversal Algorithm postOrder(v) for each child w of v do recursively perform postOrder(w) “visit” node v du (disk usage) command in Unix

Binary Trees A special class of trees: max degree for each node is 2 Recursive definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree. Any tree can be transformed into binary tree. by left child-right sibling representation

Example J IM H L A B C D E F G K

Samples of Trees ABABABCGEIDHF Complete Binary Tree Skewed Binary Tree ECD

Maximum Number of Nodes in BT The maximum number of nodes on level i of a binary tree is 2 i-1, i>=1. The maximum nubmer of nodes in a binary tree of depth k is 2 k -1, k>=1.

Full BT vs. Complete BT A full binary tree of depth k is a binary tree of depth k having 2 -1 nodes, k>=0. A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. k A B C G E I D H F A B C G E K D J F I H O N M L Full binary tree of depth 4 Complete binary tree

Sequential Representation A B -- C -- D --. E [1] [2] [3] [4] [5] [6] [7] [8] [9]. [16] [1] [2] [3] [4] [5] [6] [7] [8] [9] ABCDEFGHIABCDEFGHI ABECDABCGEIDHF (1) waste space (2) insertion/deletion problem

Linked Representation typedef struct tnode *ptnode; typedef struct tnode { int data; ptnode left, right; }; data leftright data leftright

Binary Tree Traversals Let L, V, and R stand for moving left, visiting the node, and moving right. There are six possible combinations of traversal lRr, lrR, Rlr, Rrl, rRl, rlR Adopt convention that we traverse left before right, only 3 traversals remain lRr, lrR, Rlr inorder, postorder, preorder

CS Illustrations for Traversals Assume: visiting a node is printing its label Preorder: Inorder: Postorder:

CS Illustrations for Traversals (Contd.) Assume: visiting a node is printing its data Preorder: Inorder: Postorder:

Arithmetic Expression Using BT +*A*/EDCB inorder traversal A / B * C * D + E infix expression preorder traversal + * * / A B C D E prefix expression postorder traversal A B / C * D * E + postfix expression level order traversal + * E * D / C A B

Inorder Traversal (recursive version) void inorder(ptnode ptr) /* inorder tree traversal */ { if (ptr) { inorder(ptr->left); printf(“%d”, ptr->data); indorder(ptr->right); } A / B * C * D + E

Preorder Traversal (recursive version) void preorder(ptnode ptr) /* preorder tree traversal */ { if (ptr) { printf(“%d”, ptr->data); preorder(ptr->left); predorder(ptr->right); } + * * / A B C D E

Postorder Traversal (recursive version) void postorder(ptnode ptr) /* postorder tree traversal */ { if (ptr) { postorder(ptr->left); postdorder(ptr->right); printf(“%d”, ptr->data); } A B / C * D * E +

Level Order Traversal (using queue) void levelOrder(ptnode ptr) /* level order tree traversal */ { int front = rear = 0; ptnode queue[MAX_QUEUE_SIZE]; if (!ptr) return; /* empty queue */ enqueue(front, &rear, ptr); for (;;) { ptr = dequeue(&front, rear);

if (ptr) { printf(“%d”, ptr->data); if (ptr->left) enqueue(front, &rear, ptr->left); if (ptr->right) enqueue(front, &rear, ptr->right); } else break; } + * E * D / C A B

Application: Evaluation of Expressions +*A*/EDCB inorder traversal A / B * C * D + E infix expression preorder traversal + * * / A B C D E prefix expression postorder traversal A B / C * D * E + postfix expression level order traversal + * E * D / C A B