在java中没有递归的preorder遍历 [英] preorder traversal without recursion in java

查看:222
本文介绍了在java中没有递归的preorder遍历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在没有递归的情况下为preorder / inorder / postorder遍历提供执行代码吗?

Can anyone provide executing code for preorder/inorder/postorder traversal without recursion?

推荐答案

任何努力?它看起来像一个学校作业。



首先,我们不在CodeProject上提供代码。社区的全部目的是帮助想要学习的程序员,如果他们遇到问题,他们可以在社区寻求帮助。



其次,目的是任务是看学生的水平,知道他/她是否理解了这个概念。通过询问现成的代码,您给导游留下了错误的印象。
Any effort? It looks like a school assignment.

First of all, we do not provide code here on CodeProject. The whole purpose of the community to help the programmers who want to learn and if they have problems, they can ask for help in the community.

Second, the purpose of assignment is to see the level of student to know if he/she has understood the concept. By asking the ready-made code, you are giving a wrong impression to your guide.


Manas Bhardwaj是完全正确的!



但至少要尝试自己搜索。

啊......你知道谷歌吗?好吧,看看它给你的是什么: http:// cybergrouch。 blogspot.hu/2012/03/non-recursive-binary-tree-traversal.html [ ^ ]。但是,甚至不要把这些代码作为你的功课,因为你的导师会知道,这对你来说太先进了。您仍然可以编译和调试/跟踪它,看看它是如何工作的。
Manas Bhardwaj is totally right!

But at least try to search for yourself.
Ah... you know google? Well, look what it has given to me for you: http://cybergrouch.blogspot.hu/2012/03/non-recursive-binary-tree-traversal.html[^]. But don't even dream to give that code as your homework, since your tutor will know, that it is too advanced for you. Still, you can compile and debug/trace it, and see how it works.


从您发布问题的方式来看,您可以假设您有一棵树来遍历非节点的位置leafes中有一些操作数,leafes是数字。希望你至少知道什么是预购,顺序和后序这是很容易使用递归算法。递归通过在局部变量所在的调用堆栈上预置状态来工作。



现在对于一些完全重要的东西而且从未想过(开玩笑):现在只需写一个算法不使用在支持方法或过程的所有语言中找到的隐式堆栈机制,而是编写一个非递归算法使用显式堆栈来跟踪或在哪里在你现在的树上。



正如我之前提到的其他人一样,请试一试!



问候,



- Manfred
From how you posted your question one can assume that you have a tree to traverse where the nodes that are not leafes have some kind of operand in them and the leafes are numbers. Hoping you at least know what preorder, inorder and postorder are this is quite easy to work out using a recursive algorithm. Recursion works by presering state on the call stack where the local variables reside.

And now for something totally whack and never before thought of (kidding): Now just write an algorithm without using the implicit stack mechanism found in all the languages that support methods or procedures, but rather write a non-recursive algorithm that uses an explicit stack to keep track or where in the tree you currently are.

As the others before me already mentioned, do give it a try!

Regards,

— Manfred

这篇关于在java中没有递归的preorder遍历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