二叉排序树的基本操作

来源:互联网 发布:彩票代买软件 编辑:程序博客网 时间:2024/04/30 03:18
package datastructure;class BSTree{    int data;    BSTree leftchild;    BSTree rightchild;}class BST{    //搜索    public BSTree search(BSTree tree, int key){        if(tree == null)            return null;        if(tree.data == key)            return tree;        if(key < tree.data)            return search(tree.leftchild, key);        else            return search(tree.rightchild, key);    }    public BSTree search2(BSTree tree, int key){        while(tree != null){            if(tree.data == key)                return tree;            if(key < tree.data)                tree = tree.leftchild;            else                tree = tree.rightchild;        }        return null;    }    //插入 建立    //递归    public BSTree insert(BSTree root, int key){        if(root == null){            root =  new BSTree();            root.data = key;            root.leftchild = null;            root.rightchild = null;        }        else if(key < root.data)            root.leftchild = insert(root.leftchild, key);        else if(key > root.data)            root.rightchild = insert(root.rightchild, key);        return root;    }    //迭代    public BSTree insert2(BSTree root, int key){        BSTree parent = null, tree = root;        while(tree != null){            if(tree.data == key)                return tree;            parent = tree;            tree = (key < tree.data) ? tree.leftchild : tree.rightchild;        }        tree = new BSTree();        tree.data = key;        tree.leftchild = tree.rightchild = null;        if(root == null){            root = tree;            return root;        }        if(key < parent.data)            parent.leftchild = tree;        else            parent.rightchild = tree;        return root;    }    //查找要删除节点    public boolean deleteBSTreeNode(BSTree root, int key){        if(root == null)            return false;        if(key == root.data)            return delete(root);        else if(key < root.data)            return deleteBSTreeNode(root.leftchild, key);        else            return deleteBSTreeNode(root.rightchild, key);    }    //执行删除    public boolean delete(BSTree p){        BSTree q = null, s = null;        if(p.leftchild == null && p.rightchild == null){//叶子节点            p = null;        }else if(p.rightchild == null){//只有左子树            q = p;            p = p.leftchild;            q = null;        }else if(p.leftchild == null){//只有右子树            q = p;            p = p.rightchild;            q = null;        }else{//左右子树都不为空            q = p;            s = p.leftchild;            while(s.rightchild != null){                q = s;                s = s.rightchild;            }            p.data = s.data;            if(q != p)                q.rightchild = s.leftchild;            else                q.leftchild = s.leftchild;            s = null;        }        return true;    }    //初始化二叉排序树    public BSTree initBSTree(){        BSTree root = null;        int[] data = {40, 20, 60, 10, 30, 50, 70, 45, 55, 52};        for(int i = 0; i < data.length; i++){        //  root = insert2(root, data[i]);            root = insert(root, data[i]);        }        return root;    }    //中序遍历    public void inorder(BSTree tree){        if(tree != null){            inorder(tree.leftchild);            System.out.print(tree.data + " ");            inorder(tree.rightchild);        }    }}public class SearchTree1 {    public static void main(String[] args){        BST bst = new BST();        BSTree tree = bst.initBSTree();        System.out.println(tree);        System.out.println("初始化二叉查找树后为:");        bst.inorder(tree);        System.out.println();        int key = 60;        BSTree searchNode = bst.search(tree, key);        if(searchNode != null)            System.out.println("查找key " + key +" 节点:" + searchNode + " data = " + searchNode.data);        else            System.out.println("没有找到 " + key);        boolean flag = bst.deleteBSTreeNode(tree, key);        if(flag){            System.out.println("删除 " + key +" 后:");            bst.inorder(tree);        }else            System.out.println("没有值为 " + key + " 的节点!");    }}
0 0