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; }