数据结构--树(JAVA实现代码)


二叉搜索树

class BSTTree<T extends Comparable<T>>{
//节点类
    private class Node
    {
        private T value;
        private Node left;
        private Node right;
        Node(T val)
        {
            this.value=val;
            this.left=null;
            this.right=null;
        }
    }
    private Node root;
    private int height;
    //前序,中序,后序遍历
    private void preOrder(Node t){
        if(t==null)
            return;
        System.out.print(t.value+" ");
        preOrder(t.left);
        preOrder(t.right);
    }
    private void inOrder(Node t){
        if(t==null)
            return;
        inOrder(t.left);
        System.out.print(t.value+" ");
        inOrder(t.right);
    }
    private void postOrder(Node t){
        if(t==null)
            return;
        postOrder(t.left);
        postOrder(t.right);
        System.out.print(t.value+" ");
    }
    //插入,删除,查找
    private Node insert(Node t,T data){
        if(t==null)
        {
            t=new Node(data);
            return t;
        }
        else if(t.value.compareTo(data)<0)
            t.right=insert(t.right,data);
        else if(t.value.compareTo(data)>0)
            t.left=insert(t.left,data);
        return t;
    }
    private Node del(Node t,T data){
        if(t==null)
            return t;
        else if(t.value.compareTo(data)<0)
            t.right=del(t.right,data);
        else if(t.value.compareTo(data)>0)
            t.left=del(t.left,data);
        else
        {
            return delete_(t);
        }
        return t;
    }
    private Node delete_(Node t)
    {
        if(t.left==null&&t.right==null)
            return null;
        else if(t.left==null)
            return t.right;
        else if(t.right==null)
            return t.left;
        else
        {
            Node temp=t;
            Node s=t.right;
            while(s.left!=null)
            {
                temp=s;
                s=s.left;
            }
            if(temp==t)
                temp.right=s.right;
            else
                temp.left=s.right;
            t.value=s.value;
            return t;
        }
    }
    private boolean search(Node t,T data){
        if(t==null)
            return false;
        if(t.value.compareTo(data)<0)
            return search(t.right,data);
        else if(t.value.compareTo(data)>0)
            return search(t.left,data);
        return true;
    }
    //获得高度
    private void getHeight(Node t,int h)
    {
        if(t==null)
            return;
        ++h;
        if(h>this.height)
            this.height=h;
        getHeight(t.left,h);
        getHeight(t.right,h);
    }
    public BSTTree(){
        root=null;
        height=0;
    }
    public void preOrder(){
        preOrder(root);
        System.out.println();
    }
    public void inOrder(){
        inOrder(root);
        System.out.println();
    }
    public void postOrder(){
        postOrder(root);
        System.out.println();
    }
    //层序遍历
    public void levelOrder(){
        if(root!=null)
        {
            Queue<Node>queue=new LinkedList<>();
            queue.add(root);
            while(!queue.isEmpty()){
                Node temp=queue.peek();
                queue.poll();
                System.out.print(temp.value+" ");
                if(temp.left!=null)
                    queue.add(temp.left);
                if(temp.right!=null)
                    queue.add(temp.right);
            }
        }
        System.out.println();
    }
    public void Insert(T data){
        root=insert(root,data);
    }
    public void Delete(T data){
        root=del(root,data);
    }
    public boolean Search(T data){
        return search(root,data);
    }
    public int getHeight(){
        this.height=0;
        getHeight(root,0);
        return this.height;
    }
    //获得迭代器
    public Iterator<T> iterator(){
        return new InorderIterator();
    }
    //这里体现了内部类的特点,可以获取BSTTree类的内容,并对其进行修改
    private class InorderIterator implements Iterator<T>{
        private ArrayList<T>list=new ArrayList<>();
        private int current=0;
        public InorderIterator(){
            inorder();
        }
        private void inorder(){
            inorder(root);
        }
        private void inorder(Node t){
            if(t==null)
                return;
            inorder(t.left);
            list.add(t.value);
            inorder(t.right);
        }
        //因为继承Iterator接口,故需要实现以下三个方法
        @Override
        public boolean hasNext(){
            return current<list.size();
        }
        @Override
        public T next(){
            return list.get(current++);
        }
        @Override
        public void remove(){
            Delete(list.get(current));
            list.clear();
            inorder(root);
        }
        public void clear(){
            root=null;
            list=null;
            height=0;
        }
    }
}
public class test{
    public static void main(String[]args)throws Exception {
        BSTTree<Integer>tree=new BSTTree<>();
        for(int i=0;i<20;++i)
            tree.Insert(new Integer((int)(Math.random()*1000)));
        tree.Insert(100);
        tree.preOrder();
        tree.inOrder();
        tree.postOrder();
        tree.levelOrder();
        System.out.println(tree.getHeight());
        if(tree.Search(100)){
            tree.Delete(100);
        }
        tree.preOrder();
        tree.inOrder();
        tree.postOrder();
        tree.levelOrder();
        System.out.println(tree.getHeight());
        Iterator<Integer>iterator=tree.iterator();
       while(iterator.hasNext()){
           System.out.print(iterator.next()+" ");
       }
       System.out.println();
    }
}  

