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
LVL 7
gudii9Asked:
Who is Participating?
 
ozoCommented:
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;
    }
}

Open in new window

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
 
gudii9Author Commented:
I see while debugging lot of Thread class methods being called by Stack as well not sure why.
0
 
ozoCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
zzynxSoftware engineerCommented:
>> I see while debugging...
While debugging which program?
0
 
ozoCommented:
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
 
zzynxSoftware engineerCommented:
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?
0
 
gudii9Author 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
 
gudii9Author 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
 
ozoCommented:
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
 
gudii9Author Commented:
let me understand this post
0
 
gudii9Author 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.