c# - Linked list addition of two minimum at last node like tree -
i have switched c# c++. under situation using linked list contains nodes follows : (which sorted in increasing order)
1->1->1->44->46->48->49->50->null
what have :
(1)add first 2 nodes. (2)put obtained @ last node. so, here after first addition llist :(1+1=2)
1->1->1->44->46->48->49->50->2->null
(3)again add 2 minimum nodes , put sum @ last node(but don't add nodes added).
(4) time 2 minimum 1 , 2 because "1" , "1"(the first two) added. llist becomes :
1->1->1->44->46->48->49->50->2->3->null
(5) 2 minimum 3 , 44 ,so
1->1->1->44->46->48->49->50->2->3->47->null
(6) 2 minimum 46 , 47(see @ last have 47 second smallest) , 1->1->1->44->46->48->49->50->2->3->47->93->null (7) 48 , 49,
1->1->1->44->46->48->49->50->2->3->47->93->97->null
(8) next minimums "50" , "93" left so,
1->1->1->44->46->48->49->50->2->3->47->93->97->143->null
(9) finally: 97 , 143 left, so
1->1->1->44->46->48->49->50->2->3->47->93->97->143->240->null
there 1 element left(240) stop here.
could 1 please me in making it's algorithm ?
my idea: implement algorithm : here "freq" contains value , left , right tree left , right.
while (front != rear) { if (counter == 0) { console.writeline("check1"); temp = new node(); temp.freq = front.freq + front.next.freq; front.is_processed = 1; front.next.is_processed = 1; temp.is_processed = 0; temp.left = front; temp.right = front.next; temp.next = null; rear.next = temp; front = front.next.next; rear = rear.next; remaining = count_remaining(); if (remaining == 1) { break; } } if (rear.freq.equals(front.freq)) { console.writeline("check2"); temp = new node(); temp.freq = front.freq + rear.freq; rear.is_processed = 1; front.is_processed = 1; temp.is_processed = 0; temp.left = front; temp.right = rear; temp.next = null; rear.next = temp; rear = rear.next; remaining = count_remaining(); if (remaining == 1) { break; } } if (rear.freq < front.freq) { console.writeline("check3"); node pmin1 = null; node pmin2 = null; front_rear(ref pmin1, ref pmin2); temp = new node(); temp.freq = pmin1.freq + pmin2.freq; pmin1.is_processed = 1; pmin2.is_processed = 1; temp.is_processed = 0; temp.left = pmin2; temp.right = pmin1; temp.next = null; rear.next = temp; rear = rear.next; remaining = count_remaining(); if (remaining == 1) { break; } } if (rear.freq > front.freq) { console.writeline("check4"); node pmin1 = null; node pmin2 = null; front_rear(ref pmin1, ref pmin2); temp = new node(); temp.freq = pmin1.freq + pmin2.freq; pmin1.is_processed = 1; pmin2.is_processed = 1; temp.is_processed = 0; temp.left = pmin2; temp.right = pmin1; temp.next = null; rear.next = temp; rear = rear.next; remaining = count_remaining(); if (remaining == 1) { break; } } counter++; }
but surprisingly prints : (it not @ go "check3" if (rear.freq < front.freq)
, can see step5 above @ condition don't print check3
)
check1 check4 check4 check4 check4 check4 check4
why going in condition if (rear.freq > front.freq)
? (it's actualy tree using linked list) parent node sum of 2 minimum nodes.
your code sloppy, i'll give basic framework use
node first = findlowestunprocessednode(); node second = findlowestunprocessednode(); while(first != null && second != null) { node sumnode = new node(first, second) addtoendoflist(sumnode); node first = findlowestunprocessednode(); node second = findlowestunprocessednode(); }
here functions make yourself. these shouldn't hard
findlowestunprocessednode(...)
: finds lowest unprocessed node , marks processed. if nodes processed, returnsnull
. might find want add parameters method.node(node first, node second)
: add constructornode
class. should initialize node havefirst
left ,second
right, , it'sfreq
should sum offirst.freq
,second.freq
.addtoendoflist(node newnode);
: adds node end of list.
please note these abstract place-holder methods can , should add parameters them, or make them instance methods if think out.
Comments
Post a Comment