Example #1
0
  public static void insert(TreeNode tnode, int x, TreeNode p) {
    if (tnode == null) {
      tnode = new TreeNode();
      tnode.value = x;
      tnode.lchild = null;
      tnode.rchild = null;
      if (p.value > x) p.lchild = tnode;
      else p.rchild = tnode;
      tnode.parent = p;
      return;
    }

    if (x < tnode.value) {
      insert(tnode.lchild, x, tnode);
    } else {
      insert(tnode.rchild, x, tnode);
    }
  }