Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Info Check out gilgarn.org. QUIZ DO YOU: – Love CSC 171 ? – Want a job? – Like to exert power over others? – Want to improve CSC UR?

Similar presentations


Presentation on theme: "General Info Check out gilgarn.org. QUIZ DO YOU: – Love CSC 171 ? – Want a job? – Like to exert power over others? – Want to improve CSC UR?"— Presentation transcript:

1 General Info Check out gilgarn.org

2 QUIZ DO YOU: – Love CSC 171 ? – Want a job? – Like to exert power over others? – Want to improve CSC education @ UR? DID YOU: – Do well (A or B) in 171? – Think Workshops were helpful? CAN YOU: – Stand looking at me for another semester?

3 IF YOU: Answered “YES” to most of the above questions THEN: – Consider serving as a WORKSHOP LEADER BENEFITS: – FAME, MONEY, POWER – LOOKS GOOD ON YOUR RESUME – DO SOMETHING GOOD FOR HUMANITY INTEREST MEETING: – THURS (3/25) 5:15PM – LAS, basement of Lattimore

4 Trees CSC 172 SPRING 2004 LECTURE 14

5 Lists We have seen lists: public class Node { Object data; Node next; } 

6 Trees Now, look at trees: public class Node { Object data; Node left; Node right; }        

7 Rooted Trees Collection of nodes, one of which is the root Nodes != root have a unique parent node Each non-root can reach the root by following parent links one or more times

8 Definitions If node p is the parent of node c then c is a child of p Leaf : no children Interior node : has children Path : list of nodes (m 1,m 2,…,m k ) such that each is the parent of the following path “from m 1 to m k ” Path length = k-1, number of links, not nodes

9 If there is a path from m to n, then m is an ancestor of n and n is a descendant of m Note m == n is possible Proper ancestors, descendants : m != n Height of a node n is the length of the longest path from n to a leaf Height of a tree is the height of its root Depth of a node is the length of the path from the root to n Subtree rooted at n is all the descendants of n

10 The children of any given note are often ordered “from the left” Child c 1 is to the left of c 2 then all the nodes in the subtree rooted at c 1 are “to the left” of those in the subtree rooted at c 2 Nodes may have labels, which are values associated with the nodes

11 Example: UNIX File Systems / /bin/dev/usr … /dev/term/dev/sound/dev/tty01. /usr/anna/usr/jon/usr/ted

12 Example: Expression Trees Labels are operands or operators Leaves : operands Interior nodes : operators Children are roots of sub-expressions to which the operator is applied

13 (x+1)*(x-y+4) x1x y *+- - 4

14 Recursion on Trees Many algorithms to process trees are designed with a basis (leaves) and induction (interior nodes) Example: If we have an expression tree we can get infix (operator between operands - common) prefix (operator before operands – like function calls) postfix (operator after operands – good for compilers)

15 Expression Tree to Postfix Basis For a leaf, just print the operand Induction: For an interior node apply algorithm to each child from left print the operator

16 (x+1)*(x-y+4) x1x y *+- - 4 x 1 + x y – 4 - *

17 General Trees Some trees are binary: public class Node { Object data; Node left; Node right; }        

18 Some trees are not binary / /bin/dev/usr … /dev/term/dev/sound/dev/tty01. /usr/anna/usr/jon/usr/ted How do we implement such trees?

19 LMC-RS Leftmost-Child, Right-Sibling Tree Representation Each node has a reference to 1. It’s leftmost child 2. It’s right sibling – the node immediately to the right having the same parent Advantage: represents trees without limits or pre- specified number of children Disadvantage: to find the i th child of node n, you must traverse a list n long

20 LMC-RS public class Node { Object data; Node l_child; Node r_sibling; }       

21 Structural Induction Basis = leaves (one-node trees) Induction = interior nodes (trees with => 2 nodes) Assume the statement holds for the subtrees at the children of the root and prove the statement for the whole tree

22 Tree Proof Example Consider a LMC-RS tree S(T): T has one more  reference than it has nodes Basis: T is a single node – 2  references  

23 Induction T has a root r and one or more sub trees T 1, T 2,…,T k BTIH: each of these trees, by itself has one more  than nodes How many nodes all together? How many  references? How many nodes do I add to make one tree? How many  references do we reduce to make one tree?

24 T 1 n 1 nodes n 1 +1  T 2 n 2 nodes n 2 +1  T k n k nodes n k +1  … ?  ?  ?  One more node One more  Still “k” extra 

25 T 1 n 1 nodes n 1 +1  T 2 n 2 nodes n 2 +1  T k n k nodes n k +1  … ? ?  ? One more node One more  Still “k” extra  How many less?

26 Example: Pair Quiz S(T): A full binary tree of height h has (2 h+1 – 1) nodes Basis? Induction?

27 Example: S(T): A full binary tree of height h has 2 h+1 – 1 nodes Basis? Nodes = 1, height == 0, 2 0+1 -1 = 1 Induction?

28 T 1 h height 2 h+1 -1 nodes Height = h+1 T 2 h height 2 h+1 -1 nodes

29 Why Structural Induction? Q: Why do I want you to draw the pictures? A: We know you can do the algebra. As you move on, the difficult part of the proofs is “setting up” the proof, from some situation. Sure, if someone sets it up for you, it’s just an “exercise” to solve. The real value is in being able to reason about a situation so as to set up a proof.

30 Process Real world phenomena or artifact Formal Representation “set up” of proof Rigorous Knowledge Informal reasoning abstraction analysis Formal techniques: induction, algebra

31 Kraft’s Inequality Suppose that a binary tree has leaves {l 1,l 2,..l M } at depths {d 1,d 2,...,d M }, respectively. Prove:

32 How do you set this up?

33 Basis: In a tree with zero nodes, the sum is zero In a tree with one node – One leaf, depth 0

34 T 1 ∑ 1 <=1 T 2 ∑ 2 <=1 Same number of total leaves, all depths increase by 1 So, in new Tree ∑ 1 <=1/2 && ∑ 2 <=1/2


Download ppt "General Info Check out gilgarn.org. QUIZ DO YOU: – Love CSC 171 ? – Want a job? – Like to exert power over others? – Want to improve CSC UR?"

Similar presentations


Ads by Google