Search a node at nth level in n-ary tree - n-ary-tree

I have a n-ary tree. I am trying to find a node within that n-ary tree at a given level.
Is there a way to find or algo where I can pass the node name to be searched and the level at which it is currently in the tree and extract the node?
Trying different ways but so far no success. Any help would be appreciated. Thanks

Related

Given a pre-order binary tree visit construct a binary search tree with the same pre-order visit. (if possible)

Im trying to solve this problem:" A binary tree is given, check his pre-order visit and build a binary search tree with the same pre-order visit. Demonstrate if it is always possible, if not give an example when this is not possible."
Any help? I need to write pseudocode and give time complexity but i have a lot of doubts about building a binary-search-tree with the same pre-order visit for every possible binary tree.
If you are using the classic algorithm for inserting in a binary search tree, that is, to perform a search and on the found NULL pointer where the search was stopped to put the new node, then just to insert in an empty tree the preorder sequence will produce exactly a binary tree with exactly the given preorder sequence.
Just try. Traverse any preorder sequence and insert it in an empty tree and you will realize it.
I hope to help you. And welcome to stack overflow!

Preorder insertion

I need to implement an n-ary tree. The problem is I'm allowed to use only preorder traversal. What I find it hard is to make a function that will add new node. New nodes are being added from left to right, and again, I'm not allowed to use level order, only preorder.
What I thought is to maybe somehow compare levels of the leaf nodes, and if there are free nodes at the maximum level of the tree, then that's where I add new node. Since this is not that easy, and I'm not sure if I'm even on the right way, I decided to post question here to see if anyone has any idea? Or maybe if there's another way of doing this?
Thank you in advance.

Binary Search Tree Algorithm - Start Searching From a Node Other Than Root

Is there a way to search in a binary search tree starting from a node other than the root? E.g to start searching from a node in the third level of the tree.
Is there a way to search in a binary search tree starting from a node
other than the root?
Yes. There is.
But not with the traditional Binary Search Tree data structure.
You will have to modify the structure of a Node to achieve that.
One most straight forward way to achieve that is to have a pointer to root node from any other node. That way, you will be able to go to your root node directly from any given node and apply regular Binary Search algorithm. This method includes overhead of saving one more pointer to Root and would not change the time complexity of Binary Serach, it would remain O(lgN)
Other a bit complex way to achieve that would be to have a pointer to immidiate parent from any node. Given any node, you can first perform binary search on that subtree to find if the node to find exists on the subtree of the given node. That will finish the algorithm in O(lgM) where M is the number of nodes in the subtree. That search would be bit efficient because M < N. If you do not find the node in the subtree, then you will have to traverse back to the root. ( Root will have null as the parent pointer ) While traversing back, you can also track whether the node you are currently at is the node to find, if is is, you can directly return from there. Once you reach the root of the tree, you can apply standard Binary Serach algorithm. The time complexity for this method remains same as O(lgN). In most cases, the algorithm would finish faster than standard Binary Search.

Use case of different traversal order in binary tree

There are preorder, inorder and postorder traversal for a binary tree, but no matter what order, it just traverses the tree to find a matched path. Is there any use case where I have to use any of the orders? Or are they just different ways but no difference regarding practical usage? Thanks.
There is definite practical usage with these traversals.
There are few specific use cases as below :
By using In-order traversal, you can get sorted node values if your requirement needs sorted information..
By using Pre-order traversal, you can create a copy of the tree and also can be used to get prefix expression of an expression tree.
Postorder traversal is used to delete the tree and also can be useful to get postfix expression of an expression tree.
The appropriate traversal technique shall be used based on which nodes should be fetched first for the requirement / design in hand. In case, if your requirement requires roots to be processed /picked / analyzed before picking up leaf nodes then pre-order traversal shall be helpful. Else, if leaf nodes have to be processed / fetched / analyzed before root nodes, then post-order shall be helpful.

Can a non binary tree be tranversed in order?

We are dealing with a Most similar neigthbour algorithm here. Part of the algorithm involves searching in order over a tree.
The thing is that until now, we cant make that tree to be binary.
Is there an analog to in order traversal for non binary trees. Particularly, I think there is, just traversing the nodes from left to right (and processing the parent node only once?")
Any thoughts?
update
This tree will have in each node a small graph of n objects. Each node will have n children (1 per each element in the graph), each of which will be another graph. So its "kind of" a b tree, without all the overflow - underflow mechanics. So I guess the most similar in order traversal would be similar to a btree inorder traversal ?
Thanks in advance.
Yes, but you need to define what the order is. Post and Pre order are identical, but inorder takes a definition of how the branches compare with the nodes.
There is no simple analog of the in-order sequence for trees other than binary trees (actually in-order is a way to get sorted elements from a binary search tree).
You can find more detail in "The art of computer programming" by Knuth, vol. 1, page 336.
If breadth-first search can serve your purpose then you can use that.

Resources