CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }
-
Upload
osborne-daniel -
Category
Documents
-
view
219 -
download
0
Transcript of CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }
![Page 1: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/1.jpg)
CSC 172 CSC 172 DATA STRUCTURESDATA STRUCTURES
![Page 2: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/2.jpg)
LISTSLISTSWe have seen lists:public class Node {Object data;Node next;}
![Page 3: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/3.jpg)
TREESTREESNow, look at trees:
public class Node {Object data;Node left;Node right;}
![Page 4: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/4.jpg)
ROOTED TREESROOTED TREES
Collection of nodes, one of which is the rootNodes != root have a unique parent nodeEach non-root can reach the root by
following parent links one or more times
![Page 5: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/5.jpg)
DEFINITIONSDEFINITIONS
If node p is the parent of node cthen c is a child of pLeaf : no childrenInterior 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: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/6.jpg)
If there is a path from m to n, then m is an ancestor of n and n is a descendant of mNote m == n is possibleProper ancestors, descendants : m != n
Height of a node n is the length of the longest path from n to a leafHeight 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 7: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/7.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 or data associated with the nodes
![Page 8: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/8.jpg)
Example: UNIX File SystemsExample: UNIX File Systems
![Page 9: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/9.jpg)
Example: UNIX File SystemsExample: UNIX File Systems
/
/bin /dev /usr…
/dev/term /dev/sound /dev/tty01.
/usr/anna /usr/jon /usr/ted
![Page 10: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/10.jpg)
Example: Expression TreesExample: Expression Trees
Labels are operands or operatorsLeaves : operandsInterior nodes : operatorsChildren are roots of sub-expressions to which the
operator is applied
![Page 11: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/11.jpg)
(x+1)*(x-y+4)(x+1)*(x-y+4)
x 1
x y
*
+ -
- 4
![Page 12: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/12.jpg)
Example Musical Time Span Example Musical Time Span ReductionReduction
![Page 13: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/13.jpg)
Recursion on TreesRecursion 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 getinfix
(operator between operands - common)
prefix (operator before operands – like function calls)
postfix (operator after operands – good for compilers)
![Page 14: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/14.jpg)
Expression Tree to PostfixExpression Tree to Postfix
BasisFor a leaf, just print the operand
Induction:For an interior node
apply algorithm to each child from leftprint the operator
![Page 15: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/15.jpg)
(x+1)*(x-y+4)(x+1)*(x-y+4)
x 1
x y
*
+ -
- 4
x 1 + x y – 4 - *
![Page 16: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/16.jpg)
TREE DATA STRUCTURESTREE DATA STRUCTURES
![Page 17: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/17.jpg)
BINARY TREESBINARY TREESSome trees are binary:public class Node {Object data;Node left;Node right;}
![Page 18: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/18.jpg)
Some trees are not binarySome 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 19: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/19.jpg)
LMC-RSLMC-RSLeftmost-Child, Right-Sibling Tree RepresentationEach node has a reference to1. It’s leftmost child2. It’s right sibling – the node immediately to the
right having the same parentAdvantage: represents trees without limits or pre-
specified number of childrenDisadvantage: to find the ith child of node n, you
must traverse a list n long
![Page 20: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/20.jpg)
LMC-RSLMC-RS
public class Node {Object data;Node l_child;Node r_sibling;}
![Page 21: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/21.jpg)
Proving Tree PropertiesProving Tree PropertiesStructural InductionStructural 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 22: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/22.jpg)
Tree Proof ExampleTree Proof Example
Consider a LMC-RS treeS(T): T has one more reference than it has
nodesBasis: T is a single node – 2 references
![Page 23: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/23.jpg)
InductionInduction
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 24: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/24.jpg)
T1
n1 nodes
n1+1
T2
n2 nodes
n2+1
Tk
nk nodes
nk+1 …
∑i=1
k
nk=nodes ∑i=1
k
nk1 =φ nodes k= φ
? ? ?
One more nodeOne more Still “k” extra
![Page 25: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/25.jpg)
T1
n1 nodes
n1+1
T2
n2 nodes
n2+1
Tk
nk nodes
nk+1 …
∑i=1
k
nk=nodes ∑i=1
k
nk1 =φ nodes k= φ
? ? ?
One more nodeOne more Still “k” extra
How many less?
![Page 26: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/26.jpg)
Example: Pair QuizExample: Pair Quiz
S(T): A full binary tree of height h has (2h+1 – 1) nodes
Basis?Induction?
![Page 27: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/27.jpg)
Example:Example:
S(T): A full binary tree of height h has 2h+1 – 1 nodes
Basis?Nodes = 1, height == 0, 20+1-1 = 1Induction?
![Page 28: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/28.jpg)
T1
h height2 h+1-1 nodes
2h1−1 2h1−1 1=nodes
Height = h+1
T2
h height2 h+1-1 nodes
2h2−1=nodes
![Page 29: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/29.jpg)
A tree viewed recursivelyA tree viewed recursively
![Page 30: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/30.jpg)
Binary TreeBinary Tree
public class BinTree {Object data;BinTree leftChild;BinTree rightChild;
….// accessor methods
}
![Page 31: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/31.jpg)
Exercise:Size of a treeExercise:Size of a treeRecursive viewST = SL + SR + 1
![Page 32: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/32.jpg)
Size of a Tree QuizSize of a Tree Quiz
public static int size (BinaryNode t) {
}
![Page 33: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/33.jpg)
Size of a TreeSize of a Tree
public static int size (BinaryNode t) {if (t == null)
return 0 ;else
return 1 + size(t.left) + size(t.right);}
![Page 34: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/34.jpg)
Height of a treeHeight of a treeRecursive view of height calculationHT = Max(HL,HR) + 1;
![Page 35: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/35.jpg)
Height of a TreeHeight of a Tree
public static int height (BinaryNode t) {if (t == null) return -1 ;else
return 1 + Math.max(height(t.left))
,(height(t.right));}
![Page 36: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/36.jpg)
Tree TraversalTree Traversal
(a) Preorder(b) Postorder(c) Inorder
![Page 37: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/37.jpg)
PreorderPreorder
public void printPreOrder() {
}
![Page 38: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/38.jpg)
PreorderPreorder
public void printPreOrder() {System.out.println(element); if (left != null)
left.printPreOrder();if (right != null)
right.printPreOrder();}
![Page 39: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/39.jpg)
InorderInorder
public void printInOrder() {
}
![Page 40: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/40.jpg)
InorderInorder
public void printInOrder() {if (left != null)
left.printInOrder(); System.out.println(element); if (right != null)
right.printInOrder();}
![Page 41: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/41.jpg)
PostorderPostorder
public void printPostOrder() {
}
![Page 42: CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; }](https://reader033.fdocuments.in/reader033/viewer/2022051216/5697bfe11a28abf838cb3849/html5/thumbnails/42.jpg)
PostorderPostorder
public void printPostOrder() {if (left != null)
left.printPostOrder();if (right != null)
right.printPostOrder();System.out.println(element);
}