Poping from Binary Tree


(nawaz) #1

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


(Christophe Meessen) #2

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
}

(nawaz) #3

Thanks a lot