meljun_cortes_jedi slides data structures chapter05 trees
Post on 06-Apr-2018
231 Views
Preview:
TRANSCRIPT
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
1/30
1Data Structures Trees
5 Trees
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
2/30
2Data Structures Trees
Objectives
At the end of the lesson, the student should be able to:
Discuss the basic concepts and definitions on trees
Identify the types of trees: ordered, oriented and free tree
Use the link presentation of trees
Explain the basic concepts and definitions on forests
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
3/30
3Data Structures Trees
Objectives
Convert a forest into its binary tree representation and viceversa using natural correspondence
Traverse forests using preorder, postorder, level-order andfamily-order
Create representation oftrees using sequential allocation
Represent trees using arithmetic tree representations
Use trees in an application: the equivalence problem
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
4/30
4Data Structures Trees
Definition and Related
Concepts *degree of a tree - degree of node(s) of highest degree Ordered Tree - order of each node is significant
Oriented Tree order of the subtrees of every node is
irrelevant
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
5/30
5Data Structures Trees
Definition and Related
Concepts Free Tree tree w/o a root and significant node orientation
Progression of Trees
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
6/30
6Data Structures Trees
Link Representation of Trees
Node structure of a tree node:
Some properties of a tree with n nodes and degree k:
The number of link fields = n * k
The number of non-null links = n-1(#branches)
The number of null links = n*k - (n-1) = n (k-1) + 1
Alternative structure for better space utilization:
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
7/30
7Data Structures Trees
Forests
zero or more disjoint trees taken together
ordered forest - if trees comprising a forest are orderedtrees and if their order in the forest is material
Natural Correspondence method used to convert anordered forest into a unique binary tree and vice versa
Let F = (T1, T2, ..., Tn) be an ordered forest of ordered trees. The
binary tree B(F) corresponding to F is obtained as follows:
If n = 0, then B(F) is empty. If n > 0, then the root of B(F) is the root of T1; the left subtree
of B(F) is B( T11, T12, ... T1m ) where T11, T12, ...
T1m are the subtrees of the root of T1; and the right
subtree of B (F) is B( T2, T3, ..., Tn ).
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
8/30
8Data Structures Trees
Forests
Non-recursive approach implementation:
1) Link together the sons in each family from left toright. (Note: the roots of the tree in the forest are
brothers, sons of an unknown father.)
2) Remove links from a father to all his sons except theoldest (or leftmost) son.
3) Tilt the resulting figure by 45 degrees.
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
9/30
9Data Structures Trees
Forests
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
10/30
10Data Structures Trees
Forests
Forest Traversal
forests can only be traversed in preorderand postorder since themiddle node concept is not defined
Preorder Traversal1) Visit the root of the first tree.2) Traverse the subtrees of the first tree in preorder.3) Traverse the remaining trees in preorder.
Postorder Traversal1) Traverse the subtrees of the first tree in postorder.
2) Visit the root of the first tree.3) Traverse the remaining trees in postorder.
Note: Forest postorder is the same as the B(F) inorder
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
11/30
11Data Structures Trees
Forests
Forest Traversal
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
12/30
12Data Structures Trees
Forests
Sequential representation of forests
preorder sequential representation
Using RLINK
actual internal representation:
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
13/30
13Data Structures Trees
Forests
Sequential representation of forests
preorder sequential representation
Using RTAG and a stack
Actual internal representation:
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
14/30
14Data Structures Trees
Forests
Sequential representation of forests
family-order sequential representation
Using LLINK
Actual internal representation:
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
15/30
15Data Structures Trees
Forests
Sequential representation of forests
family-order sequential representation
Using LTAG
Actual internal representation:
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
16/30
16Data Structures Trees
Forests
Sequential representation of forests
level-order sequential representation
Using LTAG and a queue
Actual internal representation:
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
17/30
17Data Structures Trees
Forests Converting from sequential to link representation
BinaryTree convert(){BinaryTree t = new BinaryTree();BTNode alpha = new BTNode();LinkedStack stack = new LinkedStack();BTNode sigma;BTNode beta;
t.root = alpha;for (int i=0; i
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
18/30
18Data Structures Trees
Arithmetic Tree
Representations degree - number of children of a node weight - number of descendants of a node
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
19/30
19Data Structures Trees
Arithmetic Tree
Representations preorder sequence with degrees
preorder sequence with weights
postorder sequence with degrees
postorder sequence with weights
INFO 1 2 5 11 6 12 13 7 3 8 4 9 14 15 16 10
WEIGHT 15 6 1 0 2 0 0 0 1 0 5 3 2 0 0 0
INFO 11 5 12 13 6 7 2 8 3 15 16 14 9 10 4 1
DEGREE 0 1 0 0 2 0 3 0 1 0 0 2 1 0 2 3
INFO 11 5 12 13 6 7 2 8 3 15 16 14 9 10 4 1
WEIGHT 0 1 0 0 2 0 6 0 1 0 0 2 3 0 5 15
INFO 1 2 5 11 6 12 13 7 3 8 4 9 14 15 16 10
DEGREE 3 3 1 0 2 0 0 0 1 0 2 1 2 0 0 0
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
20/30
20Data Structures Trees
Arithmetic Tree
Representations level-order sequence with degrees
level-order sequence with weights
INFO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
DEGREE 3 3 1 2 1 2 0 0 1 0 0 0 0 2 0 0
INFO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
WEIGHT 15 6 1 5 1 2 0 0 3 0 0 0 0 2 0 0
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
21/30
21Data Structures Trees
Equivalence Problem
equivalence relation - relation between the elements of aset of objects S satisfying the following three properties forany objects x, y and z (not necessarily distinct) in S:
(a) Transitivity: if x y and y z then x z
(b) Symmetry: if x y then y x(c) Reflexivity: x x
Equivalence ProblemGiven any pairs of equivalence relations of the form i j for any i, jin S, determine whetherk is equivalent to i, for any k, i in S, on the
basis of the given pairs. Theorem: An equivalence relation partitions its set S into disjoint
classes, called equivalence classes, such that two elements areequivalent if and only if they belong to the same equivalence class
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
22/30
22Data Structures Trees
Equivalence Problem
Computer implementation
use trees to represent equivalence classes
use union operation to merge equivalence classes - setting a treeas a subtree of another tree
use find operation to to determine if two objects belong to the sameequivalence class or not "do the objects have the same root in thetree?
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
23/30
23Data Structures Trees
Equivalence Problem
class Equivalence{int[] FATHER;int n;
Equivalence(){}
Equivalence(int size){n = size+1; /* +1 since FATHER[0] will not be
used */FATHER = new int[n];
}
void setSize(int size){
FATHER = new int[size+1];}
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
24/30
24Data Structures Trees
Equivalence Problem
void setEquivalence(int a[], int b[]){int j, k;for (int i=0; i 0) j = FATHER[j];
while (FATHER[k] > 0) k = FATHER[k];/* If not equivalent, merge the two trees */if (j != k) FATHER[j] = k;}
}
/* Returns true if equivalent, else returns false */
boolean test(int j, int k){while (FATHER[j] > 0) j = FATHER[j];while (FATHER[k] > 0) k = FATHER[k];/* If they have same root, they are equivalent */if (j == k) return true;else return false;
}
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
25/30
25Data Structures Trees
Equivalence Problem
Degeneracy and the Weighting Rule for Union
problem with the previous algorithm: degeneracy - producing a treethat has the deepest possible height (yielding O(n) time complexity)
Consider the set S = { 1, 2, 3, ..., n } and the equivalence relations 1
2, 1 3, 1 4, ..., 1 n:
ase Forest (Tree)Case Forest (Tree)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
26/30
26Data Structures Trees
Equivalence Problem
Degeneracy and the Weighting Rule for Union
to solve the problem, we use a technique known as the weightingrule for union
Let node i and node j be roots. If the number of nodes in the tree rooted
at node i is greater than the number of nodes in the tree rooted at nodej, then make node i the father of node j; else, make node j the father ofnode i
/* Implements weighting rule for union, O(1) time */void union(int i, int j){
int count = FATHER[i] + FATHER[j];if (Math.abs(FATHER[i]) > Math.abs(FATHER[j] {FATHER[j] = i;FATHER[i] = count;
} else{FATHER[i] = j;FATHER[j] = count;
}}
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
27/30
27Data Structures Trees
Equivalence Problem
Worst-case trees and the Collapsing Rule for Find
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
28/30
28Data Structures Trees
Equivalence Problem
Worst-case trees and the Collapsing Rule for Find
let n1, n2, ..., nm be nodes in the path from node n1 to the root r. To
collapse, we make r the father of np, 1 p < m:
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
29/30
29Data Structures Trees
Equivalence Problem
Worst-case trees and the Collapsing Rule for Find/* Implements the collapsing rule for find. Returns the
root of i */int find(int i){
int j, k, l;
k = i;
/* Find root */while (FATHER[k] > 0) k = FATHER[k];
/* Compress path from node i to the root */j = i;while (j != k){
l = FATHER[j];FATHER[j] = k;j = l;
}return k;
}
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
30/30
30Data Structures Trees
Summary Trees may be ordered, oriented or free
Link allocation can be used to represent trees. Trees can also berepresented sequentially using arithmetic tree representation.
Zero or more disjoint trees taken together are known as a forest
An ordered forest may be converted into a unique binary tree andvice versa using natural correspondence
Forests can be traversed in preorderand postorder
Forests can be represented sequentially using preorder, family-order and level-order sequential representations
The equivalence problem can be solved using trees and the unionand find operations. The weighting rule for union and thecollapsing rule for find aid in better algorithm performance.
top related