trees csc 172 spring 2002 lecture 14. lists we have seen lists: public class node { object data;...
TRANSCRIPT
![Page 1: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/1.jpg)
Trees
CSC 172
SPRING 2002
LECTURE 14
![Page 2: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/2.jpg)
ListsWe have seen lists:public class Node {
Object data;Node next;
}
![Page 3: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/3.jpg)
TreesNow, look at trees:
public class Node {
Object data;
Node left;
Node right;
}
![Page 4: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/4.jpg)
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
![Page 5: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/5.jpg)
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 (m1,m2,…,mk) such that each is the parent of the following
path “from m1 to mk”
Path length = k-1, number of links, not nodes
![Page 6: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/6.jpg)
Example: UNIX File Systems
/
/bin /dev /usr…
/dev/term /dev/sound /dev/tty01.
/usr/anna /usr/jon /usr/ted
![Page 7: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/7.jpg)
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
![Page 8: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/8.jpg)
The children of any given note are often ordered “from the left”
Child c1 is to the left of c2 then all the nodes in the subtree rooted at c1 are “to the left” of those in the subtree rooted at c2
Nodes may have labels, which are values associated with the nodes
![Page 9: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/9.jpg)
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
![Page 10: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/10.jpg)
(x+1)*(x-y+4)
x 1
x y
*
+ -
- 4
![Page 11: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/11.jpg)
General TreesSome trees are binary:
public class Node {
Object data;
Node left;
Node right;
}
![Page 12: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/12.jpg)
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?
![Page 13: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/13.jpg)
LMC-RSLeftmost-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 ith child of node n, you must traverse a list n long
![Page 14: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/14.jpg)
![Page 15: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/15.jpg)
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?
![Page 16: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/16.jpg)
LMC-RS
public class Node {
Object data;
Node l_child;
Node r_sibling;
}
![Page 17: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/17.jpg)
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)
![Page 18: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/18.jpg)
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
![Page 19: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/19.jpg)
(x+1)*(x-y+4)
x 1
x y
*
+ -
- 4
x 1 + x y – 4 - *
![Page 20: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/20.jpg)
Exploring trees
Postorder
visit children, operate on node
Preorder
operate on node, visit children
![Page 21: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/21.jpg)
(x+1)*(x-y+4)
x 1
x y
*
+ -
- 4
* + x 1 - - x y 4
![Page 22: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/22.jpg)
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
![Page 23: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/23.jpg)
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
![Page 24: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/24.jpg)
Induction
T has a root r and one or more sub trees T1, T2,…,Tk
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?
![Page 25: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/25.jpg)
T1
n1 nodes
n1+1
T2
n2 nodes
n2+1
Tk
nk nodes
nk+1
…
nodesnk
ik
1
)1(1
k
ikn knodes
? ? ?
One more nodeOne more Still “k” extra
![Page 26: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/26.jpg)
T1
n1 nodes
n1+1
T2
n2 nodes
n2+1
Tk
nk nodes
nk+1
…
nodesnk
ik
1
)1(1
k
ikn knodes
? ? ?
One more nodeOne more Still “k” extra
How many less?
![Page 27: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/27.jpg)
Example:
S(T): A full binary tree of height h has 2h+1 – 1 nodes
Basis?
Nodes = 1, height == 0, 20+1-1 = 1
Induction?
![Page 28: Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; }](https://reader033.vdocuments.net/reader033/viewer/2022052603/56649ebb5503460f94bc3dd7/html5/thumbnails/28.jpg)
T1
h height2 h+1-1 nodes
nodeshh 1)12()12( 11
Height = h+1
T2
h height2 h+1-1 nodes
nodesh 12 2