COSC 2011 Section N. Trees: Terminology and Basic Properties

Similar documents
COMP26120: Algorithms and Imperative Programming. Lecture 1 Trees

csci 210: Data Structures Trees

interface Position<E> { E getelement() throws IllegalStateExcept..; }

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

Trees. CSE 373 Data Structures

Binary Trees

March 20/2003 Jayakanth Srinivasan,

Chapter 20: Binary Trees

Binary Trees, Binary Search Trees

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

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

Trees. Tree Structure Binary Tree Tree Traversals

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

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

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

CE 221 Data Structures and Algorithms

CSC148 Week 6. Larry Zhang

TREES. Trees - Introduction

Programming II (CS300)

Data Structures Lecture 6

Why Do We Need Trees?

Trees. Trees. CSE 2011 Winter 2007

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

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

Trees. Eric McCreath

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

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

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

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

Trees. Truong Tuan Anh CSE-HCMUT

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

Binary Trees. Height 1

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

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

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

Advanced Tree Data Structures

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

INF2220: algorithms and data structures Series 1

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

CSI33 Data Structures

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

Recursive Data Structures and Grammars

Trees and Tree Traversal

Trees: examples (Family trees)

Binary Trees Fall 2018 Margaret Reid-Miller

CSC148H Week 8. Sadia Sharmin. July 12, /29

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

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

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

Introduction to Computers and Programming. Concept Question

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 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

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

INF2220: algorithms and data structures Series 1

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

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

Algorithms and Data Structures

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

Chapter. Basic Data Structures. Contents. 2.1 StacksandQueues Lists Trees Exercises... 84

CSE 373 APRIL 17 TH TREE BALANCE AND AVL


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

CSI33 Data Structures

Topic 14. The BinaryTree ADT

CS350: Data Structures Tree Traversal

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

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

A set of nodes (or vertices) with a single starting point

Trees Algorhyme by Radia Perlman

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Friday Four Square! 4:15PM, Outside Gates

Lecture 26. Introduction to Trees. Trees

Binary trees. Binary trees. Binary trees

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

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

Data Structures and Algorithms

Binary search trees (BST) Binary search trees (BST)

Garbage Collection: recycling unused memory

Successor/Predecessor Rules in Binary Trees

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

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

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

BBM 201 Data structures

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

tree nonlinear Examples

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

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

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

A Hierarchical Structure. Lecture11: Trees I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

Data Structures and Algorithms

Programming II (CS300)

A Hierarchical Structure. Lecture11: Tree I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

Principles of Computer Science

Transcription:

COSC 2011 Tuesday, March 27 2001 Overview Trees and Binary Trees Quick review of definitions and examples Tree Algorithms Depth, Height Tree and Binary Tree Traversals Preorder, postorder, inorder Binary Search Tree 2001-03-27 1 Trees: Terminology and Basic Properties Definitions (continued) Ancestor: Either the node itself or an ancestor of the parent of the node. Descendant: A node v is a descendant of a node u if u is an ancestor of v Descendant: A node v is a descendant of a node u if u is an ancestor of v 2 Trees: Terminology and Basic Properties (continued) Subtree: The subtree of tree T rooted at a node v is the tree consisting of all descendants of v in T (including v itself). Ordered Tree: A linear ordering defined for the children of each node. We can identify the children as being the first, second, third etc. Note the Recursive Definitions for Ancestor, Descendant and Subtree! 3 Trees: Examples 4 1

Trees: Another Example Trees: Terminology Internal nodes: directories External nodes: regular files. 5 6 Trees: Binary Trees (1) 7 Binary Tree Ordered tree. Each node has a maximum of two children. Definitions: Proper Binary tree: Each node has either zero or two children.every internal node has exactly two children. Left or Right Child Each child of a node is labeled left or right child. Left child comes before the right child. 8 2

Trees: Binary Trees (1) Left and Right Subtree The subtree rooted at a left or right child of an internal node v. Trees: Binary Trees (2) Recursive Definition: 9 10 Tree Algorithms: (1) Assumptions: Accessor methods root() & parent(v) take O(1) time. Query methods isinternal(v) isexternal(v) & isroot(v) take O(1) time. Accessor method children(v) takes O(c v ) time where c v is the number of children of v. 11 12 3

