S
subramanian100in
This is not a homework assignment question.
Kindly excuse me for posting this question here.
Suppose a BST contains 100,000 ndoes.
Suppose I have to traverse the BST by Preorder, say, without using
recursion. Suppose I know the count of nodes. Suppose I use a stack
approach for traversal.
Implementation 1:
I allocate memory for an array of nodeCount times the size of a tree
node. Then I traverse the tree using this stack for pushing and
popping the nodes temporarily.
Advantage: malloc and free will be called only once.
DIsadvantage: huge memory will be needed, whose availability may not
be assured and even if available, all of it may not be needed.
Implementation 2:
Instead of an array of nodes, I use a singly linked list. Push and pop
in the beginning of the list each time.
Advantage: memory is allocated only on need based.
Disadvantage : frequent calls to malloc and free.
QUESTION: Kindly explain as to which approach should be preferred and
why ?
Is there any other way of traversing the tree without using recursion?
Thanks
Kindly excuse me for posting this question here.
Suppose a BST contains 100,000 ndoes.
Suppose I have to traverse the BST by Preorder, say, without using
recursion. Suppose I know the count of nodes. Suppose I use a stack
approach for traversal.
Implementation 1:
I allocate memory for an array of nodeCount times the size of a tree
node. Then I traverse the tree using this stack for pushing and
popping the nodes temporarily.
Advantage: malloc and free will be called only once.
DIsadvantage: huge memory will be needed, whose availability may not
be assured and even if available, all of it may not be needed.
Implementation 2:
Instead of an array of nodes, I use a singly linked list. Push and pop
in the beginning of the list each time.
Advantage: memory is allocated only on need based.
Disadvantage : frequent calls to malloc and free.
QUESTION: Kindly explain as to which approach should be preferred and
why ?
Is there any other way of traversing the tree without using recursion?
Thanks