Trees. Trees. CSE 2011 Winter 2007

Similar documents
OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2)

Trees. Make Money Fast! Stock Fraud. Bank Robbery. Ponzi Scheme. Trees Goodrich, Tamassia

Trees. 9/21/2007 8:11 AM Trees 1

Data Structures. Trees, Binary trees & Binary Search Trees

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme Goodrich, Tamassia. Trees 1

Trees and Binary Trees

Data Structures and Algorithms. Trees

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 02/06/2006 Trees 1

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme. Trees Goodrich, Tamassia

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 1/18/2005 4:17 AM Trees 1

Stock Fraud. Nancy Amato. Parasol Lab, Dept. CSE, Texas A&M University

Trees 3/19/14. Mammal. Dog Pig Cat

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Binary Trees, Binary Search Trees

Data Structures Lecture 6

Outline. Definitions Traversing trees Binary trees

Trees. Data structures and Algorithms. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme

What is a Tree. Trees. Tree ADT ( 6.1.2) Tree Terminology. subtree. In computer science, a tree is an abstract model

Trees. (Trees) Data Structures and Programming Spring / 28

CE 221 Data Structures and Algorithms

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Ch02 Data Structures

TREES. Trees - Introduction

Ch02 Data Structures

Trees, Binary Trees, and Binary Search Trees

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

March 20/2003 Jayakanth Srinivasan,

CSI33 Data Structures

Programming II (CS300)

Why Do We Need Trees?

Binary Trees

Trees. CSE 373 Data Structures

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

CSI33 Data Structures

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Binary trees. Binary trees. Binary trees

Chapter 20: Binary Trees

DataStruct 7. Trees. Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011.

CSCI2100B Data Structures Trees

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Trees. Truong Tuan Anh CSE-HCMUT

CSE 230 Intermediate Programming in C and C++ Binary Tree

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

CSC148 Week 6. Larry Zhang

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Introduction to Computers and Programming. Concept Question

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1


Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

COSC 2011 Section N. Trees: Terminology and Basic Properties

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

Data Structures and Algorithms for Engineers

BBM 201 Data structures

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Associate Professor Dr. Raed Ibraheem Hamed

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Garbage Collection: recycling unused memory

Friday Four Square! 4:15PM, Outside Gates

Analysis of Algorithms

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

CS 171: Introduction to Computer Science II. Binary Search Trees

DATA STRUCTURES USING C

Lecture 26. Introduction to Trees. Trees

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Binary Trees Fall 2018 Margaret Reid-Miller

(2,4) Trees. 2/22/2006 (2,4) Trees 1

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

CSE 214 Computer Science II Introduction to Tree

Trees. Tree Structure Binary Tree Tree Traversals

Tree Structures. A hierarchical data structure whose point of entry is the root node

Trees. A tree is a directed graph with the property

Data Structure - Binary Tree 1 -

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Topic 14. The BinaryTree ADT

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Algorithms and Data Structures

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node


tree nonlinear Examples

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Transcription:

Trees CSE 2011 Winter 2007 2/5/2007 10:00 PM 1 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)? Trees Basic concepts Tree traversal Binary trees Binary search trees and operations 2 1

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 relation Applications: Organization charts File systems Programming environments US Sales International Computers R Us Manufacturing Laptops Europe Asia Canada Desktops R&D 3 Recursive Definition A tree is a collection of nodes. The collection can be empty. Otherwise, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T 1, T 2,..., T k, each of whose roots are connected by a directed edge from r 4 2

Terminologies Child and Parent Every node except the root has one parent A node can have zero or more children Leaves Leaves are nodes with no children Sibling Nodes with the same parent Ancestor and descendant If there is a path from n1 to n2 then n1 is an ancestor of n2 n2 is a descendant of n1 Proper ancestor and proper descendant if n1 n2 5 Terminologies (2) Path a sequence of edges Length of a path number of edges on the path Depth of a node length of the unique path from the root to that node 6 3

