JAVA数据结构实现

JAVA单链表实现

#单链表的数据结构
public singleLinkedList(){
    private int size;
    private Node head;
    
    public singleLInkedList(){
        this.size = 0;
        this.head = Null;
    }
    private class Node(Object data){
        Object data;
        Node next;
        public Node(Object data){
            this.data = data;
        }
        
    }
    //添加头节点
    public Object addHeadNode(Object data){
        Node newHead = new Node(data);
        if(size == 0) head = newHead;
        else{
            newHead.next = head;
            head = neaHead;
        }
        size++;
        return data;
    }
    //在头部删除元素
    public Object deleteHeadNode(){
        Object data = head.data;
        head = head.next;
        size--;
        return data;
    }
    //查找指定元素,找到了返回Node,找不到返回Null
    public Node find(Object data){
        //设定指针遍历
        Node current = head;
        while(current != null){
            if(current.next.data == data) return curren
            else current = current.next;
        }
        return null;
    }
    //删除指定的元素,删除成功返回true
    public boolean delete(Object data){
        Node curren = head;
        while(current.next.data!=data&&current.next!=null){
            current = current.next;
        }
        if(current.next != null){
            current.next = current.next.next;
            size--;
            return true;
        }
        else return false;
    }
    //判断链表是否未空
    public boolean isEmpty(){
        if(size>0) return false;
        else return true;
    }
}

 =================================================

 二叉树

//节点类
Class Node{
    Object data;
    Node leftChild;
    Node rightChild;
    
    public Node(Object data){
        this.data = data;
    }
}
//二叉树类
class BinaryTree{
    private Node root = null;
    public BinaryTree(Object data){
        root = new Node(data);
        root.leftChild = null;
        root.rightChild = null;
    }
    
    public Node getRoot(){
        return root;
    }
    //先序遍历查找
    public Node findKey(Node node,Object key){
        if(node == null) return null;
        if(node.data == key) return node;
        findKey(node.leftChild,key);
        findKey(node.rightChild,key);
    }
    
    //先序遍历
    public void preOrder(Node node){
        if(node){
            System.out.println(node.data);
            preOrder(node.leftChild);
            preOrder(node.leftChild);
        }
    }
    //后序
    //中序...
    //树的深度
    public int getDepth(Node node){
        if (node == null) return 0;
        else{
            int left = getDepth(node.leftChild);
            int right = getDepth(node.rightChild);
            if(left>right) return left+1;
            else return right+1;
        }
    }
    //二叉树中节点个数
    public int getNodeCount(Node node){
        if(node == null) return 0;
        else{
            int left = getNodeCount(node.leftChild);
            int right = getNodeCount(node.rightChild);
            return lef+right+1;
        }
    }
    //二叉树叶子节点数
    public int getLeavsCount(Node node){
        if(node == null) return 0;
        if(node.leftChild == null && node.rightChild = null) return 1;
        else{
            left = getLeavsCount(node.leftChild);
            right = getLeavsCount(node.rightChild);
            return left+right;
        }
    }
    //计算二叉树度为1的节点
    public int getNode1Count(Node node){
        if(node == null ) return 0;
        if((node.leftChild != null && node.rightChild == null )|| (node.lefChild == null&& node.rightChild != null))
        {
            return 1;
        }
        else{
            left = getNode1Count(node.leftChild);
            rigth = getNode1Count(node.rightChild);
            return left+right;
        }
    }
    //交换左右节点,先序,后序都可以
    public void ExchangeTree(Node node){
        if(node){
            Node temp = new Node();
            temp = node.rightChild;
            node.rigthChild = node.leftChild;
            node.leftChild = temp;
            ExchangeTree(node.leftChild);
            ExchangeTree(node.rightChild);
            
        }
    }
    //层次遍历二叉树
    //层次遍历二叉树,用队列实现,JAVA队列可以用LinkedList pull,add方法实现,或者直接用Queue
    public void leveOrder(Node node){
        if(node){
            Queue<Node> queue = new Queue<Node>();
            queue.add(node);
            while(!queue.isEmpty()){
                Node cur = queue.poll();
                System.out.print(cur.data);
                if(cur.leftChild != null) queue.add(cur.leftChild);
                if(cur.rightChild != null) queue.add(cur.rightChuld);
            }
        }
    }
}

 二叉查找树

#二叉查找树
private class Node(){
    Object data;
    Node leftChild;
    Node rightChild;
    public Node(Object data){
        this.data = data
        this.leftChild = null;
        this.rightChild = null;
    }
}
public class BinarySearchTree(){
    Node root;
    public BinarySearchTree(){
        root = null;
    }
    //contains
    public boolean contains(Object data,Node t){
        if(t == null) return false;
        else{
            if(t.data < data) return contains(x,t.rightChild);
            if(t.data > data) return contains(x,t.leftChild);
            else return true;
        }
    }
    public Node insert(Object data,Node t){
        if(t == null){
            Node node = new Node(data);
            return node;
        }
        if(t.data > data) return insert(data,t.leftChild);
        if(t.data < data) return insert(data,t.leftChild);
        else return t;
    }
    public Node findMin(Node t){
        if(t == null) return nll;
        else{
            if(t.leftChild == null) return t;
            else return findMin(t.leftChild);
        }
    }
    public Node findMax(node t){
        if(t == null) return null;
        else{
            if(t.rightChild == null) return t;
            else return findMin(t.rightChild);
        }
    }
}
原文地址:https://www.cnblogs.com/jellyj/p/9876668.html