The Tree DT lass #14: ntroduction to Trees Software Design (S 340): M. llen, 25 Feb. 15! Trees are collections of objects, related in a branching, ordered fashion! an represent a variety of real-world objects and concepts, like:! Directory/file systems! Ranked organizations! Progress in an search! Structure of nested arithmetic expressions Wednesday, 25 Feb. 2015" Software Design (S 340)" 2" The Tree DT! Like lists, trees are collections of objects of some type! Unlike array-based structures, more like linked collections! tems are usually not directly indexed, but instead are accessed by traversing down the tree from a fixed starting point! Unlike either sort of structure, trees are not linear, but instead have a hierarchical structure:! Tree nodes (vertices) hold data! Tree edges (branches) connect nodes! Like lists, trees have a fixed order of traversal: not first-to-last, but top-down, starting at the top (root) vertex Defining the Tree DT Recursively 1. ny single node r is a tree (the root). 2. f r is a single root node and T 1, T 2,, T k are trees with their own roots q 1, q 2,, q k, then the structure formed by connecting root r to every other root q i is also a tree. q 1 T 1 q 2 T 2 r " q k T k Wednesday, 25 Feb. 2015" Software Design (S 340)" 3" Wednesday, 25 Feb. 2015" Software Design (S 340)" 4" 1
Tree Terminology deg(t) = 3 Root deg(n) = 3 Tree Terminology! Edges are directional, from parent to child, with at most one parent per child! The root is the top node (only one without a parent)! node without any children is called a leaf! ny other node is internal! The degree of a node is the number of its children nternal deg(n) = 2! path is a sequence of nodes n 0, n 1, n k where each n i is the parent of n i+1! The length k of a path is the number of edges in it! The descendants of node n are all the nodes on some path from n to any leaf! The ancestors of node n are all the nodes that are on some path from root to n ncestors of l 0 r n 0 n 1 n 2 path from root to l 6 of length 2 l 0 l 1 l 2 l 3 n 3 l 6! The degree of a tree is the maixmal node-degree Leaves deg(n) = 0! The siblings of node n are all the nodes with same parent pair of siblings l 4 l 5 Descendants of n 2 Wednesday, 25 Feb. 2015" Software Design (S 340)" 5" Wednesday, 25 Feb. 2015" Software Design (S 340)" 6" Tree Terminology! The depth (level) of a node n is the length of the single path from the root down to n! The height of node n is the length of the longest path from n to any leaf! The height of a tree itself is the height of the root height(t) = 2 height(n) = 2 depth(n) = 0 height(n) = 1 depth(n) = 1 height(n) = 0 depth(n) = 2 mplementing Tree Nodes! Typically, we might use a linking structure for nodes class TreeNode<T>! TreeNode<T> left;! TreeNode<T> right;!! One drawback: this only works for trees with nodes that all have degree 2 left! right! " Wednesday, 25 Feb. 2015" Software Design (S 340)" 7" Wednesday, 25 Feb. 2015" Software Design (S 340)" 8" 2
Tree Nodes with Varying Degree! nstead, we could use a more general linking structure, based on child-sibling relations! Using a sibling link allows us to add as many or as few as we need at any level D E F K child! class TreeNode<T>! TreeNode<T> child;! TreeNode<T> sibling;! sibling! D E F K inary Trees! n fact, for many purposes, we will be perfectly happy with trees in which every node has at most two children, and will use basic parent-child pair-linking! Usually, we will draw them without the explicit null nodes at the leaves class inarynode<t>! TreeNode<T> left;! TreeNode<T> right;! Wednesday, 25 Feb. 2015" Software Design (S 340)" 9" Wednesday, 25 Feb. 2015" Software Design (S 340)" 10" Properties of inary Trees! perfect binary tree is one where each node has either 2 or 0 children, and all leaves have the same depth Perfect tree mperfect tree Properties of Perfect inary Trees! perfect binary tree has: 1. 2 L nodes at level L 2. 2 h leaves for height h 3. 2 h+1 1 nodes for height h (equivalently, 2n 1 nodes for n leaves) 4. eight log 2 n for n leaves 2 0 = 1 nodes Total count 2 4 1 = 15 nodes 2 1 = 2 nodes log 2 8 = 3 height 2 2 = 4 nodes 2 3 = 8 leaves 2 3 = 8 nodes Wednesday, 25 Feb. 2015" Software Design (S 340)" 11" Wednesday, 25 Feb. 2015" Software Design (S 340)" 12" 3
Properties of inary Trees! complete binary tree is perfect except for (possibly) the deepest level! f the deepest level is not complete, it must be filled in left-to-right order omplete tree ncomplete tree Properties of omplete inary Trees! complete binary tree has: 1. t most 2 L nodes at level L 2. t least 2 h nodes for height h 3. t most 2 h+1-1 nodes for height h 2 0 = 1 nodes 8 2 3 nodes 8 2 4-1 nodes 2 1 = 2 nodes 2 2 = 4 nodes 1 2 3 nodes Wednesday, 25 Feb. 2015" Software Design (S 340)" 13" Wednesday, 25 Feb. 2015" Software Design (S 340)" 14" Tree Traversal! We can list binary tree nodes using four basic orderings: 1. level-order: list nodes left-to-right on each level in turn, from the top downwards 2. pre-order: list parent first, then repeat recursively for each child, left-to-right 3. post-order: traverse children left-to-right recursively; list parent after all children have been listed 4. in-order: list left sub-tree recursively, then root, then list right sub-tree recursively Traversal Examples! Level-order: L-to-R, level by level.! F,,,, D,,, E,! Pre-order: Parent, then recursively L-to-R on children.! F,,, D,, E,,,! Post-order: Recursively L-to-R on children, then parent.!,, E, D,,,,, F! n-order: Left subtree, then root, then right subtree.!,,, D, E, F,,, Wednesday, 25 Feb. 2015" Software Design (S 340)" 15" Wednesday, 25 Feb. 2015" Software Design (S 340)" 16" 4
n Example: Expression Trees! n expression tree is a binary tree corresponding to some mathematical expression! Each internal node is an arithmetical operator! Each leaf is a value or a declared variable! The tree can be evaluated by traversing the elements and performing computation as the elements are traversed Expression Tree! We can use a stack of trees and an expression in postfix form to create such a tree! We can traverse the tree to generate or evaluate the represented expression! n-order: 3 + 1 * 4 / 2 * 5-5!! Pre-order: * + 3 1 - * / 4 2 5 5! Post-order: 3 1 + 4 2 / 5 * 5 - *! Wednesday, 25 Feb. 2015" Software Design (S 340)" 17" Wednesday, 25 Feb. 2015" Software Design (S 340)" 18" This Week & Next! Topic: Stacks, Queues, ntroduction to Trees! Read: Start reading Text, chapter 04! omework 02: Today, 5:00 PM! Midterm 01: Wednesday, 02 March (in class)! overs all material up to (and including) Stacks & Queues! hapters 1, 2, 3 of text! Lectures 1 9! Office ours: Wing 210! Tuesday: 10:00 11:30 M & 4:00 5:30 PM! Thursday: 4:00 5:30 PM only (M trip) Wednesday, 25 Feb. 2015" Software Design (S 340)" 19" 5