Trees A non-linear implementation for collection classes.

Slides:



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

Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
© 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.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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 (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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Data Structures TREES.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
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.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 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.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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:
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
Trees Saurav Karmakar
Chapter 12 Abstract Data Type.
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.
Data Structures and Design in Java © Rick Mercer
Fundamentals of Programming II Introduction to Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees.
Trees Definitions Implementation Traversals K-ary Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CE 221 Data Structures and Algorithms
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Trees.
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
Trees.
Presentation transcript:

Trees A non-linear implementation for collection classes

Linear structures Arrays and linked lists are linear: Obvious first and last elements Obvious order for traversal

Non-linear structures Other organizations are possible, (e.g., file organization on disk is a tree) May be more efficient No obvious first and last elements No obvious order for traversal c: \drivers \documents and settings \program files \adobe \canon \google

Tree as graph Connected graph with n nodes and n-1 edges -> no cycles

Rooted tree – any root -directed edges -one node is root -all nodes accessible from root -root has indegree 0 -leaf nodes have outdegree 0 -branch nodes have in and out edges

Relative terminology Relative to a node Parent node Ancestors Child node Descendents Sibling node

Measuring location by path length Depth (from root) 2 Height (above deepest descendent) 2

Recursive definition of list Empty list (0 nodes) OR Head node and 0 or 1 non-empty sublist

Recursive definition of list Empty list (0 nodes) OR Head node and 0 or 1 non-empty sublist

Recursive definition of tree Empty tree (0 nodes) OR Root node and 0 or more non-empty subtrees (child node and its descendents)

Recursive definition of tree

Tree application – file system Directory structure is tree rooted at / (Windows explorer)

A Unix directory Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

The Unix directory with file counts Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley Task: count total files (in parentheses at each node)

The Unix directory with file counts Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley Task: count total files (in parentheses at each node) recursively

Number of files in subtrees Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Number of files in subtrees Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Traversal: how to organize activity on all nodes in a collection? List – process sequentially by 1.iteration 2.recursion

Tree – no obvious order to process ? Traversal: how to organize activity on all nodes in a collection?

Traversal of trees Problem: how to visit all nodes; what order to visit: Many traversals for different purposes: Breadth first Depth first Preorder, postorder (recursive) …more

Preorder Traversal Recursive definition: Visit root before children 1.Visit root 2.Visit subtrees (left to right)

Preorder Traversal Recursive definition: Visit root before children 1.Visit root 2.Visit subtrees (left to right)

Recursive definition of tree

Postorder Traversal Recursive definition: Visit root after children 1.Visit subtrees (left to right) 2. Visit root

Postorder Traversal Recursive definition: Visit root after children 1.Visit subtrees (left to right) 2. Visit root

Traversal examples W RS A TFC OX D I UP B E

Implementing trees List –Node – data & reference Tree –Node – data & references class ListNode {int data; ListNode next; } class TreeNode {int data; TreeNode child1, child2, child3; }

Implementing trees Problem of representing a node: How many edges to allow for? e.g., Class TreeNode { int data; TreeNode child1, child2, child3; }

Implementing trees Problem of representing a node: Three solutions: 1.allow lots of child edge references 2.restrict tree structure to two child edges 3.use first child/next sibling representation

First child/next sibling Class TreeNode { int data; TreeNode child, sibling; } Problem: Makes path to most nodes longer

Binary Trees result – for many purposes, two references are sufficient  binary trees with -left child -right child class BNode { int data; BNode left, right; }