Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //BENE STYLE
- public static int CountOddRic(BST T)
- {
- BST last = null;
- var S = new Stack<BST>(); // stack dell'albero
- var RR = new Stack<int>(); // memorizzo il ret calcolato in preorder
- var RS = new Stack<int>(); // memorizzo il r_sx calcolato in inorder
- var ret = 0;
- while (S.Count > 0 || T != null)
- if (T != null)
- {
- // preorder
- ret = (T.Key % 2 == 1 ? T.Key : 0);
- RR.Push(ret);
- S.Push(T);
- T = T.Sx;
- }
- else
- {
- T = S.Peek();
- if (T.Dx != null && T.Dx != last)
- {
- // inorder
- if (T.Sx != null)
- RS.Push(ret);
- T = T.Dx;
- }
- else
- {
- // postorder
- var r_sx = 0;
- var r_dx = 0;
- if (T.Sx != null && T.Dx == null)
- r_sx = ret;
- else if (T.Sx != null && T.Dx != null)
- r_sx = RS.Pop();
- if (T.Dx != null)
- r_dx = ret;
- ret = RR.Pop();
- ret = ret + r_sx + r_dx;
- last = T;
- T = null;
- S.Pop();
- }
- }
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement