二叉排序树JAVA的实现
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
System.out.print(" ");
}
Fra Baidu bibliotek} }
}
//打印二叉树 public void BSTreedisplay(BSTreeNode t, int h) { for (int k = 0; k < h; k++) System.out.print(" "); if (t == null) { System.out.print("Nil"); } else {
System.out.println("(" + t.data + ","); BSTreedisplay(t.lchild, h + 1); System.out.print(","); System.out.println();
BSTreedisplay(t.rchild, h + 1); System.out.print(")"); System.out.println(); for (int k = 0; k < h - 1; k++)
// 查找 public BSTreeNode SearchNode(BSTreeNode node, int data) { if (node == null) return null; else if (node.data == data) { // System.out.println(node.data); return node; } else if (node.data > data) return SearchNode(node.lchild, data); else return SearchNode(node.rchild, data);
public class BSTreeNode { int data; BSTreeNode lchild; BSTreeNode rchild;
}
public class BSTree { BSTreeNode root = null;
// 插入节点 public void InsertNode(BSTree T, int data) { BSTreeNode node = new BSTreeNode(); BSTreeNode y = null, x; node.data = data; node.lchild = null; node.rchild = null; x = root; while (x != null) { y = x; if (x.data > node.data) x = x.lchild; else x = x.rchild; } if (y == null) T.root = node; else if (y.data > node.data) y.lchild = node; else y.rchild = node; }
}
Fra Baidu bibliotek} }
}
//打印二叉树 public void BSTreedisplay(BSTreeNode t, int h) { for (int k = 0; k < h; k++) System.out.print(" "); if (t == null) { System.out.print("Nil"); } else {
System.out.println("(" + t.data + ","); BSTreedisplay(t.lchild, h + 1); System.out.print(","); System.out.println();
BSTreedisplay(t.rchild, h + 1); System.out.print(")"); System.out.println(); for (int k = 0; k < h - 1; k++)
// 查找 public BSTreeNode SearchNode(BSTreeNode node, int data) { if (node == null) return null; else if (node.data == data) { // System.out.println(node.data); return node; } else if (node.data > data) return SearchNode(node.lchild, data); else return SearchNode(node.rchild, data);
public class BSTreeNode { int data; BSTreeNode lchild; BSTreeNode rchild;
}
public class BSTree { BSTreeNode root = null;
// 插入节点 public void InsertNode(BSTree T, int data) { BSTreeNode node = new BSTreeNode(); BSTreeNode y = null, x; node.data = data; node.lchild = null; node.rchild = null; x = root; while (x != null) { y = x; if (x.data > node.data) x = x.lchild; else x = x.rchild; } if (y == null) T.root = node; else if (y.data > node.data) y.lchild = node; else y.rchild = node; }