gudii9
asked on
stack peek and pop methods to get binary of number
Hi,
I was looking at
stack peek and pop methods to get binary of number
i have not clearly understood the mechanics, algorithm. can you please advise. are there any alternatives to this. please advise
I was looking at
stack peek and pop methods to get binary of number
i have not clearly understood the mechanics, algorithm. can you please advise. are there any alternatives to this. please advise
Where were you looking at it?
Without seeing it myself, I would guess that the stack might be used to reverse the order of binary digits that were generated starting at the low end.
Without seeing it myself, I would guess that the stack might be used to reverse the order of binary digits that were generated starting at the low end.
>> I see while debugging...
While debugging which program?
While debugging which program?
ASKER
i think this is closer link
http://stackoverflow.com/questions/1294701/post-order-traversal-of-binary-tree-without-recursion
http://stackoverflow.com/questions/1294701/post-order-traversal-of-binary-tree-without-recursion
Post-order traversal of a binary tree is somewhat different from getting binary of a number, but both can use recursion or an explicit stack for reversing the order of processed elements, whether they are nodes visited in pre-order, or low order digits.
gudii9, you really have the bad habit of not answering our questions.
I asked you something but didn't get an answer yet. Then, how do you expect us to answer your questions?
I asked you something but didn't get an answer yet. Then, how do you expect us to answer your questions?
ASKER
>> I see while debugging...
While debugging which program?
sorry. I read this in rush earlier and missed it.
I was watching this video on one of online java course. I do not have that source code with me.
ASKER
Post-order traversal of a binary tree is somewhat different from getting binary of a number, but both can use recursion or an explicit stack for reversing the order of processed elements, whether they are nodes visited in pre-order, or low order digits.
can you please provide some simple examples, source code on these topics.
static void print_binary_recurse(int n){
if( n>1 ){ print_binary_recurse(n/2); }
System.out.print(n%2);
}
static void print_binary_stack(int n){
Stack<Integer> stack = new Stack<Integer>();
do{
stack.push(n%2);
}while( (n/=2)>0 );
while( !stack.empty() ){
System.out.print(stack.pop ());
}
}
The correspondence between the two methods might be made more direct, but I thought that would clutter the individual examples.
Note that both make use of a stack. In the recursive example, the program execution stack is used implicitly.
Similarly, using an implicit or explicit stack to reverse the order of nodes visted in pre-order can be a way to do a post-order traversal of a binary tree.
if( n>1 ){ print_binary_recurse(n/2);
System.out.print(n%2);
}
static void print_binary_stack(int n){
Stack<Integer> stack = new Stack<Integer>();
do{
stack.push(n%2);
}while( (n/=2)>0 );
while( !stack.empty() ){
System.out.print(stack.pop
}
}
The correspondence between the two methods might be made more direct, but I thought that would clutter the individual examples.
Note that both make use of a stack. In the recursive example, the program execution stack is used implicitly.
Similarly, using an implicit or explicit stack to reverse the order of nodes visted in pre-order can be a way to do a post-order traversal of a binary tree.
ASKER
let me understand this post
ASKER
The correspondence between the two methods might be made more direct, but I thought that would clutter the individual examples.
Note that both make use of a stack. In the recursive example, the program execution stack is used implicitly.
Similarly, using an implicit or explicit stack to reverse the order of nodes visted in pre-order can be a way to do a post-order traversal of a binary tree.
i was not clear on above explanation. can you please elaborate
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER