Tag: 二叉树

如何打印二叉树图?

如何在Java中打印二叉树,使输出如下所示: 4 / \ 2 5 我的节点: public class Node<A extends Comparable> { Node<A> left, right; A data; public Node(A data){ this.data = data; } }

如何find任意二叉树中两个节点的最低共同祖先?

这里的二叉树可能不一定是二叉search树。 该结构可以被视为 – struct node { int data; struct node *left; struct node *right; }; 我可以和朋友一起解决的最大的解决scheme就是这样的 – 考虑这个二叉树 : 二叉树http://lcm.csa.iisc.ernet.in/dsa/img151.gif 中序遍历产生 – 8,4,9,2,5,1,6,3,7 而后序遍历产生 – 8,9,4,5,2,6,7,3,1 例如,如果我们想要find节点8和节点5的共同祖先,那么我们在中序树遍历中列出所有在8和5之间的节点,在这种情况下恰好是[4,9 ,2]。 然后我们检查这个列表中的哪个节点在后序遍历中最后出现,这是2.因此,对于8和5的共同祖先是2。 这个algorithm的复杂性,我相信是O(n)(O(n)对于inorder / postorder遍历,其余的步骤再次是O(n),因为它们只不过是数组中的简单迭代)。 但是这是错误的。 🙂 但是这是一个非常粗糙的方法,我不确定是否在某些情况下出现故障。 有没有其他(可能更优化)解决这个问题?

无recursion二叉树的后序遍历

没有使用recursion做二叉树的后序遍历的algorithm是什么?

如何确定二叉树是否平衡?

这些学年已经有一段时间了。 在医院find了IT专家的工作。 试图移动到现在做一些实际的编程。 我现在正在研究二叉树,我想知道确定树是否高度平衡是最好的方法。 我在想这个: public boolean isBalanced(Node root){ if(root==null){ return true; //tree is empty } else{ int lh = root.left.height(); int rh = root.right.height(); if(lh – rh > 1 || rh – lh > 1){ return false; } } return true; } 这是一个很好的实现? 还是我错过了什么?

跳过列表与二进制树

我最近遇到了称为跳过列表的数据结构。 他们似乎有非常相似的行为二叉search树…我的问题是 – 为什么你会想在二叉search树上使用跳过列表?

解释Morris inorder树遍历而不使用堆栈或recursion

有人可以帮助我理解下面的Morris inorder树遍历algorithm,而不使用堆栈或recursion? 我试图了解它是如何工作的,但它只是逃避我。 1. Initialize current as root 2. While current is not NULL If current does not have left child a. Print current's data b. Go to the right, ie, current = current->right Else a. In current's left subtree, make current the right child of the rightmost node b. Go to this left child, […]

如何实现二叉树?

哪个是可以用来在Python中实现二叉树的最好的数据结构?

C如何“绘制”二进制树到控制台

什么algorithm可以用来在控制台中绘制二叉树? 树在C中实现。例如,控制台中将显示一个数字为2 3 4 5 8的BST,如下所示:

二叉树有哪些应用?

我想知道二叉树的特定应用是什么。 你能举一些真实的例子吗?