Samsung Interview Question
Developer Program EngineersCountry: India
Initially when the tree is created with a single node, the root of the tree and top of stack point to this node. when second node is added Top becomes the new node and an extra pointer is maintained which points to its predecessor top node. Inthis way nodes can be pushed and popped and a BST structure can be maintained.
This seems to be a bad idea. Because insertion in BST would take O(logN) time. Where as for ordinary stack insertion takes O(1) time. Same is the case even for pop (deletion) and peek. I guess they just want to check insertion and deletion operations in BST.
- Anonymous September 23, 2011If some one thinks there is an advantage with using BST behind a stack then please post it.