1 / 39

CSC 242 – Program Design II

CSC 242 – Program Design II. Trees. Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC (Portions of these notes from Weiss book and University of Washington Website). Introduction.

Télécharger la présentation

CSC 242 – Program Design II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 242 – Program Design II Trees Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC (Portions of these notes from Weiss book and University of Washington Website)

  2. Introduction As a plant, trees are pretty well known. As a way to organize data, they are also, surprisingly, well known. • Family Tree • Tournament Players Chart

  3. Introduction There are two general forms of trees: • Binary Trees • General How do we use trees?

  4. Concepts What if we need to categorize our data into groups and subgroups, not in linear order? A queue or stack is not set up for this. This hierarchical classification appears often in problems and we should be able to represent it.

  5. Family Trees Piper ------- Amber | X | Paige Lee / | | \ Martha Boyce Madelyn Earl

  6. Organizational Trees President / | \ Vice Pres Vice Pres Vice Pres of X of Y of Z | | | ManagerA ManagerB ManagerC

  7. Other CS Uses for Trees • Folders/files on a computer • Each folder/file is a node, subfolders are children. • AI: decision trees / game trees • Compilers: parse tree a = (b + c) * d; • Expression Trees

  8. Trees • So, what exactly is a tree?

  9. Terminology Tree – set of nodes connected by edges that indicate the relationships among the nodes. Nodes are arranged in levels that indicate the node’s hierarchy. The top level is a single node called the root.

  10. Terminology • Trees have a starting node called the root; all other nodes are reachable from the root by the edges between them. • GOAL: Use a tree to build a collection that has O(log n) time for many useful operations.

  11. Terminology • Nodes at each successive level of a tree are the children of the nodes at the previous level. • A node that has children is the parent of those children • Nodes with the same parent are called siblings. • Nodes can also be ancestors and/or descendants.

  12. Terminology

  13. Terminology • Nodes with no children are called leaf nodes. • Nodes with children are called interior nodes or non-leaf nodes. • Any node and its descendants form a subtree of the original tree.

  14. r T2 T3 T1 Visualizing Trees • every node links to a set of subtrees • root of each subtree is a child of root r. • r is the parent of each subtree.

  15. Terminology • The height of the tree is the number of levels of the tree. • The height of a node is the length of the path from the node to the deepest leaf. • We can reach any node in a tree by following a path that begins at the root and goes from node to node along the edges that join them. The path between the root and any other node is unique.

  16. Terminology • The length of a path is the number of edges that compose it (which is one less than the number of nodes in the path). • The depth of a node is the length of the path from the root to the node. • The depth of any node is one more than the depth of its parent.

  17. Paths • path: a sequence of nodes n1, n2, … , nk such that ni is the parent of ni+1 for 1  i < k • in other words, a sequence of hops to get from one node to another • the length of a path is the number of edges in the path, or 1 less than the number of nodes in it

  18. Depth and Height • depth or level: length of the path from root to the current node (depth of root = 0) • height: length of the longest path from root to any leaf • empty (null) tree's height: -1 • single-element tree's height: 0 • tree with a root with children: 1

  19. Example

  20. Example

  21. Trees • General Trees: Each node in the tree can have an arbitrary number of children • n-ary tree: Each node has no more than “n” children • Binary tree: Each node has no more than 2 children.

  22. General Tree

  23. Binary Trees • Each node has at most two children. • Children are called the left child and the right child. • Root of a binary tree has two subtrees – the left subtree and the right subtree. • Every subtree in a binary tree is also a binary tree.

  24. 1 2 3 4 5 6 7 Binary Trees • When a binary tree of height h has all of its leaves at level h and every non-leaf has exactly two children, the tree is said to be full. • If all levels of the binary tree contain as many nodes as possible and the nodes on the last level are filled from left to right, the tree is complete.

  25. Fact • The height of a binary tree with n nodes that is either complete or full is (floor of)(log2n).

  26. Traversals • Aka “iteration” • We must “visit” or process each node exactly once. • Order in which we visit is not unique. We can choose an order suitable to our application. • “Visiting” a node means to “process the data within” that node.

  27. Traversals of a Binary Tree • We know that each subtree of a binary tree is a binary tree. • We can use recursion to visit all the nodes. • To visit all, we must visit the root, visit all nodes in the left subtree (LST) and all nodes in the right subtree (RST)

  28. Common Traversals • Preorder (Root,Left,Right) • Depth-first traversal • Inorder (Left, Root, Right) • Postorder (Left, Right, Root) • Level-order – visits nodes from left to right within each level of the tree, beginning with the root • Breadth-first traversal

  29. Preorder Traversal (Root, Left, Right) • order: ??

  30. Preorder Traversal (Root, Left, Right) • order: 1,2,4,8,9,5,10,11,3,6,12,13,7,14,15

  31. Inorder Traversal (Left, Root, Right) • order: ??

  32. Inorder Traversal (Left, Root, Right) • order: 8,4,9,2,10,5,11,1,12,6,13,3,14,7,15

  33. Postorder Traversal (Left, Right, Root) • order: ??

  34. Postorder Traversal (Left, Right, Root) • order: 8,9,4,10,11,5,2,12,13,6,14,15,7,3,1

  35. Level Order Traversal Visit in order of level, start at root • Order??

  36. Level Order Traversal Visit in order of level, start at root • Order: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

  37. Practice • Do a preorder, inorder, postorder, and level order traversal on the following tree – write the nodes values in the order they would be visited.

  38. Practice

  39. General Tree Traversals • Level-order • Preorder • Postorder • Inorder not well defined.

More Related