AVL树

class AVLTree<T extends  Comparable<T>>
{
    private class Node
    {
        private T value;
        private Node left;
        private Node right;
        private int h;
        private int n;
        public Node(T data){
            this.value=data;
            this.left=null;
            this.right=null;
            this.n=1;
            this.h=0;
        }
    }
    private Node root;
    //获得最大值
    private int max(int a,int b){
        return a>b?a:b;
    }
    //获得高度
    private int height(Node t){
        if(t==null)
            return -1;
        return t.h;
    }
    //获得大小
    private int size(Node t){
        if(t==null)
            return 0;
        return t.n;
    }
    //左转
    private Node rotateLeft(Node t){
        Node temp=t.right;
        t.right=temp.left;
        temp.left=t;
        t.h=1+max(height(t.left),height(t.right));
        t.n=1+size(t.left)+size(t.right);
        temp.h=1+max(height(temp.left),height(temp.right));
        temp.n=1+size(temp.left)+size(temp.right);
        return temp;
    }
    //右转
    private Node rotateRight(Node t){
        Node temp=t.left;
        t.left=temp.right;
        temp.right=t;
        t.h=1+max(height(t.left),height(t.right));
        t.n=1+size(t.left)+size(t.right);
        temp.h=1+max(height(temp.left),height(temp.right));
        temp.n=1+size(temp.left)+size(temp.right);
        return temp;
    }
    //平衡因子
    private int balanceFactor(Node t){
        if(t==null)
            return 0;
        return height(t.left)-height(t.right);
    }
    //平衡
    private Node Balance(Node t)
    {
        if(balanceFactor(t)<-1)
        {
            if(balanceFactor(t.right)>0)
                t.right=rotateRight(t.right);
           t=rotateLeft(t);
        }
        else if(balanceFactor(t)>1)
        {
            if(balanceFactor(t.left)<0)
                t.left=rotateLeft(t.left);
            t=rotateRight(t);
        }
        return t;
    }
    //遍历
    private void print(Node t){
        if(t==null)
            return;
        System.out.print(t.value+" ");
        print(t.left);
        print(t.right);
    }
    //插入
    private Node insert(Node t,T data)
    {
        if(t==null)
        {
            t=new Node(data);
            return t;
        }
        else if(t.value.compareTo(data)<0)
        {
            t.right=insert(t.right,data);
        }
        else if(t.value.compareTo(data)>0)
        {
            t.left=insert(t.left,data);
        }
        else
            return t;
            //更新t的高度和大小,再进行平衡
        t.h=1+max(height(t.left),height(t.right));
        t.n=1+size(t.left)+size(t.right);
        return Balance(t);
    }
    //获得最小值
    private T findMin(Node t){
        if(t.left==null)
            return t.value;
        return findMin(t.left);
    }
    //删除
    private Node del(Node t,T data)
    {
        if(t==null)
            return t;
        if(t.value.compareTo(data)<0)
            t.right=del(t.right,data);
        else if(t.value.compareTo(data)>0)
            t.left=del(t.left,data);
        else
        {
            if(t.left==null&&t.right==null)
                return null;
            else if(t.left==null)
                return t.right;
            else if(t.right==null)
                return t.left;
            else
            {
                t.value=findMin(t.right);
                t.right=del(t.right,t.value);
            }
        }
        t.h=1+max(height(t.left),height(t.right));
        t.n=1+size(t.left)+size(t.right);
        return Balance(t);
    }
    //查找
    private boolean search(Node t,T data){
        if(t==null)
            return false;
        else if(t.value.compareTo(data)<0)
            return search(t.right,data);
        else if(t.value.compareTo(data)>0)
            return search(t.left,data);
        else
            return true;
    }
    public AVLTree(){
        root=null;
    }
    public void Insert(T data){
        root=insert(root,data);
    }
    public void Delete(T data){
        root=del(root,data);
    }
    public boolean Search(T data){
        return search(root,data);
    }
    public void Print(){
        print(root);
        System.out.println();
    }
    public int Height(){
        return height(root);
    }
    public int Size(){
        return size(root);
    }
    //获得迭代器
    public Iterator<T> iterator(){
        return new InorderIterator();
    }
    private class InorderIterator implements Iterator<T>{
        private ArrayList<T>list=new ArrayList<>();
        private int current=0;
        public InorderIterator(){
            inorder();
        }
        private void inorder(){
            inorder(root);
        }
        private void inorder(Node t){
            if(t==null)
                return;
            inorder(t.left);
            list.add(t.value);
            inorder(t.right);
        }
        @Override
        public boolean hasNext(){
            return current<list.size();
        }
        @Override
        public T next(){
            return list.get(current++);
        }
        @Override
        public void remove(){
            Delete(list.get(current));
            list.clear();
            inorder(root);
        }
        public void clear(){
            root=null;
            list.clear();
            current=0;
        }
    }
}
public class test{
    public static void main(String[]args)throws Exception {
        AVLTree<Integer>tree=new AVLTree<>();
        tree.Insert(10);
        tree.Insert(20);
        tree.Insert(30);
        tree.Print();
        System.out.println(tree.Height()+" "+tree.Size());
        tree.Insert(40);
        tree.Insert(50);
        tree.Insert(60);
        tree.Print();
        System.out.println(tree.Height()+" "+tree.Size());
        tree.Insert(70);
        tree.Insert(80);
        tree.Insert(90);
        tree.Print();
        System.out.println(tree.Height()+" "+tree.Size());
        Iterator<Integer>iterator= tree.iterator();
        while(iterator.hasNext())
            System.out.print(iterator.next()+" ");
        System.out.println();
        tree.Delete(40);
        tree.Print();
        System.out.println(tree.Height()+" "+tree.Size());

    }
}

