BinaryTree preOrder iteration without using stack - binary-tree

Is there a way to preOrder iterate over a binaryTree without using stack or list?
recursive or not it doesnt matter ...
I just searched in the internet and I couldnt find any code doing such thing.
Note: Just save the values in array and not to print them out to the screen.

Related

Is there a way to return boolean success of BST removal in one traversal?

That is, return a boolean of whether or not an element was actually removed in the tree.
The common implementation is to call find() to see if the element is in your tree and return false if find() fails to find the target. This requires going down the tree twice: once for find() and once for remove().
One way is to set a private field flag that you will set upon finding the element during remove(). Seems kinda gross though. Anyone have any better ideas?
You can remove the elements in the BST in one traversal itself. There is no need of seperate traversal for finding the element in the removal process. To do remove the elements in the BST in one first traversal follow the steps:
Search for the node to be deleted.
If the node is found apply the removing algorithm
To clear understanding referenter link description here

Evaluating an expression tree without a stack

Is it possible to evaluate an expression tree (pre/postfix) without using a stack? Had this question while talking about trees in the algorithm class at school. My guess is no.
Yes, you can.
Do a breadth first traversal of the tree (like a search, but do through all the tree). You can do this with a vector/queue/list in a iterative way.
Once you are done you can go backwards through the list/vector/queue you generated in the previous step. At each point compute the value of the node in the list. Since you have all the children already visited (you are going backwards) all you have to do is to lookup their value and apply the instructions in the node.

Special stack to find medium

From this, we can design data structure special stack with method getMin() which should return minimum element from the SpecialStack.
My question is: How to implement the method getMed() which should return the medium element from the SpecailStack?
From this Data Structure to find median, we know the best data structure is two heaps. Left is a Max-Heap; Right is a Min-Heap. However, for my question it seems not good, because the top element pushed into stack must be maintained which heap can not do that. Am I right?
Edit I do not know how to maintain the index of latest pushed element with Heap.
Thanks a lot.
You could alternatively use an Order Statistic Tree.
You can use any balanced binary search tree here instead of a heap. It is easy to find min or max element in a tree(the leftmost and the rightmost node), and it also supports deletion in O(log N).
So you can maintain a stack and two binary search trees(instead of two heaps). Push implementation is pretty strainforward. To pop an element, you can delete the top element from a tree where it is stored, adjust the trees(like in two heaps algorithm) and then pop it from the stack.

Threaded Binary Search Trees Advantage

An explanation about Threaded Binary Search Trees (skip it if you know them):
We know that in a binary search tree with n nodes, there are n+1 left and right pointers that contain null. In order to use that memory that contain null, we change the binary tree as follows -
for every node z in the tree:
if left[z] = NULL, we put in left[z] the value of tree-predecessor(z) (i.e, a pointer to the node which contains the predecessor key),
if right[z] = NULL, we put in right[z] the value of tree-successor(z) (again, this is a pointer to the node which contains the successor key).
A tree like that is called a threaded binary search tree, and the new links are called threads.
And my question is:
What is the main advatage of Threaded Binary Search Trees (in comparison to "Regular" binary search trees).
A quick search in the web has told me that it helps to implement in-order traversal iteratively, and not recursively.
Is that the only difference? Is there another way we can use the threads?
Is that so meaningful advantage? and if so, why?
Recursive traversal costs O(n) time too, so..
Thank you very much.
Non-recursive in-order scan is a huge advantage. Imagine that somebody asks you to find the value "5" and the four values that follow it. That's difficult using recursion. But if you have a threaded tree then it's easy: do the recursive in-order search to find the value "5", and then follow the threaded links to get the next four values.
Similarly, what if you want the four values that precede a particular value? That's difficult with a recursive traversal, but trivial if you find the item and then walk the threaded links backwards.
The main advantage of Threaded Binary Search Trees over Regular one is in Traversing nature which is more efficient in case of first one as compared to other one.
Recursively traversing means you don't need to implement it with stack or queue .Each node will have pointer which will give inorder successor and predecessor in more efficient way , while implementing traversing in normal BST need stack which is memory exhaustive (as here programming language have to consider implementation of stack) .

can we construct a binary tree with singly linked list

I have googled a lot and none of them showed how to create a binary tree with singly linked list.
Is it even possible to create one ?! I remember that I Have read somewhere that Binary trees can be created using singly linked list.
You can represent a binary tree as an array. If the only direction you want to go in your tree is root-to-leaf, then you could, in theory, use a singly linked list instead of the array.
This would result, however, in a huge performance loss as you will have to go pointer chasing instead of just jumping directly to the next node, as you do in an array.
I find it hard to think of a scenario where you would actually do that, but its possible in principle.

Resources