package com.redis.manyredis.tree.binarySortTree;
/**
* @Desoription
* @Author jinghong.lu
* @Date
**/
public class BinarySortTreeTest {
public static void main(String[] args) {
//跟结点
BinarySortTree root = new BinarySortTree();
root.setData(62);
//跟结点左子树
BinarySortTree lchild58 = new BinarySortTree();
lchild58.setData(58);
root.setLchild(lchild58);
//58左右子树结点
BinarySortTree lchild47 = new BinarySortTree();
lchild47.setData(47);
lchild58.setLchild(lchild47);
//47左右子树结点
BinarySortTree lchild35 = new BinarySortTree();
lchild35.setData(35);
BinarySortTree lrhild51 = new BinarySortTree();
lrhild51.setData(51);
lchild47.setLchild(lchild35);
lchild47.setRchild(lrhild51);
//35右子树
BinarySortTree lrhild37 = new BinarySortTree();
lrhild37.setData(37);
lchild35.setRchild(lrhild37);
//跟结点右子树
BinarySortTree rchild88 = new BinarySortTree();
rchild88.setData(88);
root.setRchild(rchild88);
//88左右子树结点
BinarySortTree lchild73 = new BinarySortTree();
lchild73.setData(73);
BinarySortTree lrhild99 = new BinarySortTree();
lrhild99.setData(99);
rchild88.setLchild(lchild73);
rchild88.setRchild(lrhild99);
//99左子树
BinarySortTree lchild93 = new BinarySortTree();
lchild93.setData(93);
lrhild99.setLchild(lchild93);
System.out.println("------------------------原来二叉排序树------------------------------");
System.out.println(root.toString());
System.out.println("------------------------原来二叉排序树------------------------------");
int[] a = {62,88,58,47,35,73,51,99,37,93};
BinarySortTree binarySortTree = createSortTree(a);
System.out.println(binarySortTree.toString());
int count = 0;
//查找
findKey(root, 37, count);
}
/**
* 创建二叉排序树
*/
public static BinarySortTree createSortTree(int[] intArray) {
BinarySortTree binarySortTree = new BinarySortTree();
binarySortTree.setData(intArray[0]);
for (int i = 1; i < intArray.length; i++) {
createTreeNode(binarySortTree,intArray[i]);
}
return binarySortTree;
}
public static void createTreeNode(BinarySortTree binarySortTree, int data) {
int tempDate = binarySortTree.getData();
BinarySortTree lchild = binarySortTree.getLchild();//左孩子
BinarySortTree rchild = binarySortTree.getRchild();//右孩子
if (tempDate > data) {
if(lchild == null){
lchild = new BinarySortTree();
lchild.setData(data);
binarySortTree.setLchild(lchild);
return;
}
//左孩子
createTreeNode(lchild,data);
}else{
if(rchild == null){
rchild = new BinarySortTree();
rchild.setData(data);
binarySortTree.setRchild(rchild);
return;
}
//右孩子
createTreeNode(rchild,data);
}
}
public static void findKey(BinarySortTree sortTree, int key, int count) {
count++;
BinarySortTree lchild = sortTree.getLchild();//左孩子
BinarySortTree rchild = sortTree.getRchild();//右孩子
int data = sortTree.getData();//数据
if (key == data) {
System.out.println("查找到了key:" + data + ",查找了:" + count + "次");
return;
}
if (lchild == null && rchild == null) {
System.out.println("没有子结点,改树中没有关键字,跑了" + count + "次");
return;
}
if (key > data) {
//拿右子树
findKey(rchild, key, count);
} else {
//拿左子树
findKey(lchild, key, count);
}
}
}
把数据结构的二叉排序树-根据java版本编写了一遍。有优化的地方望大佬指点