390 likes | 503 Views
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.
E N D
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 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
Introduction There are two general forms of trees: • Binary Trees • General How do we use trees?
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.
Family Trees Piper ------- Amber | X | Paige Lee / | | \ Martha Boyce Madelyn Earl
Organizational Trees President / | \ Vice Pres Vice Pres Vice Pres of X of Y of Z | | | ManagerA ManagerB ManagerC
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
Trees • So, what exactly is a tree?
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.
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.
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.
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.
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.
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.
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.
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
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
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.
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.
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.
Fact • The height of a binary tree with n nodes that is either complete or full is (floor of)(log2n).
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.
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)
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
Preorder Traversal (Root, Left, Right) • order: ??
Preorder Traversal (Root, Left, Right) • order: 1,2,4,8,9,5,10,11,3,6,12,13,7,14,15
Inorder Traversal (Left, Root, Right) • order: ??
Inorder Traversal (Left, Root, Right) • order: 8,4,9,2,10,5,11,1,12,6,13,3,14,7,15
Postorder Traversal (Left, Right, Root) • order: ??
Postorder Traversal (Left, Right, Root) • order: 8,9,4,10,11,5,2,12,13,6,14,15,7,3,1
Level Order Traversal Visit in order of level, start at root • Order??
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
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.
General Tree Traversals • Level-order • Preorder • Postorder • Inorder not well defined.