基于字符串的二叉树和插入

时间:2018-11-16 05:04:05

标签: java data-structures binary-tree

二叉树不使用比较,而是用户输入要添加左或右子节点的节点的字符串name,如果该节点已有两个子节点的子节点,它将不要覆盖它。

我遇到了一些困难,它并不能阻止它覆盖先前存在的Node,而且它并不总是记住自己的孩子。

请告诉我我是否错过了一些简单的事情,或者是否需要重做所有内容?如果是,那么这次应该怎么做。

感谢您的高级帮助。

public class Node {

   private String name;
   private Node leftChild;
   private Node rightChild;
   private Node parent;

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public Node getLeftChild() {
       return leftChild;
   }

   public void setLeftChild(Node leftChild) {
       this.leftChild = leftChild;
   }

   public Node getRightChild() {
       return rightChild;
   }

   public void setRightChild(Node rightChild) {
       this.rightChild = rightChild;
   }

   public Node getParent() {
       return parent;
   }

   public void setParent(Node parent) {
       this.parent = parent;
   }

   public void displayNode() // display ourselves{
      System.out.println(name);
   }

}

public class Tree {
   private Node root;

   public Tree() {
       root = null;
   }

   public void insertRoot(String rootName) {
       if (root == null) {
          Node newNode = new Node();
          newNode.setName(rootName);
          root = newNode;
       } else {
          System.out.println("There is already a root");
       }
   }

   public void insertLeftChild(String parentName, String childName) {
      Node temp = new Node();
      Node current = parent(parentName, root, temp);

     if (current.getName() == null) {
        System.out.println("No such parent exists");
     } else if (current.getLeftChild() == null) {

        Node newNode = new Node();
        newNode.setName(childName);

        current.displayNode();

        newNode.setParent(current);

        current.setLeftChild(newNode);
        System.out.println("It worked");
     } else if (current.getLeftChild() != null) {
        System.out.println("They already have a left child");
     }
  }

  public void insertRightChild(String parentName, String childName) {
     Node temp = new Node();

     Node current = parent(parentName, root, temp);

     if (current.getName() == null) {
        System.out.println("No such parent exists");
     } else if (current.getRightChild() == null) {
        Node newNode = new Node();
        newNode.setName(childName);

        newNode.setParent(current);
        current.setRightChild(newNode);
     } else if (current.getRightChild() != null) {
        System.out.println("They already have a right child");
     }
  }

  public Node parent(String parentName, Node current, Node found) {
     if (current != null) {
        if (current.getName().equals(parentName)) {
           found.setName(parentName);
           found.setParent(current.getParent());
           found.setLeftChild(current.getLeftChild());
           found.setRightChild(current.getRightChild());

           return found;
        }

        parent(parentName, current.getLeftChild(), found);
        parent(parentName, current.getRightChild(), found); // right child  
     }
     return found;
  }
}

这是主要方法

public class Demo {

public static void main(String[] args) {

    Tree t = new Tree();

    t.insertRoot("1");

    t.insertLeftChild("1", "2");

    t.insertLeftChild("2", "3");

    t.insertLeftChild("3", "4");

    t.insertLeftChild("1", "3");

    t.insertRightChild("7", "8");   
 }
}

这是当前结果

1 有效 没有这样的父母 没有这样的父母 1个 有效

“有效”是程序是否完成左侧插入的标记 “ 1”显示新插入要添加到的父节点的Node值

1 个答案:

答案 0 :(得分:0)

我已经更新了查找父节点的逻辑。如果不存在,它将返回null,否则将返回父节点。

public Node parent(String parentName, Node root) {
    if (root != null) {
        if (root.getName().equals(parentName)) {
            return root;
        } else {
            Node found = parent(parentName, root.getLeftChild());
            if (found == null) {
                found = parent(parentName, root.getRightChild());
            }
            return found;
        }
    } else {
        return null;
    }
}

完整代码。

class Node {

private String name;
private Node leftChild;
private Node rightChild;
private Node parent;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Node getLeftChild() {
    return leftChild;
}

public void setLeftChild(Node leftChild) {
    this.leftChild = leftChild;
}

public Node getRightChild() {
    return rightChild;
}

public void setRightChild(Node rightChild) {
    this.rightChild = rightChild;
}

public Node getParent() {
    return parent;
}

public void setParent(Node parent) {
    this.parent = parent;
}

public void displayNode() {

    System.out.println(name);
}

}



public class Tree {

private Node root;

public Tree() {
    root = null;
}

public void insertRoot(String rootName) {
    if (root == null) {
        Node newNode = new Node();
        newNode.setName(rootName);
        root = newNode;
    } else {
        System.out.println("There is already a root");
    }
}

public void insertLeftChild(String parentName, String childName) {
    Node parent = parent(parentName, root);

    if (parent == null) {
        System.out.println("No such parent exists");
    } else if (parent.getLeftChild() == null) {

        Node newNode = new Node();
        newNode.setName(childName);

        parent.displayNode();

        newNode.setParent(parent);

        parent.setLeftChild(newNode);
        System.out.println("It worked");
    } else if (parent.getLeftChild() != null) {
        System.out.println("They already have a left child");
    }
}

public void insertRightChild(String parentName, String childName) {

    Node parent = parent(parentName, root);

    if (parent == null) {
        System.out.println("No such parent exists");
    } else if (parent.getRightChild() == null) {
        Node newNode = new Node();
        newNode.setName(childName);

        newNode.setParent(parent);
        parent.setRightChild(newNode);
    } else if (parent.getRightChild() != null) {
        System.out.println("They already have a right child");
    }
}

public Node parent(String parentName, Node root) {
    if (root != null) {
        if (root.getName().equals(parentName)) {
            return root;
        } else {
            Node found = parent(parentName, root.getLeftChild());
            if (found == null) {
                found = parent(parentName, root.getRightChild());
            }
            return found;
        }
    } else {
        return null;
    }
}

public static void main(String[] args) {

    Tree t = new Tree();

    t.insertRoot("1");

    t.insertLeftChild("1", "2");

    t.insertLeftChild("2", "3");

    t.insertLeftChild("3", "4");

    t.insertLeftChild("1", "3");

    t.insertRightChild("7", "8");
}
}

输出..

1
It worked
2
It worked
3
It worked
They already have a left child
No such parent exists