Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int Algo_20170718_2(BST T, int h)
- {
- if (T == null)
- {
- return -1;
- }
- else
- {
- // valori costanti di s & d ad ogni inizio di ricorsione
- var s = 5;
- var d = 3;
- Console.WriteLine("Ispezionando {0}: h:{1} s:{2} d:{3}", T.key, h, s, d);
- if (T.sx != null)
- s = Algo_20170718_2(T.sx, h + 11);
- if (T.dx != null)
- d = Algo_20170718_2(T.dx, h + 7);
- var ret =
- (s == 5 && d == 3) ?
- h :
- s + d;
- Console.WriteLine("Ispezionando {0}: h:{1} s:{2} d:{3} ret:{4}", T.key, h, s, d, ret);
- return ret;
- }
- }
- public static int Algo_20170718_2_Miky(BST T, int h)
- {
- BST last = null;
- BST next = null;
- BST curr = T;
- var S = new Stack<BST>();
- var RET = -1;
- var S_s = new Stack<int>();
- var S_d = new Stack<int>();
- var S_h = new Stack<int>();
- S_h.Push(h);
- while (S.Count > 0 || curr != null)
- {
- if (curr != null)
- {
- S.Push(curr);
- next = curr.Sx;
- // valori costanti di s & d ad ogni inizio di ricorsione
- var temp_s = 5;
- var temp_d = 3;
- S_s.Push(temp_s);
- S_d.Push(temp_d);
- var temp_h_sx = S_h.Peek();
- if (next != null)
- S_h.Push(temp_h_sx += 11);
- Console.WriteLine("Ispezionando {0}: h:{1} s:{2} d:{3}", curr.key, temp_h_sx, temp_s, temp_d);
- }
- else
- {
- curr = S.Peek();
- if (curr.Dx != null && curr.Dx != last)
- {
- next = curr.Dx;
- var temp_h_dx = S_h.Peek();
- if (next != null)
- S_h.Push(temp_h_dx += 7);
- }
- else
- {
- next = null;
- S.Pop();
- var temp_h = S_h.Pop();
- var temp_s = S_s.Pop();
- var temp_d = S_d.Pop();
- var temp_ret =
- (temp_s == 5 && temp_d == 3) ?
- temp_h :
- temp_s + temp_d;
- if (S.Count > 0)
- {
- if (curr == S.Peek().sx) // sono un sinitro
- {
- S_s.Pop();
- S_s.Push(temp_ret);
- }
- else if (curr == S.Peek().dx) // sono un destro
- {
- S_d.Pop();
- S_d.Push(temp_ret);
- }
- }
- Console.WriteLine("Ispezionando {0}: h:{1} s:{2} d:{3} ret:{4}", curr.key, temp_h, temp_s, temp_d, temp_ret);
- RET = temp_ret;
- }
- }
- last = curr;
- curr = next;
- }
- return RET;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement