簡體   English   中英

返回二叉樹中節點的父節點

[英]Return parent of node in Binary 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)
       return root;
    else
       return parent(root.left);
    }
    //same case for right
}

但是這個程序不起作用並且給出了錯誤的結果。 我的基本算法是程序從root檢查開始,如果它在leftright 如果是孩子,或者是else查詢過的node ,請與孩子一起遞交。

這可以被重新描述為遍歷二叉樹以找到給定節點的父節點。

假設你有一個

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;
}

如果找到給定節點,此方法將遍歷樹並立即返回結果。 為了證明它是如何工作的,我們應該創建一個樣本樹並將其分配給root節點。 我們使用用作目標的唯一編號來計算每個節點。

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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM