简体   繁体   English

将通用树转换为二叉树后,如何使用二叉树找到给定通用树节点“ v”的父级?

[英]After a General Tree is converted to a Binary Tree, how is it possible to find the parent given a node “v” of the General Tree using the Binary Tree?

I have tried two implementations and they don't quite work. 我尝试了两种实现方式,但它们效果不佳。

Here is one of my implementation that I am completely stuck on. 这是我完全坚持的实现之一。

/** Returns the parent of a given node or the node itself if it is the root */
public Position<E> parent(Position<E> v) throws IllegalArgumentException {
      if(bTree.isRoot(v)) { return v; } // returns the node v itself if v is the root.
      Position<E> parent = bTree.root(); 
      // execute this recursive method and return the parent. 
      return preorderTraversal(parent, v, null);
  }

/* This is the helper method that will traverse the binary tree in PreOrder. It updates 
 * the parent until the node v has been found.
 */
private Position<E> preorderTraversal(Position<E> focusNode, Position<E> v, Position<E> parent) {

     System.out.print(focusNode.getElement() + "\t");

      if (focusNode != null && v != focusNode && hasLeft(focusNode)) {
          parent = focusNode;
          System.out.print("setting parent to: " + parent.getElement() + "\t");
          preorderTraversal(bTree.left(focusNode), v, parent);
      }
      else if (focusNode != null && v != focusNode && hasRight(focusNode)){
          preorderTraversal(bTree.right(focusNode), v, parent);
      }
      return parent;
  }

// -------------- EXTRA HELPER METHODS ---------------
private boolean hasLeft(Position<E> temp ) {
      if (bTree.left(temp) != null) return true;
      else return false;
  }

  private boolean hasRight(Position<E> temp ) {
      if (bTree.right(temp) != null) return true;
      else return false;
  }

The problem here seems to be that it traverses the left subtree, and updates the correct parents, but while returning the value, it always returns the root node. 这里的问题似乎是它遍历左子树并更新正确的父级,但是在返回值时,它总是返回根节点。 I don't seem to understand why this is so. 我似乎不明白为什么会这样。 Another is that when traversing the right subtree, my parent nodes are always wrong. 另一个是遍历正确的子树时,我的父节点总是错误的。 Please help! 请帮忙!

You are treating left and right differently without giving a motivation. 您在没有动机的情况下对左右有所不同。 You have not specified what a general tree is (one guess is every node can have more than two descendants , but, as the converted nodes nodes seem to be comparable: how many values does an original node have? And how it is converted to a binary tree (left references have been descendant references, right for siblings?) Giving a URL might be appropriate). 您尚未指定通用树是什么(猜测每个节点可以具有两个以上的后代 ,但是,由于转换后的节点似乎具有可比性:原始节点具有多少值?如何将其转换为a二叉树(左边的引用是后代引用,右边的用于兄弟姐妹?)提供URL可能是适当的。
Combine the checks: if (null == focusNode || v == focusNode) return parent; 合并检查: if (null == focusNode || v == focusNode) return parent; .
Do not assign values to a variable that contradict its meaningful name as you do to parent before recurring on left, rather pass the appropriate value: preorderTraversal(bTree.left(focusNode), v, focusNode); 不要像在parent重复之前那样给变量preorderTraversal(bTree.left(focusNode), v, focusNode);一个与其有意义的名称相矛盾的值,而preorderTraversal(bTree.left(focusNode), v, focusNode);传递左值,而是传递适当的值: preorderTraversal(bTree.left(focusNode), v, focusNode); .
Where you proceed differently in different parts of a conditional statement without undisputable and blatantly obvious reason , put a comment: why are you passing parent when recurring on right? 如果您在条件陈述的不同部分进行不同的处理而没有无可争辩且显而易见的理由 ,请发表评论:为什么在正确地重复发生时您要通过父母?
(One up for quoting the method comments. Consider upgrading them to javadoc .) (一个引用方法注释的方法。考虑将其升级为javadoc 。)

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

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