# 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

LVL 7
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
I see while debugging lot of Thread class methods being called by Stack as well not sure why.
0
Commented:
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.
0
Sr. Software engineerCommented:
>> I see while debugging...
While debugging which program?
0
Author Commented:
0
Commented:
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.
0
Sr. Software engineerCommented:
gudii9, you really have the bad habit of not answering our questions.
0
Author Commented:
>> 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.
0
Author Commented:
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.
0
Commented:
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.
0
Author Commented:
let me understand this post
0
Author Commented:
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
0
Commented:
``````static void print_binary_recurse(int n){
if( n/2 > 0 ){
print_binary_recurse(n/2);
}
System.out.print(n%2);
}

static void print_binary_stack(int n){
Stack<Integer> stack = new Stack<Integer>();
for(;;){
if( n/2 > 0 ){
stack.push(n);
n=n/2;
continue;
}
break;
}
for(;;){
System.out.print(n%2);
if( !stack.empty() ){
n=stack.pop();
continue;
}
break;
}
}
``````
These versions should make the iterative pushes and pops of n on the explicitly declared Stack more directly parallel to the recursive pushes and pops of n on the implicit program execution stack.
0

Experts Exchange Solution brought to you by