I am trying to implement Stack using Binary tree. Stuck at how can we Pop things from binary tree keeping stack concepts

To implement a stack with a binary tree, you use the stack depth as key value. The stack depth is the number of elements in the binary tree.

To pop from the binary tree, you remove the entry whose key is the number of elements.

```
func (t *BinaryTree) Push(value interface{}) {
t.Insert(t.Len()+1, value)
}
func (t *BinaryTree) Pop() interface{} {
if t.Len() == 0 {
panic("pop empty stack")
}
value := t.Value(t.Len())
t.Remove(t.Len())
return value
}
```

1 Like

Thanks a lot

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.