二叉树查找java_查找二叉树之JAVA实现

//树的节点类

class BinaryNode {

T element;

BinaryNode left;

BinaryNode right;

public BinaryNode(T theElement) {

element = theElement;

}

public BinaryNode(T theElement, BinaryNode left, BinaryNode right) {

this.element = theElement;

this.left = left;

this.right = right;

}

}

//查找二叉树

public class BinarySearchTree {

//私有属性

private BinaryNode root;//根节点

//构造方法

BinarySearchTree() {

this.root = null;

}

//置空操作

public void makeEmpty() {

this.root = null;

}

//判断是否为空

public boolean isEmpty() {

return root == null;

}

//判断是否包含某节点

public boolean contains(T x) {

return contains(x, root);

}

//查找最小的元素

public T findMin() {

if (isEmpty())

throw new RuntimeException("searchTree is empty");

return findMin(root).element;

}

//查找最大的元素

public T findMax() {

if (isEmpty())

throw new RuntimeException("searchTree is empty");

return findMax(root).element;

}

//插入操作

public void insert(T x) {

this.root = insert(x, root);

}

//删除操作

public void remove(T x) {

this.root = remove(x, root);

}

//获取树的最大深度

public int maxDeep() {

return maxDeep(root);

}

//获取树的最小深度

public int minDeep() {

return minDeep(root);

}

//先序打印操作

public void prePrintTree() {

prePrintTree(root);

System.out.println();

}

//中序打印操作

public void infPrintTree() {

infPrintTree(root);

System.out.println();

}

//后序打印操作

public void epiPrintTree() {

epiPrintTree(root);

System.out.println();

}

//判断是否是AVL树

public boolean isAVL() {

return Math.abs(maxDeep(root.right) - maxDeep(root.left)) < 2;

}

public boolean contains(T x, BinaryNode t) {

if (t == null)

return false;

int compareResult = x.compareTo(t.element);

if (compareResult < 0)

return contains(x, t.left);

else if (compareResult > 0)

return contains(x, t.right);

else

return true;

}

public BinaryNode findMin(BinaryNode t) {

if (t == null)

throw new RuntimeException("this binaryNode is Empty");

if (t.left == null)

return t;

return findMin(t.left);

}

public BinaryNode findMax(BinaryNode t) {

if (t == null)

throw new RuntimeException("this binaryNode is Empty");

if (t.right == null)

return t;

return findMax(t.right);

}

public BinaryNode insert(T x, BinaryNode t) {

if (t == null)//传入节点为空,则将该节点作为根节点返回

return new BinaryNode<>(x, null, null);

int compareResult = x.compareTo(t.element);

if (compareResult < 0)//传入元素x小于t

t.left = insert(x, t.left);

else if (compareResult > 0)//传入元素x大于t

t.right = insert(x, t.right);

return t;

}

public BinaryNode remove(T x, BinaryNode t) {

if (!contains(x, root))

throw new RuntimeException("this searchTree is not contains element:" + x);

if (t == null)//传入节点为空

throw new RuntimeException("this binaryNode is empty");

int compareResult = x.compareTo(t.element);

if (compareResult < 0)

t.left = remove(x, t.left);

else if (compareResult > 0)

t.right = remove(x, t.right);

else if (t.left != null && t.right != null) { //有两个儿子

t.element = findMax(t.left).element;

remove(t.element, t.left);

} else //单儿子情形

t = t.left == null ? t.right : t.left;

return t;

}

public int maxDeep(BinaryNode t) {

int dl, dr;//记录左右树的深度

if (t == null)

return 0;

else {

dl = maxDeep(t.left);

dr = maxDeep(t.right);

}

return dl > dr ? dl + 1 : dr + 1;

}

public int minDeep(BinaryNode t) {

int dl, dr;//记录左右树的深度

if (t == null)

return 0;

else {

dl = maxDeep(t.left);

dr = maxDeep(t.right);

}

return dl < dr ? dl + 1 : dr + 1;

}

public void prePrintTree(BinaryNode t) {

if (t == null)

return;

System.out.print(t.element + " ");

prePrintTree(t.left);

prePrintTree(t.right);

}

public void infPrintTree(BinaryNode t) {

if (t == null)

return;

infPrintTree(t.left);

System.out.print(t.element + " ");

infPrintTree(t.right);

}

public void epiPrintTree(BinaryNode t) {

if (t == null)

return;

epiPrintTree(t.left);

epiPrintTree(t.right);

System.out.print(t.element + " ");

}

//测试方法

public static void main(String[] args) {

BinarySearchTree bst = createBinarySearchTree();

System.out.println("先序打印出该树===>>>");

bst.prePrintTree();

System.out.println("中序打印出该树===>>>");

bst.infPrintTree();

System.out.println("后序打印出该树===>>>");

bst.epiPrintTree();

System.out.println("查找最大值===>>>" + bst.findMax());

System.out.println("查找最小值===>>>" + bst.findMin());

System.out.println("树中是否包含元素:" + bst.contains(5));

System.out.println("树的最大深度" + bst.maxDeep());

System.out.println("树的最大深度" + bst.minDeep());

System.out.println("删除某一元素===>>>");

bst.remove(6);

bst.prePrintTree();

bst.makeEmpty();

System.out.println(bst.maxDeep());

}

/** * 创造一个如下的查找二叉树: * 6 * / \ * 2 8 * / \ * 1 4 * / * 3 * * */

private static BinarySearchTree createBinarySearchTree() {

BinarySearchTree bst = new BinarySearchTree();

bst.insert(6);

bst.insert(2);

bst.insert(1);

bst.insert(4);

bst.insert(3);

bst.insert(8);

return bst;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值