Пример #1
0
 public static BiTreeNode newNode(int data) {
   BiTreeNodeData d = new BiTreeNodeData(data);
   BiTreeNode t = new BiTreeNode();
   t.setData(d);
   t.setLeftchild(null);
   t.setRightchild(null);
   return t;
 }
Пример #2
0
  public static void testInOrderTraverse() {
    BiTreeNodeData a = new BiTreeNodeData("A");
    BiTreeNodeData b = new BiTreeNodeData("B");
    BiTreeNodeData c = new BiTreeNodeData("C");
    BiTreeNodeData d = new BiTreeNodeData("D");

    BiTreeNode A = new BiTreeNode();
    A.setData(a);
    BiTreeNode B = new BiTreeNode();
    B.setData(b);
    BiTreeNode C = new BiTreeNode();
    C.setData(c);
    BiTreeNode D = new BiTreeNode();
    D.setData(d);
    A.setLeftchild(B);
    A.setRightchild(C);
    B.setRightchild(D);

    BiTree.inOrderTraverse(A);
  }
Пример #3
0
 public BiTreeNode preOrderCreateBiTree(String init, BiTreeNode t) {
   if ("#".equalsIgnoreCase(init.substring(0, 1))) {
     t = null;
   } else {
     BiTreeNodeData d = new BiTreeNodeData(init.substring(0, 1));
     t = new BiTreeNode();
     t.setData(d);
     t.setLeftchild(preOrderCreateBiTree(init.substring(1), t.getLeftchild()));
     t.setRightchild(preOrderCreateBiTree(init.substring(1), t.getRightchild()));
   }
   return t;
 }
Пример #4
0
 /*
  Give a binary search tree and a number, inserts a new node
  with the given number in the correct place in the tree.
  Returns the new root pointer which the caller should
  then use (the standard trick to avoid using reference
  parameters).
 */
 public static BiTreeNode insert(BiTreeNode node, int data) {
   if (null == node) {
     return newNode(data);
   } else {
     if (data <= node.getData().getIdata()) {
       node.setLeftchild(insert(node.getLeftchild(), data));
     } else {
       node.setRightchild(insert(node.getRightchild(), data));
     }
     // return the (unchanged) node pointer
     return node;
   }
 }