CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

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.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
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 A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Compiled by: Dr. Mohammad Omar Alhawarat
Data Structures TREES.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
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.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
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.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
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.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
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.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
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.
CSE 373 Data Structures Lecture 7
CS 201 Data Structures and Algorithms
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.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
CMSC 341 Introduction to Trees.
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees.
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.
CS210- Lecture 9 June 20, 2005 Announcements
Binary Trees.
Binary Trees, Binary Search Trees
Trees.
Presentation transcript:

CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees

Today – Trees Tree basics Tree implementation Induction and trees Tree traversal Tree algorithm analysis Tree application: file system

Terminology

Rooted Tree Recursive definition. A tree consists of a distinguished node r, called the root, and zero or more nonempty (sub)trees T 1, T 2, …, T k, each of whose roots are connected by a directed edge from r. Parent and child. The root r is the parent of each subtree root.

Tree Definition of leaves? Which nodes are leaves? Definition of siblings? Which nodes are siblings? Grandparent and grandchild?

Path and Length A path from node n 1 to node n k is defined as a sequence of nodes n 1, n 2, …, n k such that n i is the parent of n i+1, for 1≤i<k. The length of this path is the # of edges on the path, namely k-1. Is there a path of length zero? There is exactly one path from the root to each node.

Depth and Height The depth of node n i is the length of the unique path from the root to n i. What is the depth of the root? The height of n i is the length of the longest path from n i to a leaf. What is the height of a leaf? The height of a tree is equal to the height of its root. The depth of a tree is equal to the depth of the deepest leaf. How is the height of a tree related to its depth?

Ancestor and Descendant If there is a path from n i to n k, then n i is an ancestor of n k and n k is a descendant of n i. If n i ≠ n k, then n i is a proper ancestor of n k and n k is a proper descendant of n i.

Implementation of Trees Can you suggest an obvious way to implement a tree? Have in each node, besides its data, a link to each child of the node. Why is this approach possibly very inefficient? The # of children per node may vary greatly and is not known in advance. Is there a better solution?

Solution

Induction and Trees

Check Your Answer m = 5, i = ?, l = ?

Check Your Answer m = 2, i = ?, l = ?

Tree Traversals

Time Analysis of Tree Algorithm

Application One popular use of tree is the directory structure in operating systems, including UNIX and DOS.

List Directory

Time Analysis Line 1 is executed exactly once per node, since each name is output once. It follows that line 2 must also be executed once per node. Line 4 can be executed at most once for each child of each node. But the number of children is exactly one less than the number of nodes. The “for” loop iterates once per iteration of line 4, plus each time the loop ends. Thus, if there are N file names to be output, then the running time is O(N).

Traversal Strategy What is the traversal strategy? In preorder traversal, work in a node is performed before (pre) its children are processed.

Size of Each Directory Suppose we know the number of disk blocks taken by each file. How can we find the size of each directory?

Postorder Traversal In a postorder traversal, the work at a node is performed after (post) its children are evaluated.