简体   繁体   English

Java-在二叉树中找到节点的父级? (从普通树转换而来)

[英]Java - finding a parent of a node in a Binary Tree? (converted from a general tree)

I have a binary tree that was converted from a general tree. 我有一棵从普通树转换而来的二叉树。 Meaning, the left node of any given node is that node's child, and the right node of any given node is that node's sibling. 意思是,任何给定节点的左节点是该节点的子节点,而任何给定节点的右节点是该节点的兄弟节点。

My question is - how can i write a method that will take a node and find its parent? 我的问题是-我如何编写一个将采用节点并找到其父节点的方法? (by traversing the entire tree i guess) (通过遍历整个树,我猜)

thanks! 谢谢!

Let see if I understand this correctly 让我看看我是否正确理解了

It depends on your implementation of tree. 这取决于您对树的实现。 Usually tree do not have any cycles and each node have a reference to a child. 通常,树没有任何循环,并且每个节点都有对子代的引用。 But again, this is all how the tree is being implemented. 但是同样,这就是树的实现方式。

If you are looking for leaf, you can keep recursing to the child of the node until it reaches the base case where child is equal to null. 如果要查找叶子,则可以继续递归到该节点的子节点,直到到达子节点等于null的基本情况为止。

findLeaves (Node n) {
  if (n == null)
    return;
  else if (n.left == null AND n.right == null)
    return n; // A child
  else
    findLeaves(n.left);
    findLeaves(n.right);

}

Same thing with root. 与root相同。 If the node have reference to parent and we are looking for the root. 如果节点引用了父节点,我们正在寻找根节点。 keep recursing to the parents until the parents is null which means that node is a parent. 继续递归给父母,直到父母为空,这意味着该节点是父母。

findRoot (Node n) {
  if (n == null)
    return;
  else if (n.parent == null)
    return n; // A root
  else
    findRoot(n.parent);

}

I hope that helps 希望对您有所帮助

this will help you 这会帮助你

private static BinaryNode getParent(AnyType x, BinaryNode<AnyType> t, BinaryNode<AnyType> parent) {
        if (t == null) {
            return null;
        } else {
            if (x.compareTo(t.element) < 0) {
                return getParent(x, t.left, t);
            } else if (x.compareTo(t.element) > 0) {
                return getParent(x, t.right, t);
            } else {
                return parent;
            }
        }
    }

if you have a node class that contains 'value' integer variable. 如果您的节点类包含“值”整数变量。 a sudo-code can be written as something like this sudo代码可以写成这样的东西

Node find_parent(Node currentNode, Node parentNode, int valueOfChildNode) {
     Node finalNode = null;
     if ( currentNode->value == valueOfChildNode ) return parentNode;
     if ( currentNode->left_child) 
          finalNode = find_parent(currentNode->left_child,valueOfChildNode,currentNode);
     if ( !finalNode && currentNode->right_child )
          find_parent(currentNode->right_child,valueOfChildNode,currentNode);
     return finalNode;
}

you can change it according to your requirement. 您可以根据需要进行更改。 you can call it as in the example given below. 您可以按照以下示例进行调用。

find_parent( binaryTree->root_node, null, 15);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM