CS 201 Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

CHAPTER 4 TREES §1 Preliminaries 1. Terminology Lineal Tree Pedigree Tree ( binary tree ) 1/17.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Trees, Binary Trees, and Binary Search Trees COMP171.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Trees, Binary Trees, and Binary Search Trees COMP171.
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.
Data Structures – Week #5 Trees (Ağaçlar). December 4, 2015Borahan Tümer, Ph.D.2 Trees (Ağaçlar) Toros GöknarıAvrupa Göknarı.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
CSE 373 Data Structures Lecture 7
Trees Saurav Karmakar
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Csc 2720 Instructor: Zhuojun Duan
Algorithms and Data Structures Recursion and Binary Tree
CMSC 341 Introduction to Trees.
CSE 373 Data Structures Lecture 7
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
CS212: Data Structures and Algorithms
Trees CSE 373 Data Structures.
Trees.
Trees CMSC 202, Version 5/02.
Data Structures – Week #5
CMSC 202 Trees.
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Binary Search Trees < > = Binary Search Trees
Trees BST, AVL, SPLAY TREES
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

CS 201 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 Izmir University of Economics

Izmir University of Economics Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root, and zero or more nonempty (sub)trees T1, T2, ..., Tk, each of whose roots are connected by a directed edge from r. The root of each subtree is said to be a child of r, and r is the parent of each subtree root. Izmir University of Economics

Izmir University of Economics Preliminaries - II Observation: For a tree with N nodes, there are N-1 edges. Proof: Each edge connects a node to its parent and every node except the root has one parent. Izmir University of Economics

Izmir University of Economics Preliminaries - III The root is A. Node F has A as a parent and K, L, and M as children. Each node may have an arbitrary number of children, possibly zero. Nodes with no children are known as leaves (leaf node). B, C, H, I, P, Q, K, L, M, and N are leaves. Nodes with the same parent are siblings; thus K, L, and M are all siblings. Grandparent and grandchild relations can be defined in a similar manner. Izmir University of Economics

Izmir University of Economics Preliminaries - IV A path from node n1 to nk is defined as a sequence of nodes n1, n2, ..., nk such that ni is the parent of ni+1 for 1 ≤ i < k. The length of this path is the number of edges on the path, namely k - 1. There is a path of length zero from every node to itself. Notice that there is exactly one path from the root to each node. For any node ni, the depth of ni is the length of the unique path from the root to ni. Thus, the root is at depth 0. The height of ni is the length of the longest path from ni to a leaf. Thus all leaves are at height 0. The height of a tree is equal to the height of the root. The depth of a tree is equal to the depth of the deepest leaf which is always equal to the height of the tree. If there is path from n1 to n2 (n1≠n2), then n1 is an (proper) ancestor of n2 and n2 is a (proper) descendant of n1. Izmir University of Economics

Implementation of Trees One way is to have in each node besides its data, a link to to each child of that node. However, since the number of children per node can vary greatly and is not known in advance, it might be infeasible. The solution is simple: keep the children in a linked list of tree nodes. Izmir University of Economics

Tree Traversals with an Application - I One popular use is the directory structure in many common operating systems. The root of this directory is /usr. (The asterisk next to the name indicates that /usr is itself a directory.) /usr has three children, mark, alex, and bill, which are themselves directories. Thus, /usr contains three directories and no regular files. The filename /usr/mark/book/ch1.r is obtained by following the leftmost child three times. Each / after the first indicates an edge; the result is the full pathname. Izmir University of Economics

Tree Traversals with an Application - II Suppose we would like to list the names of all of the files in the directory. Our output format will be that files that are at depth d will have their names indented by d tabs. The strategy below is known as preorder traversal. In a preorder traversal, work at a node is performed before (pre) its children. If there are N file names, then the running time is O(N). void list_directory (DirEntry* D){ listAll ( D, 0 ); } void listAll (DirEntry* D, unsigned int depth){ if ( D != NULL){ printName ( depth, D->name ); if( isDirectory(D)) for each child c of D listAll ( c, depth+1 ); Izmir University of Economics

Tree Traversals with an Application - III Another common method of traversing a tree is the postorder traversal. With it, the work at a node is performed after (post) its children are evaluated. As an example, the same directory structure as before, is represented with the numbers in parentheses (the number of disk blocks taken up by each file). Since the directories are themselves files, they have size too. The running time of calculating the size each node is O(N) again. unsigned int size(DirEntry* D){ unsigned int totalSize = 0; if(D != NULL){ totalSize=sizeOfThisFile(D); if( isDirectory(D) ) for each child c of D totalSize += size( c ); } return( totalSize ); Izmir University of Economics

Izmir University of Economics Binary Trees A binary tree is a tree in which no node can have more than two children. Subtrees TL and TR which could both be possibly empty. The depth of an average binary tree is considerably smaller than N ( actually O( ) ). For a special type of binary tree, namely the binary search tree, the average value of depth is O (log N). Unfortunately it can be as large as N - 1. Izmir University of Economics

Izmir University of Economics Implementation Because a binary tree has at most two children, we can keep direct links to them. The declaration of tree nodes is similar in structure to that for doubly linked lists, in that a node is a structure consisting of the element information plus two pointers (left and right) to other nodes. Trees are generally drawn as circles connected by lines. NULL links are not explicitly drawn. typedef struct tree_node *tree_ptr; struct tree_node { element_type element; tree_ptr left; tree_ptr right; }; typedef tree_ptr TREE; Why? A binary tree with N nodes, has N+1 NULL links. Proof: Ni: #nodes with i children N0+N1+N2=N (1) // #nodes N1+2*N2=N-1 (2) // #edges multiply (1) by 2 and subtract (2) 2*N0+N1=2*N-(N-1)=N+1 Izmir University of Economics

An Example: Expression Trees The leaves of an expression tree are operands, such as constants or variable names, and the other nodes contain operators. Operator nodes might have 1 (unary minus), 2, or more than two children. We can evaluate an expression tree, T, by applying the operator at the root to the values obtained by recursively evaluating the left and right subtrees. Inorder traversal: produce an infix expression by first recursively processing left, then, output the operator at the root, and finally recursively process the right. the left subtree evaluates to a + (b * c) and the right subtree evaluates to ((d *e) + f )*g. The entire tree therefore represents (a + (b*c)) + (((d * e) + f)* g). Izmir University of Economics

Constructing an Expression Tree - I We now give an algorithm to convert a postfix expression into an expression tree. Since we already have an algorithm to convert infix to postfix, we can generate expression trees from the two common types of input. The method we describe strongly resembles the postfix evaluation algorithm of Section 3.2.3. stack=; while (!EndOf(expression)){ read(ch); if (isOperand(ch)) push(MakeNode(ch, NULL, NULL), stack); else if (isOperator(ch)){ T1=topAndPop(stack); T2=topAndPop(stack); push(MakeNode(ch, T2, T1), stack); } MakeNode(data,left,right) creates a struct tree_node and returns a pointer to it Izmir University of Economics

Constructing an Expression Tree - II As an example, suppose the input is: a b + c d e + * * a b + c d e + * * a b + c d e + * * a b + c d e + * * a b + c d e + * * Izmir University of Economics

Constructing an Expression Tree - III a b + c d e + * * a b + c d e + * * Izmir University of Economics