Terminologies (3) Height of a node length of the longest path from that node to a leaf all leaves are at height 0 The height of a tree = the height of the root = the depth of the deepest leaf 7 Example: UNIX Directory 8 4

Example: Expression Trees Leaves are operands (constants or variables) The internal nodes contain operators 9 Implementation of Trees class TreeNode { Object element; TreeNode firstchild; TreeNode nextsibling; TreeNode parent; } 10 5

A NULL B C D E F G H I J K L M N P Q 11 Preorder Traversal A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Application: print a structured document Algorithm preorder(v) visit(v) for each child w of v preorder (w) 1 Make Money Fast! 2 1. Motivations 2. Methods References 5 9 3 4 1.1 Greed 1.2 Avidity 6 7 8 2.1 Stock Fraud 2.2 Ponzi Scheme 2.3 Bank Robbery 12 6

An Example 13 Postorder Traversal In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1 h1c.doc 3K 3 homeworks/ DDR.java 10K cs16/ Algorithm postorder(v) for each child w of v postorder (w) visit(v) programs/ 2 4 5 6 h1nc.doc 2K 7 Stocks.java 25K Robot.java 20K 8 todo.txt 1K 14 7

Applications Either preorder traversal or postorder traversal can be used when the order of computation is not important. Example: printing the contents of a tree (in any order) Preorder traversal is required when we must perform a computation for each node before performing any computations for its descendents. Postorder traversal is needed when the computation for a node v requires the computations for v s children to be done first. Example: Given a file system, compute the disk space used by a directory. 15 Example: Computing Disk Space 16 8

Example: UNIX Directory Traversal 17 Example: Unix Directory Traversal Preorder Postorder 18 9

Binary Trees 19 Binary Trees A tree in which each node can have at most two children. Generic binary tree The depth of an average binary tree is considerably smaller than N. In the worst case, the depth can be as large as N 1. Worst-case binary tree 20 10

Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Yes Want a fast meal? No How about coffee? On expense account? Yes No Yes No Starbucks Spike s Al Forno Café Paragon 21 Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2 (a 1) + (3 b)) + 2 a 1 3 b 22 11

Implementation of Binary Trees class BinaryNode { Object BinaryNode BinaryNode BinaryNode } element left; right; parent; 23 Implementation of Binary Trees (2) A node is represented by an object storing Element Parent node Left child node Right child node B B A D A D C E C E 24 12

Binary Tree Traversal Preorder (node, left, right) Postorder (left, right, node) Inorder (left, node, right) 25 Preorder Traversal: Example Preorder traversal node, left, right prefix expression + + a * b c * + * d e f g 26 13

Postorder Traversal: Example Postorder traversal left, right, node postfix expression a b c * + d e * f + g * + 27 Inorder Traversal: Example Inorder traversal left, node, right infix expression a + b * c + d * e + f * g 28 14

Pseudo-code for Binary Tree Traversal 29 Representing General Trees with Binary Trees Procedure: Start from the root The first child of a node v becomes the left child of v The other children of v form a chain of right children rooted at the left child B A C D E F G H Homework: write a program that converts a general tree to a binary tree I J K 30 15

Another Example tree T binary tree representing T 31 One More Example 32 16

Properties of Proper Binary Trees A binary trees is proper if each node has either zero or two children. Level: depth The root is at level 0 Level d has at most 2 d nodes Notation: n number of nodes e number of external (leaf) nodes i number of internal nodes h height n = e + i e = i + 1 h+1 e 2 h n = 2e 1 h i 2 h 1 2h+1 n 2 h+1 1 log 2 e h e 1 log 2 (i + 1) h i log 2 (n + 1) 1 h (n 1)/2 33 Properties of (General) Binary Trees Level: depth The root is at level 0 Level d has at most 2 d nodes Notation: n number of nodes e number of external (leaf) nodes i number of internal nodes h height h+1 n 2 h+1 1 1 e 2 h h i 2 h 1 log 2 (n + 1) 1 h n 1 34 17

Next time Binary Search Trees 35 18