Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees A non-linear implementation for collection classes.

Similar presentations


Presentation on theme: "Trees A non-linear implementation for collection classes."— Presentation transcript:

1 Trees A non-linear implementation for collection classes

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

3 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

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

5 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

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

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

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

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

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

11 Recursive definition of tree

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

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

14 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)

15 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

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

17 Number of files in subtrees Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley 17 1112 3 4 8 41 52

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

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

20 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

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

22 Preorder Traversal 1 25 9 3611 810 7 4 Recursive definition: Visit root before children 1.Visit root 2.Visit subtrees (left to right)

23 Recursive definition of tree

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

25 Postorder Traversal 11 210 5 149 67 8 3 Recursive definition: Visit root after children 1.Visit subtrees (left to right) 2. Visit root

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

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

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

29 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

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

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


Download ppt "Trees A non-linear implementation for collection classes."

Similar presentations


Ads by Google