Tree Algorithms: (2) Assumptions: Generic methods swapelements(v, w) & repalceelements(v, e) take O(1) time. Generic methods elements() & positions(v), which return Iterators, take O(n) time. Iterator methods take O(1) time. Tree Algorithms: (3) Depth of a node v in a Tree: The number of ancestors of the node excluding v itself. Recursive Definition: If v is the root, depth = 0. Otherwise, depth of v is one plus the depth of the parent of v. 13 14 Tree Algorithms: (4) Recursive Algorithm to compute Depth of node v: Calls itself recursively on the parent of v, and adds 1 to the return value. Algorithm depth(t, v): if T.isRoot(v) then else return 0 return 1+depth(T, T.parent(v)) 15 Tree Algorithms: (5) Height of a node v in a Tree: Recursive Definition: If v is an external node, height = 0. Otherwise, height of v is one plus the max. height of a child of v. Height of a tree is the height of the root. Height of a tree equals the max. depth of an external node of the tree. 16 4

Tree Algorithms: (6) Algorithm height(t): h = 0 for each v T.positions() do if T.isExternal(v) then h = max(h, depth(t, v)) return h Not very efficient! Worst case running time is O(n 2 ). Tree Algorithms: (7) More Efficient Algorithm: Uses recursive defn. of height. Running Time is O(n) Algorithm height2(t, v): if T.isExternal(v) then return 0 else h = 0 for each w T.children(v) do h = max(h, height2(t, w)) return 1 +h 17 18 Tree Traversals: (1) A traversal of a tree T: Systematic way of accessing or visiting all nodes of T. Specific action associated with visit to a node depends on the application - could be anything! Different Types of Traversals Available: Tree Traversals: (2) Preorder Traversal: Root of the tree is visited first. Subtrees rooted at the root s children are then visited recursively. Differ in the way the nodes are visited. 19 20 5

Tree Traversals: (3) Useful for: Producing a linear ordering of the nodes of a tree where parents must always come before children. Tree Traversals: (4) Example: Document tree if external nodes are removed, traversal examines Table of Contents. Efficient way to access all the nodes of a tree: Assume visiting a node takes O(1) time. At each node O(1+c v ) where c v is # of children of v. Running Time is O(n). 21 22 Tree Traversals: (5) Tree Traversals: (6) Postorder Traversal: Opposite of the preorder traversal. Recursively traverses the subtrees rooted at the children of the root first, then visits the root. Will visit a node v after it has visited all other nodes in the subtree rooted at v. Useful For: Solving problems where we wish to compute some property for each node v but computing that property, requires we have already computed the property for the children of v. 23 24 6

Tree Traversals: (7) Example: File System Tree Compute the disk space used by a directory. Tree Traversals: (8) Preorder and Postorder are common ways to traverse a tree, but other traversals are available: Can visit all nodes at depth d before going to depth d+1. Use a queue! Don t necessarily need to use recursion! Preorder and postorder can be done iteratively with a stack. 25 26 Binary Tree Properties (1) Level: Set of all nodes of a tree T at the same depth d, as the level d of T. Binary Tree Properties (2) Level 0 has one node the root, level 1 at most 2 nodes, level 2 at most 4 nodes. Level d has at most 2 d nodes. Maximum number of nodes on the levels of a binary tree grows exponentially as we go down the tree. 27 28 7

Binary Tree Properties (3) Let T be a proper (non-empty) binary tree with n nodes, let h be the height of T, then: 1. Number of external nodes in T is at least h+1 and at most 2 h. 2. The number of internal nodes in T is at least h and at most 2 h -1. 3. Total number of nodes in T is at least 2h+1 and at most 2 h+1 1. 4. Height of T is at least log(n+1)-1 and at most (n-1)/2. That is, log(n+1) 1 h (n-1)/2 5. Number of external nodes = 1+ number of internal nodes. 29 8