伸展树

class SplayTree <T extends Comparable<T>>
{
    private class Node
    {
        private T value;
        private Node left;
        private Node right;
        private Node parent;
        Node(T data){
            this.value=data;
            this.left=this.right=this.parent=null;
        }
    }
    private Node root;
    //旋转
    private Node Zig(Node t,Node x)
    {
        if(t==null||x==null)
            return t;
        else if(t==x)
            return t;
        Node parent=x.parent;
        Node grand=parent.parent;
        if(x.value.compareTo(parent.value)<0)
        {
            parent.left=x.right;
            if(x.right!=null)
                x.right.parent=parent;
            x.right=parent;
            parent.parent=x;
            x.parent=grand;
        }
        else
        {
            parent.right=x.left;
            if(x.left!=null)
                x.left.parent=parent;
            x.left=parent;
            parent.parent=x;
            x.parent=grand;
        }
        if(grand==null)
            t=x;
        else {
            if (x.value.compareTo(grand.value) < 0)
                grand.left = x;
            else
                grand.right = x;
        }
        return t;
    }
    //一字型旋转
    private Node Zig_Zig(Node t,Node x){
        t=Zig(t,x.parent);
        t=Zig(t,x);
        return t;
    }
    //之字型旋转
    private Node Zig_Zag(Node t,Node x){
        t=Zig(t,x);
        t=Zig(t,x);
        return t;
    }
    //上升
    private Node up(Node t,Node x){
        if(t==null||x==null||t==x)
            return t;
        else if(x.parent==t)
        {
            return t;
        }
        else
        {
            Node parent=x.parent;
            if(parent.value.compareTo(t.value)<0)
            {
                if(x.value.compareTo(parent.value)<0)
                    t=Zig_Zig(t,x);
                else
                    t=Zig_Zag(t,x);
            }
            else
            {
                if(x.value.compareTo(parent.value)>0)
                    t=Zig_Zig(t,x);
                else
                    t=Zig_Zag(t,x);
            }
        }
        return t;
    }
    //查找
    private Node search(Node t,T data){
        if(t==null)
            return t;
        else if(t.value.compareTo(data)<0)
            return search(t.right,data);
        else if(t.value.compareTo(data)>0)
            return search(t.left,data);
        else
            return t;
    }
    //伸展操作,将data所在的节点伸展到根节点
    private Node Splay(Node t,T data){
        if(t==null)
            return t;
        Node x=search(t,data);
        if(x==null||t==x)
            return t;
        else if(x.parent==t)
        {
            return Zig(t,x);
        }
        while(t.left!=x&&t.right!=x)
        {
            t=up(t,x);
            if(t==x)
                break;
        }
        return Zig(t,x);
    }
    //插入
    private Node insert(Node t,T data){
        if(t==null) {
            t = new Node(data);
            return t;
        }
        Node temp=t;
        while(true){
            if(temp.value.compareTo(data)<0)
            {
                if(temp.right!=null)
                    temp=temp.right;
                else
                {
                    Node pnew=new Node(data);
                    pnew.parent=temp;
                    temp.right=pnew;
                    break;
                }
            }
            else if(temp.value.compareTo(data)>0){
                if(temp.left!=null)
                    temp=temp.left;
                else
                {
                    Node pnew=new Node(data);
                    pnew.parent=temp;
                    temp.left=pnew;
                    break;
                }
            }
            else
                break;
        }
        return Splay(t,data);
    }
    //查找最小值
    private T findMin(Node t)
    {
        if(t.left==null)
            return t.value;
        return findMin(t.left);
    }
    private void del(Node t,T data){
        if(t==null)
            return;
        else if(t.value.compareTo(data)<0)
           del(t.right,data);
        else if(t.value.compareTo(data)>0)
            del(t.left,data);
        else
        {
            if(t.left==null&&t.right==null) {
                Node parent=t.parent;
                if(parent!=null)
                {
                    if(parent.value.compareTo(t.value)<0)
                        parent.right=null;
                    else
                        parent.left=null;
                }
                t=null;
            }
            else if(t.left==null)
            {
                Node rt=t.right;
                Node parent=t.parent;
                rt.parent=parent;
                if(parent.value.compareTo(rt.value)<0)
                    parent.right=rt;
                else
                    parent.left=rt;
            }
            else if(t.right==null)
            {
                Node lt=t.left;
                Node parent=t.parent;
                lt.parent=parent;
                if(parent.value.compareTo(lt.value)<0)
                    parent.right=lt;
                else
                    parent.left=lt;
            }
            else
            {
                t.value=findMin(t.right);
                del(t.right,t.value);
            }
        }
    }
    //遍历
    private void print(Node t)
    {
        if(t==null)
            return;
        System.out.print(t.value+" ");
        print(t.left);
        print(t.right);
    }
    public SplayTree()
    {
        root=null;
    }
    public void Insert(T data){
        root=insert(root,data);
    }
    public void Delete(T data){
        del(root,data);
    }
    public boolean Search(T data){
        return search(root,data)!=null;
    }
    public void Print(){
        print(root);
        System.out.println();
    }
    //获得迭代器
    public Iterator<T> iterator(){
        return new InorderIterator();
    }
    private class InorderIterator implements Iterator<T>{
        private ArrayList<T>list=new ArrayList<>();
        private int current=0;
        public InorderIterator()
        {
            inorder();
        }
        private void inorder(){
            inorder(root);
        }
        private void inorder(Node t){
            if(t==null)
                return;
            inorder(t.left);
            list.add(t.value);
            inorder(t.right);
        }
        @Override
        public boolean hasNext(){
            return current< list.size();
        }
        @Override
        public T next(){
            return list.get(current++);
        }
        @Override
        public void remove(){
            Delete(list.get(current));
            list.clear();
            inorder(root);
        }
    }
}
public class test{
    public static void main(String[]args)throws Exception {
        SplayTree<Integer>tree=new SplayTree<>();
        for(int i=0;i<20;++i)
        {
            tree.Insert((int)(Math.random()*1000));
            tree.Print();
        }
        tree.Insert(200);
        tree.Print();
        if(tree.Search(200))
        {
            tree.Delete(200);
            tree.Print();
        }
        Iterator<Integer>iterator=tree.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next()+" ");
        }
        System.out.println();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值