Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回二叉树中节点的父级_Java_Algorithm_Tree_Binary Search Tree - Fatal编程技术网

Java 返回二叉树中节点的父级

Java 返回二叉树中节点的父级,java,algorithm,tree,binary-search-tree,Java,Algorithm,Tree,Binary Search Tree,我正在编写一个代码来返回任何节点的父节点,但是我被卡住了。我不想使用任何预定义的ADT //Assume that nodes are represented by numbers from 1...n where 1=root and even //nos.=left child and odd nos=right child. public int parent(Node node){ if (node % 2 == 0){ if (root.left==node)

我正在编写一个代码来返回任何节点的父节点,但是我被卡住了。我不想使用任何预定义的ADT

//Assume that nodes are represented by numbers from 1...n where 1=root and even 
//nos.=left child and odd nos=right child.
public int parent(Node node){
    if (node % 2 == 0){
       if (root.left==node)
       return root;
    else
       return parent(root.left);
    }
    //same case for right
}

但是这个程序不起作用,并且给出了错误的结果。我的基本算法是程序从根开始检查它是在左边还是在右边。如果是子节点或查询的节点是其他节点,则将其与子节点一起递归。

这可以重新表述为遍历二叉树以查找给定节点的父节点

假设你有一个

class Node {
  int node;
  Node left;
  Node right;

  Node(int node, Node left, Node right) {
    this.node = node;
    this.left = left;
    this.right = right;
  }
  @Override
  public String toString (){
     return "("+node+")";
  }
}
为了简单起见,我们将只定义全局变量

Node root;
int target;
boolean found;
它们将通过下一个方法访问。首先,我们初始化一个方法调用

public Node findParent(int target){
  found = false;
  this.target = target;
  return internalFindParent(root, null);
}
其次,我们编写一个实现

private Node internalFindParent(Node node, Node parent){
  if (found) return parent;
  if (node.node == target) {
    found = true;
    return parent;
  }
  if (node.left == null) return null;
  Node temp = internalFindParent(node.left, node);
  if(temp != null)
    return temp;
  if (node.right == null) return null;
  temp = internalFindParent(node.right, node);
  if(temp != null)
    return temp;
  return null;
}
此方法遍历树,如果找到给定节点,则立即返回结果。为了演示它是如何工作的,我们应该创建一个示例树并将其分配给根节点。我们使用唯一的编号作为目标来计算每个节点

public void init() {
  root = new Node (0,
    new Node(1,
      new Node (2,
        new Node (3,
          new Node (4, null, null),
          new Node (5, null, null)
        ),
        new Node (6,
          new Node (7, null, null),
          new Node (8, null, null)
        )
      ),
      new Node (9,
        new Node (10,
          new Node (11, null, null),
          new Node (12, null, null)
        ),
        new Node (13,
          new Node (14, null, null),
          new Node (15, null, null)
        )
      )
     ),
    new Node(21,
      new Node (22,
        new Node (23,
          new Node (24, null, null),
          new Node (25, null, null)
        ),
        new Node (26,
          new Node (27, null, null),
          new Node (28, null, null)
        )
      ),
      new Node (29,
        new Node (30,
          new Node (31, null, null),
          new Node (32, null, null)
        ),
        new Node (33,
          new Node (34, null, null),
          new Node (35, null, null)
        )
      )
    )
  );
}
为了简单起见,只需在构造函数中执行所有测试

FindingParent(){
  init();
  for (int i=0; i<=35; i++){
    Node parent = findParent(i);
    if (parent != null)
      System.out.println("("+parent.node+", "+i+")");
  }

}
/**
 * @param args
 */
public static void main(String[] args) {
  new FindingParent();
  System.exit(0);
}

此输出结果为树中每个节点的父节点对和子节点对。

这可以重新表述为遍历二叉树以查找给定节点的父节点

假设你有一个

class Node {
  int node;
  Node left;
  Node right;

  Node(int node, Node left, Node right) {
    this.node = node;
    this.left = left;
    this.right = right;
  }
  @Override
  public String toString (){
     return "("+node+")";
  }
}
为了简单起见,我们将只定义全局变量

Node root;
int target;
boolean found;
它们将通过下一个方法访问。首先,我们初始化一个方法调用

public Node findParent(int target){
  found = false;
  this.target = target;
  return internalFindParent(root, null);
}
其次,我们编写一个实现

private Node internalFindParent(Node node, Node parent){
  if (found) return parent;
  if (node.node == target) {
    found = true;
    return parent;
  }
  if (node.left == null) return null;
  Node temp = internalFindParent(node.left, node);
  if(temp != null)
    return temp;
  if (node.right == null) return null;
  temp = internalFindParent(node.right, node);
  if(temp != null)
    return temp;
  return null;
}
此方法遍历树,如果找到给定节点,则立即返回结果。为了演示它是如何工作的,我们应该创建一个示例树并将其分配给根节点。我们使用唯一的编号作为目标来计算每个节点

public void init() {
  root = new Node (0,
    new Node(1,
      new Node (2,
        new Node (3,
          new Node (4, null, null),
          new Node (5, null, null)
        ),
        new Node (6,
          new Node (7, null, null),
          new Node (8, null, null)
        )
      ),
      new Node (9,
        new Node (10,
          new Node (11, null, null),
          new Node (12, null, null)
        ),
        new Node (13,
          new Node (14, null, null),
          new Node (15, null, null)
        )
      )
     ),
    new Node(21,
      new Node (22,
        new Node (23,
          new Node (24, null, null),
          new Node (25, null, null)
        ),
        new Node (26,
          new Node (27, null, null),
          new Node (28, null, null)
        )
      ),
      new Node (29,
        new Node (30,
          new Node (31, null, null),
          new Node (32, null, null)
        ),
        new Node (33,
          new Node (34, null, null),
          new Node (35, null, null)
        )
      )
    )
  );
}
为了简单起见,只需在构造函数中执行所有测试

FindingParent(){
  init();
  for (int i=0; i<=35; i++){
    Node parent = findParent(i);
    if (parent != null)
      System.out.println("("+parent.node+", "+i+")");
  }

}
/**
 * @param args
 */
public static void main(String[] args) {
  new FindingParent();
  System.exit(0);
}

此输出结果为树中每个节点的父节点对和子节点对。

尝试此方法。它可能会工作:

public BinaryTreeNode getParent(BinaryTreeNode root, BinaryTreeNode node) {

    BinaryTreeNode lh = null, rh = null;
    if (null == root)
        return null;

    if (root.getLeft() == node || root.getRight() == node)
        return root;

    lh = getParent(root.getLeft(), node);
    rh = getParent(root.getRight(), node);

    return lh != null ? lh : rh;

}

试试这个。它可能会起作用:

public BinaryTreeNode getParent(BinaryTreeNode root, BinaryTreeNode node) {

    BinaryTreeNode lh = null, rh = null;
    if (null == root)
        return null;

    if (root.getLeft() == node || root.getRight() == node)
        return root;

    lh = getParent(root.getLeft(), node);
    rh = getParent(root.getRight(), node);

    return lh != null ? lh : rh;

}

好吧,那不会编译的。你能发布你真正拥有的吗?但是你的方法返回int,你想让它返回root?root是一个包装类,就像它作为int一样吗?好吧,这不会编译。你能发布你真正拥有的吗?但是你的方法返回int,你想让它返回root?root是一个包装类,就像它作为int一样吗?