Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

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
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

I see while debugging lot of Thread class methods being called by Stack as well not sure why.
Avatar of ozo
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.
>> I see while debugging...
While debugging which program?
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?
Avatar of gudii9

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.
Avatar of gudii9

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.
Avatar of gudii9

ASKER

let me understand this post
Avatar of gudii9

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
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial