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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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
zzynxSr. Software engineerCommented:
>> I see while debugging...
While debugging which program?
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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
zzynxSr. Software 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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.