You are on page 1of 5

Course: Advanced Algorithms and Data Structures Department: Computer Engineering Year: 3rd Semester: 1st 6 :Lecturer: Dr.

M. Shaar Lecture : /6/ : A, B, C, D, E, F : 1- . 2- findelement . : : 6=n 11=1-)6(2=1-Size=2n :


4

1 A B

1 C D

findelement
k 3 p 0 1 4 9 r 3 3 1 1 strcmp (node[p].info, blanks) = = 0 node[p].Lcount True 4 True 2 True 1 ) (False findelement 9=p =<r node[p].Lcount 3<=4 true 3<=2 false 1<=1 true

Deleting an element from a list represented by a tree

Figure 6.1: the deletion algorithm

Figure 6.1 illustrates the results of deletion algorithm for a tree in which the nodes C, D, and B are deleted in that order. (Make sure that you follow the actions of the deletion algorithm on these examples).

We present the deletion algorithm to delete a leaf pointed to by P from a tree (and thus an element from a list) as follows:
if P = 0 then else

free node (P) { F= father (P) if P = left(F) then

{
Lcount (F) = Lcount(F) -1

B = right(F) } else B = left(F)


if node (B) is a leaf then {

Info(F) = info(B) free node(B) } climb up the tree


free node(P) Q=F while Q !=0 { F= father(Q) If Q = left(F)

then

Lcount(F) =Lcount(F)-1

B= right(F) } else B= left(F) if(node(Q) is a leaf AND Free node(B)) then { info (F) = info (Q)

free node(Q) } Q= F } }

Notes: The deletion itself is relatively easy. It involves only resetting a left or right pointer in the father of the deleted leaf Del to NULL. The counts in all ancestors of Del may have to be modified. The modification consists of reducing Lcount by one in each node nd of which Del was a left descendant. At the same time, if the brother of Del is a leaf, it can be moved up the tree to take the place of its father. We can then move that node up even farther if it has no brother in its new position. This may reduce the depth of the resulting tree. The algorithm never moves up a nonleaf node even if this could be done. (For example, the father of A and B in Figure 6.1 (b) has not been moved up.) This deletion algorithm involves inspection of up to two nodes (the ancestor of the node being deleted and that ancestor's brother) at each level. The operation of deleting the kth element of a list represented by a tree involves finding the element and then deleting it The C routine to delete the leaf pointed to by p using the sequential representation is somewhat simpler than is the foregoing corresponding algorithm.
delete (P) int p ; { int B , F, Q ; if (P = = 0) node [P].used = false ; else { F = (P-l)/2; If (P % 2! = 0) { node [F].Lcount= node [F].Lcount-1; B=2*F+2; } else B=2*F+1; if (strcmp (node[B].info, blanks) ! = 0) { strcpy (node [F].info, node[B].info) ; node [B].used = false ; } /* end if */
4

node [P].used = false ; Q =F; while (Q ! = 0 ) { if ( Q % 2 ! = 0) { node [F].Lcount= node [F].Lcount-1 ; B=2*F+2; } /* end if */ else B= 2*F +1; if(strcmp (node[Q].info, blanks) ! = 0 && node [B].used ==false) { strcpy (node [F].info, node[Q].info) ; node [Q].used = false; } /* end if */ Q = F; } /* end while */ } /* end if */ } /* end delete */

You might also like