You are on page 1of 5

deletion of nodes from the middle of a tree requires the movement of potentially many nodes to reflect

the change in level number of these nodes. These problems can be easily overcome through the use of a
linked representation. Each node will have three fields LCHILD, DATA and RCHILD as below: LCHILD DATA
RCHILD Enathi Rajappa Arts & Science College - Dept.of CS Page 4 B.Sc., CS/B.C.A., – Data Structures &
Algorithms – Unit II (Trees & Graphs) BINARY TREE TRAVERSAL Traversal is a process to visit all the nodes
of a tree and may print their values too. Because, all nodes are connected via edges (links) we always
start from the root (head) node. That is, we cannot randomly access a node in a tree. Let L, D, R stand for
moving left, printing the data, and moving right. • LDR  Inorder traversal • LRD  Postorder traversal •
DLR  Preorder traversal Inorder Traversal: In this traversal method, the left subtree is visited first, then
the root and later the right sub-tree. procedure INORDER(T) //T is a binary tree where each node has List
Representation of Binary Tree: While the above representation appears to be good for complete binary
trees it is wasteful for many other binary trees. In addition, the representation suffers from the general
inadequacies of sequential representations. Insertion or CHILD, DATA,RCHILD// if T≠ 0 then [print
(DATA(T)) call PREORDER(LCHILD(T)) call PREORDER(RCHILD(T))]] end PREORDER Enathi Rajappa Arts &
Science College - Dept.of CS Page 6 B.Sc., CS/B.C.A., – Data Structures & Algorithms – Unit II (Trees &
Graphs) Example: The output of pre-order traversal of this tree will be: A → B → D → E → C → F → G
Postorder Traversal In this traversal method, the root node is visited last, hence the name. First we
traverse the left subtree, then the right subtree and finally the root node. procedure POSTORDER (T) //T
is a binary tree where each node has three fields LCHILD, DATA,RCHILD// if T≠ 0 then [call
POSTORDER(LCHILD(T)) call POSTORDER(RCHILD(T)) print (DATA(T))] end POSTORDER Enathi Rajappa Arts
& Science College - Dept.of CS Page 7 B.Sc., CS/B.C.A., – Data Structures & Algorithms – Unit II (Trees &
Graphs) Example: The output of post-order traversal of this tree will be: D → E → B → F → G → C → A
THREADED BINARY TREE In the linked representation of bi three fields LCHILD, DATA,RCHILD// if T≠ 0
then [call) to represent a binary tree. Consider the above example of a binary tree and it is represented
as follows: For complete binary trees the representation is ideal as no space is wasted. For the skewed
tree less than half the array is utilized. Linked nary trees, more than one half of the link fields contain
NULL values which results in wastage of storage space. If a binary tree consists of n nodes then n+1 link
fields contain NULL values. So in order to effectively manage the space, a method was devised by Perlis
and Thornton in which the NULL links are replaced with special links known as threads. Such binary trees
with threads are known as threaded binary trees. Each node in a threaded binary tree either contains a
link to its child node or thread to other nodes in the tree. If the RCHILD(P) is normally equal to zero, we
will replace it by a pointer to the node which would be printed after P when traversing the tree in
inorder. A null LCHILD link at node P is replaced by a pointer to the node which immediately precedes
node P in inorder. In the memory represenINORDER(LCHILD(T)) print(DATA(T)) call(INORDER(RCHILD(T))]
end INORDER Enathi Rajappa Arts & Science College - Dept.of CS Page 5 B.Sc., CS/B.C.A., – Data
Structures & Algorithms – Unit II (Trees & Graphs) Example: The output of inorder traversal of this tree
will be: D → B → E → A → F → C → G Preorder Traversal: In this traversal method, the root node is
visited first, then the left subtree and finally the right subtree. procedure PREORDER (T) //T is a binary
tree where each node has three fields L BINARY TREE REPRESENTATIONS A binary tree data structure is
represented using two methods. Those methods are as follows: 1. Array Representation 2. Linked List
Representation Array Representation If a complete binary tree with n nodes (i.e., depth= [log2n] + 1) is
represented 1 > n, be able to distinguish between threads and normal pointers. This is done by adding
and has no parent. (ii) LCHILD(i) is at 2i if 2i≤n. If 2i > n, then i has no left child. (iii) RCHILD(i) is at 2i + 1 if
2i + 1≤ n. If 2i + er LBIT(P) = 0 if LCHILD(P) is a thread RB then i has no right child. Example tree: Enathi
Rajappa Arts & Science College - Dept.of CS Page 3 B.Sc., CS/B.C.A., – Data Structures & Algorithms –
Unit II (Trees & Graphs) In array representation of a binary tree, we use one-dimensional array (1-D
Arraytation we must IT(P) = 1 if RCHILD(P) is a normal pointer RBIT(P) = 0 if RCHILD(P) is a thread Enathi
Rajappa Arts & Science College - Dept.of CS Page 8 B.Sc., CS/B.C.A., – Data Structures & Algorithms –
Unit II (Trees & Grap two extra one bit fields LBIT and RBIT. LBIT(P) =1 if LCHILD(P) is a normal point
sequentially as above then for any node with index i, 1 ≤ i ≤ n we have: (i) PARENT(i) is at [i/2] if i ≠ 1.
When i = 1, i is the root hs) Example: In-order traversal of above binary tree is H - D - I - B - E - A - F - J - C
– G When we represent the above binary tree using linked list representation, nodes H, I, E, F, J and G left
child pointers are NULL. This NULL is replaced by address of its in-order predecessor respectively (I to D,
E to B, F to A, J to F and G to C), but here the node H does not have its in-order predecessor, so it points
to the root node A. And nodes H, I, E, J and G right child pointers are NULL. These NULL pointers are
replaced by address of its in-order successor respectively (H to D, I to B, E to A, and J to C), but here the
node G does not have its in-order successor, so it points to the root node A. Above example binary tree is
converted into threaded binary tree as follows. Enathi Rajappa Arts & Science College - Dept.of CS Page 9
B.Sc., CS/B.C.A., – Data Structures & Algorithms – Unit II (Trees & Graphs) APPLICATIONS OF TREES • Set
Representation • Decision Trees • Game Trees GRAPHS: TERMINOLOGY A graph, G, consists of two sets V
and E. V is a finite non-empty set of vertices. E is a set of pairs of vertices, these pairs are called edges.
V(G) and E(G) will represent the sets of vertices and edges of graph G. We will also write G = (V,E) to
represent a graph. In an undirected graph the pair of vertices representing any edge is unordered . Thus,
the pairs (v1, v2) and (v2, v1) represent the same edge. In a directed graph each edge is represented by a
directed pair (v1, v2). v1 is the tail and v2 the head of the edge. Therefore and represent two different
edges. Figure shown below has three graphs G1, G2 and G3. The graphs G1 and G2 are undirected. G3 is
a directed graph. V (G1) = {1,2,3,4}; E(G1) = {(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)} V (G2) = {1,2,3,4,5,6,7}; V
(G3) = {1,2,3}; E(G2) = {(1,2),(1,3),(2,4),(2,5),(3,6),(3,7)} E(G3) = {, , } An n vertex undirected graph with
exactly n(n - 1)/2 edges is said to be complete. G1 is the complete graph on 4 vertices while G2 and G3
are not complete graphs. In the case of a directed graph on n vertices the maximum number of edges is
n (n - 1). If (v1,v2) is an edge in E(G), then we shall say the vertices v1 and v2 are adjacent and that the
edge (v1,v2) is incident on vertices v1 and v2. Enathi Rajappa Arts & Science College - Dept.of CSN squ s
of the bars is about (N2)/2. In other words, the nal to the square of the problem size; if the pro druple.
We say that the worst-case time for cre big-O notation. For a problem of size N: d is "order 1": O(1) is
"order N": O(N) ouble the number of 1+2+...+ (N-1) for some doubles, so createList is list (0, 1, 2... N-1)
for each vidual bars. You can see quare, so its area is N2; he time for roblem size doubles, the reateList is
quadratic in the Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd
Term, 201 on 015-2016 • A quadratic-time method od is "order N squared": O(N2) Note that the big-O
expressions gets large enough, constants and than a linear-time method, which ns do not have constants
or low-order terms. Th nd low-order terms don't matter (a constant-tim ich will be faster than a
quadratic-time method Formal definition: A function T than some value n0: This is because; when N ime
method will be faster od). T (N) is O (F (N)) if for some constant c and d for all values of N greater T (N) <=
c * F (N) The idea is that T(N) is the exac size N, and that F(N) is an upper a problem of size N will be no w
the least upper bound on the actu act complexity of a method or algorithm as a fu per-bound on that
complexity (i.e., the actual tim worse than F(N)). In practice, we want the sm ctual complexity. For
example, consider T (N) = 3 n0 = 2. This is because for all va function of the problem time/space or
whatever for mallest F (N) -- 3 * N2 + 5. We c operations for each call to add is (because it has to move
every ite item at position 0). For the N ca time for all N calls? It is proport initial array is large enough to
hold N items, the is proportional to the number of items in the li item already in the array one place to
the right calls shown above, the list lengths are: 0, 1, 2... ortional to 0 + 1 + 2 + ... + N-1. Recall that we
don't care about t ent Measure the complexity are simple unless noted otherwis nts: (assume the
statements
This is code that really is exa to add shown above. The total ti Total time = time (statement 1) + If each
statement is "simple" (on constant and the total time is als exactly k statements; this is not an unrolled
loop l time is found by adding the times for all statem ) + time (statement 2) + ... + time (statement k)
(only involves basic operatiomethod createList, the "problem different for different values of N method
createList) is not indepe then the number of list when add is called ht to make room for the new ... N-1.
So what is the total t the exact time, just how the time depends on t em size" is the value of N (because
the number f N). It is clear that the time for the N calls (and endent of N (so createList is not a constant-
tim n the problem size. For er of operations will be and therefore the time for time method). Is it 23
Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd Term, 201 24
proportional to N (linear in N)? operations performed by createL different values of N: Clearly, the value
of the sum do not linear in N. In the following of the N calls. The value of the sum (0+1+2+... that the
bars fill about half of the therefore, the sum of the areas o method createList is proportiona number of
operations will quadr problem size. Notes (Big O) We express complexity using bi • A constant-time
method • A linear-time method is on 015-2016 )? That would mean that doubling N would dou teList.
Here's a table showing the value of 0+1+ N 0+1+2+...+(N-1) 4 6 8 28 16 120 does more than double when
the value of N d ng graph, the bars represent the lengths of the li ...+ (N-1)) is the sum of the areas of the
individ the square. The whole square is an N-by-an show that T (N) is O (N2) values of N greater than 2: )
by choosing c = 4 and 3 * N2 + 5 <= 4 * N2 T(N) is not O(N), because whate N greater than n0 so that 3 *
N2 atever constant c and value n0 you choose, I ca + 5 is greater than c * N. can always find a value of
PROBLEM (4) Objective How to determine the ru kinds of statements are used. running time of a piece
of code? You will find d that it depends on what Problem Statemns) then the time fo also constant: O (1).
• Sample code2: if-then-e else statements Either sequence 1 will execu slower of the two possibilities:
Max (time (sequence 1), time (s For example, if sequence 1 is O else statement would be O (N). oop like
the N calls tements: k) for each statement is cute, or sequence 2 will execute. Therefore, the the worst-
case time is the sequence 2)) O (N) and sequence 2 is O (1) the worst-case ti time for the whole if-then •
Sample code3: for loops ps The loop executes N times, s the statements are O (1), the tota s, so the
sequence of statements also executes N otal time for the for-loop is N * O (1), which is • Sample code4:
Nested lo o Nested loop 1 d loops Here the number of itera index. The outer loop executes N times. As a
result, the statements complexity is O (N * M). In a co < N instead of j < M (i.e., the inn is O (N2). s N
times. Since we assume is O (N) overall. rations of the inner loop is independent of the v s N times. Every
time the outer loop executes, t nts in the inner loop execute a total of N * M ti common special case
where the stopping cond inner loop also executes N times), the total com e value of the outer loop's s,
the inner loop executes M times. Thus, the ndition of the inner loop is j omplexity for the two loops o
Nested loop2 In this code the number o index. Now we can't just multipl r of iterations of the inner loop
depends on the iply the number of iterations of the outer loop t 27 he value of the outer loop's p times
the number of Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd Term,
201 on 015-2016 iterations of the inner loop, beca let's think about how many itera table: cause the
inner loop has a different number of rations that inner loop has. That information is V Value of i 0 0 1 1 2
2 ... .. N N-2 N N-1 N N-1 N-2 ... 2 1 So we can see that the total num ... + 3 + 2 + 1. We've seen that f f
iterations each time. So is given in the following Number of iterations of inner loop mber of times the
sequence of statements exec t formula before: the total is O (N2). ecutes is: N + N-1 + N-2 + PROBLEM (5)
Objective Find worst-case complex lexity for a code. Problem Statement What is the worst-case complex
exity of the each of the following code fragmen
This is code that really is exa to add shown above. The total ti Total time = time (statement 1) + If each
statement is "simple" (on constant and the total time is als exactly k statements; this is not an unrolled
loop l time is found by adding the times for all statem ) + time (statement 2) + ... + time (statement k)
(only involves basic operations) then the time fo also constant: O (1). • Sample code2: if-then-e else
statements Either sequence 1 will execu slower of the two possibilities: Max (time (sequence 1), time (s
For example, if sequence 1 is O else statement would be O (N). oop like the N calls tements: k) for each
statement is cute, or sequence 2 will execute. Therefore, the the worst-case time is the sequence 2)) O
(N) and sequence 2 is O (1) the worst-case ti time for the whole if-then • Sample code3: for loops ps The
loop executes N times, s the statements are O (1), the tota s, so the sequence of statements also
executes N otal time for the for-loop is N * O (1), which is • Sample code4: Nested lo o Nested loop 1 d
loops Here the number of itera index. The outer loop executes N times. As a result, the statements
complexity is O (N * M). In a co < N instead of j < M (i.e., the inn is O (N2). s N times. Since we assume is
O (N) overall. rations of the inner loop is independent of the v s N times. Every time the outer loop
executes, t nts in the inner loop execute a total of N * M ti common special case where the stopping
cond inner loop also executes N times), the total com e value of the outer loop's s, the inner loop
executes M times. Thus, the ndition of the inner loop is j omplexity for the two loops o Nested loop2 In
this code the number o index. Now we can't just multipl r of iterations of the inner loop depends on the
iply the number of iterations of the outer loop t 27 he value of the outer loop's p times the number of
Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd Term, 201 on 015-
2016 iterations of the inner loop, beca let's think about how many itera table: cause the inner loop has a
different number of rations that inner loop has. That information is V Value of i 0 0 1 1 2 2 ... .. N N-2 N
N-1 N N-1 N-2 ... 2 1 So we can see that the total num ... + 3 + 2 + 1. We've seen that f f iterations each
time. So is given in the following Number of iterations of inner loop mber of times the sequence of
statements exec t formula before: the total is O (N2). ecutes is: N + N-1 + N-2 + PROBLEM (5) Objective
Find worst-case complex lexity for a code. Problem Statement What is the worst-case complex exity of
the each of the following code ). In a co < N instead of j < M (i.e., the inn is O (N2). s N times. Since we
assume is O (N) overall. rations of the inner loop is independent of the v s N times. Every time the outer
loop executes, t nts in the inner loop execute a total of N * M ti common special case where the stopping
cond inner loop also executes N times), the total com e value of the outer loop's s, the inner loop
executes M times. Thus, the ndition of the inner loop is j omplexity for the two loops o Nested loop2 In
this code the number o index. Now we can't just multipl r of iterations of the inner loop depends on the
iply the number of iterations of the outer loop t 27 he value of the outer loop's p times the number of
Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd Term, 201 on 015-
2016 iterations of the inner loop, beca let's think about how many itera table: cause the inner loop has a
different number of rations that inner loop has. That information is V Value of i 0 0 1 1 2 2 ... .. N N-2 N
N-1 N N-1 N-2 ... 2 1 So we can see that the total num ... + 3 + 2 + 1. We've seen that f f iterations each
time. So is given in the following Number of iterations of inner loop mber of times the sequence of
statements exec t formula before: the total is O (N2). ecutes is: N + N-1 + N-2 + PROBLEM (5) Objective
Find worst-case complex lexity for a code. Problem Statement What is the fragmen

You might also like