Java实现堆,最大堆,最小堆,左高树,左低树

MyList.LinearList参见链接
MyTree.TreeNode参见链接

Heap

package MyHeap;

public interface Heap {
    abstract public int size();
    abstract public int maxSize();
    abstract public boolean empty();
    abstract public boolean full();
    abstract public Comparable top();
    abstract public Comparable pop();
    abstract public Heap push(Comparable object);
    abstract public void initialize(Comparable[] object);
    abstract public void output();
}

MaxHeap

package MyHeap;

import java.util.Random;

import MyList.LinearList;

public class MaxHeap implements Heap {
    public static void main(String[] args){
        MaxHeap heap=new MaxHeap();
        Random random=new Random();
        for(int i=0;i<100;i++){
            heap.push(random.nextInt(1000));
        }
        heap.output();
        System.out.println("Size:"+heap.size());
        for(int i=0;i<100;i++){
            System.out.print(heap.pop()+" ");
            System.out.print("\r\n");
        }
        System.out.print("\r\n");
    }

    private int MaxSize;
    private MyList.LinearList List;

    public MaxHeap(){
        MaxSize=-1;
        List=new LinearList();
    }

    public MaxHeap(int maxsize){
        MaxSize=maxsize;
        List=new LinearList();
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return List.size();
    }

    @Override
    public int maxSize() {
        // TODO Auto-generated method stub
        return MaxSize;
    }

    @Override
    public boolean empty() {
        // TODO Auto-generated method stub
        return size()==0;
    }

    @Override
    public boolean full() {
        // TODO Auto-generated method stub
        return size()==maxSize();
    }

    private int parent(int index){
        if(index>=size()||index<=0){
            return -1;
        }
        else{
            return (index-1)/2;
        }
    }

    private int leftChild(int index){
        int output=index*2+1;
        if(output>=size()||index<0){
            return -1;
        }
        else{
            return output;
        }
    }

    private int rightChild(int index){
        int output=index*2+2;
        if(output>=size()||index<0){
            return -1;
        }
        else{
            return output;
        }
    }

    @Override
    public Comparable top() {
        // TODO Auto-generated method stub
        return (Comparable) List.at(0);
    }

    private void reformDown(int index){
        int leftchild=leftChild(index);
        int rightchild=rightChild(index);
        if(leftchild==-1&&rightchild==-1){
            return;
        }
        else if(leftchild==-1){
            if(((Comparable) List.at(rightchild))
                .compareTo((Comparable) List.at(index))==1)
            {
                List.swap(index,rightchild);
                reformDown(rightchild);
            }
            else{
                return;
            }
        }
        else if(rightchild==-1){
            if(((Comparable) List.at(leftchild))
                    .compareTo((Comparable) List.at(index))==1)
            {
                    List.swap(index,leftchild);
                    reformDown(leftchild);
            }
            else{
                return;
            }
        }
        else if(((Comparable) List.at(leftchild))
                .compareTo((Comparable) List.at(rightchild))==1)
        {
            List.swap(index, leftchild);
            reformDown(leftchild);
        }
        else{
            List.swap(index,rightchild);
            reformDown(rightchild);
        }
    }

    @Override
    public Comparable pop() {
        // TODO Auto-generated method stub
        if(empty()){
            System.out.println("heap is empty");
            return null;
        }
        else{
            Comparable object=top();
            List.swap(0, size()-1);
            List.deleteByIndex(size()-1);
            reformDown(0);
            return object;
        }
    }

    private void reformUp(int index){
        int parent=parent(index);
        while(parent!=-1&&
                ((Comparable) List.at(parent))
                .compareTo((Comparable) List.at(index))==-1)
        {
            List.swap(parent, index);
            reformUp(parent);
        }
    }

    @Override
    public Heap push(Comparable object) {
        // TODO Auto-generated method stub
        if(full()){
            System.out.println("heap is full");
        }
        else{
            List.insert(object);
            reformUp(size()-1);
        }
        return this;
    }

    @Override
    public void initialize(Comparable[] object) {
        // TODO Auto-generated method stub
        List.setMaxLength(object.length);
        for(int i=0;i@Override
    public void output() {
        // TODO Auto-generated method stub
        List.output();
    }

}

MaxHblt

package MyHeap;

import java.util.Random;

import MyTree.TreeNode;

public class MaxHblt implements Heap {
    public static void main(String[] args){
        MaxHblt hblt=new MaxHblt();
        Random random=new Random();
        for(int i=0;i<100;i++){
            hblt.push(random.nextInt(1000));
        }
        hblt.output();
        System.out.println("Size:"+hblt.size());
        System.out.println("Height:"+hblt.height());
        for(int i=0;i<100;i++){
            System.out.print(hblt.pop()+" ");
            System.out.print("\r\n");
        }
        System.out.print("\r\n");
    }

    private int MaxSize;
    private TreeNode Root;

    public MaxHblt(){
        MaxSize=-1;
        Root=new TreeNode();
    }

    public MaxHblt(int maxsize){
        MaxSize=maxsize;
        Root=new TreeNode();
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return Root.size();
    }

    @Override
    public int maxSize() {
        // TODO Auto-generated method stub
        return MaxSize;
    }


    @Override
    public boolean empty() {
        // TODO Auto-generated method stub
        return size()==0;
    }

    @Override
    public boolean full() {
        // TODO Auto-generated method stub
        return size()==maxSize();
    }

    public int height(){
        return Root.height();
    }

    @Override
    public Comparable top() {
        // TODO Auto-generated method stub
        return Root.root();
    }

    private TreeNode combine(TreeNode hblt1,TreeNode hblt2){
        if(hblt1==null||hblt1.size()==0){
            return hblt2;
        }
        else if(hblt2==null||hblt2.size()==0){
            return hblt1;
        }
        else{
            TreeNode node;
            if((hblt1.root()).compareTo(hblt2.root())==1){
                node=new TreeNode(
                                hblt1.root(),
                                hblt1.leftChild(),
                                combine(
                                        hblt1.rightChild(),
                                        hblt2
                                        )
                                );
            }
            else{
                node=new TreeNode(
                                hblt2.root(),
                                hblt2.leftChild(),
                                combine(
                                        hblt2.rightChild(),
                                        hblt1
                                        )
                                );
            }
            if(node.leftChild()==null||node.leftChild().height()new TreeNode(
                        node.root(),
                        node.rightChild(),
                        node.leftChild()
                        );
            }
            return node;
        }
    }

    @Override
    public Comparable pop() {
        // TODO Auto-generated method stub
        if(empty()){
            System.out.println("heap is empty");
            return null;
        }
        else{
            Comparable object=top();
            Root=combine(Root.leftChild(),Root.rightChild());
            return object;
        }
    }

    @Override
    public Heap push(Comparable object) {
        // TODO Auto-generated method stub
        if(full()){
            System.out.println("heap is full");
        }
        else{
            TreeNode node=new TreeNode(object);
            Root=combine(Root,node);
        }
        return this;
    }

    @Override
    public void initialize(Comparable[] object) {
        // TODO Auto-generated method stub
        for(int i=0;i@Override
    public void output() {
        // TODO Auto-generated method stub
        Root.preOrder();
        System.out.print("\r\n");
    }

}

MinHeap

package MyHeap;

import java.util.Random;

import MyList.LinearList;

public class MinHeap implements Heap {
    public static void main(String[] args){
        MinHeap heap=new MinHeap();
        Random random=new Random();
        for(int i=0;i<100;i++){
            heap.push(random.nextInt(1000));
        }
        heap.output();
        System.out.println("Size:"+heap.size());
        for(int i=0;i<100;i++){
            System.out.print(heap.pop()+" ");
            System.out.print("\r\n");
        }
        System.out.print("\r\n");
    }

    private int MaxSize;
    private MyList.LinearList List;

    public MinHeap(){
        MaxSize=-1;
        List=new LinearList();
    }

    public MinHeap(int maxsize){
        MaxSize=maxsize;
        List=new LinearList();
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return List.size();
    }

    @Override
    public int maxSize() {
        // TODO Auto-generated method stub
        return MaxSize;
    }

    @Override
    public boolean empty() {
        // TODO Auto-generated method stub
        return size()==0;
    }

    @Override
    public boolean full() {
        // TODO Auto-generated method stub
        return size()==maxSize();
    }

    private int parent(int index){
        if(index>=size()||index<=0){
            return -1;
        }
        else{
            return (index-1)/2;
        }
    }

    private int leftChild(int index){
        int output=index*2+1;
        if(output>=size()||index<0){
            return -1;
        }
        else{
            return output;
        }
    }

    private int rightChild(int index){
        int output=index*2+2;
        if(output>=size()||index<0){
            return -1;
        }
        else{
            return output;
        }
    }

    @Override
    public Comparable top() {
        // TODO Auto-generated method stub
        return (Comparable) List.at(0);
    }

    private void reformDown(int index){
        int leftchild=leftChild(index);
        int rightchild=rightChild(index);
        if(leftchild==-1&&rightchild==-1){
            return;
        }
        else if(leftchild==-1){
            if(((Comparable) List.at(rightchild))
                .compareTo((Comparable) List.at(index))==-1)
            {
                List.swap(index,rightchild);
                reformDown(rightchild);
            }
            else{
                return;
            }
        }
        else if(rightchild==-1){
            if(((Comparable) List.at(leftchild))
                    .compareTo((Comparable) List.at(index))==-1)
            {
                    List.swap(index,leftchild);
                    reformDown(leftchild);
            }
            else{
                return;
            }
        }
        else if(((Comparable) List.at(leftchild))
                .compareTo((Comparable) List.at(rightchild))==-1)
        {
            List.swap(index, leftchild);
            reformDown(leftchild);
        }
        else{
            List.swap(index,rightchild);
            reformDown(rightchild);
        }
    }

    @Override
    public Comparable pop() {
        // TODO Auto-generated method stub
        if(empty()){
            System.out.println("heap is empty");
            return null;
        }
        else{
            Comparable object=top();
            List.swap(0, size()-1);
            List.deleteByIndex(size()-1);
            reformDown(0);
            return object;
        }
    }

    private void reformUp(int index){
        int parent=parent(index);
        while(parent!=-1&&
                ((Comparable) List.at(parent))
                .compareTo((Comparable) List.at(index))==1)
        {
            List.swap(parent, index);
            reformUp(parent);
        }
    }

    @Override
    public Heap push(Comparable object) {
        // TODO Auto-generated method stub
        if(full()){
            System.out.println("heap is full");
        }
        else{
            List.insert(object);
            reformUp(size()-1);
        }
        return this;
    }

    @Override
    public void initialize(Comparable[] object) {
        // TODO Auto-generated method stub
        List.setMaxLength(object.length);
        for(int i=0;i@Override
    public void output() {
        // TODO Auto-generated method stub
        List.output();
    }


}

MinHblt

package MyHeap;

import java.util.Random;

import MyTree.TreeNode;

public class MinHblt implements Heap {
    public static void main(String[] args){
        MinHblt hblt=new MinHblt();
        Random random=new Random();
        for(int i=0;i<100;i++){
            hblt.push(random.nextInt(1000));
        }
        hblt.output();
        System.out.println("Size:"+hblt.size());
        System.out.println("Height:"+hblt.height());
        for(int i=0;i<100;i++){
            System.out.print(hblt.pop()+" ");
            System.out.print("\r\n");
        }
        System.out.print("\r\n");
    }

    private int MaxSize;
    private TreeNode Root;

    public MinHblt(){
        MaxSize=-1;
        Root=new TreeNode();
    }

    public MinHblt(int maxsize){
        MaxSize=maxsize;
        Root=new TreeNode();
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return Root.size();
    }

    @Override
    public int maxSize() {
        // TODO Auto-generated method stub
        return MaxSize;
    }


    @Override
    public boolean empty() {
        // TODO Auto-generated method stub
        return size()==0;
    }

    @Override
    public boolean full() {
        // TODO Auto-generated method stub
        return size()==maxSize();
    }

    public int height(){
        return Root.height();
    }

    @Override
    public Comparable top() {
        // TODO Auto-generated method stub
        return Root.root();
    }

    private TreeNode combine(TreeNode hblt1,TreeNode hblt2){
        if(hblt1==null||hblt1.size()==0){
            return hblt2;
        }
        else if(hblt2==null||hblt2.size()==0){
            return hblt1;
        }
        else{
            TreeNode node;
            if((hblt1.root()).compareTo(hblt2.root())==-1){
                node=new TreeNode(
                                hblt1.root(),
                                hblt1.leftChild(),
                                combine(
                                        hblt1.rightChild(),
                                        hblt2
                                        )
                                );
            }
            else{
                node=new TreeNode(
                                hblt2.root(),
                                hblt2.leftChild(),
                                combine(
                                        hblt2.rightChild(),
                                        hblt1
                                        )
                                );
            }
            if(node.leftChild()==null||node.leftChild().height()new TreeNode(
                        node.root(),
                        node.rightChild(),
                        node.leftChild()
                        );
            }
            return node;
        }
    }

    @Override
    public Comparable pop() {
        // TODO Auto-generated method stub
        if(empty()){
            System.out.println("heap is empty");
            return null;
        }
        else{
            Comparable object=top();
            Root=combine(Root.leftChild(),Root.rightChild());
            return object;
        }
    }

    @Override
    public Heap push(Comparable object) {
        // TODO Auto-generated method stub
        if(full()){
            System.out.println("heap is full");
        }
        else{
            TreeNode node=new TreeNode(object);
            Root=combine(Root,node);
        }
        return this;
    }

    @Override
    public void initialize(Comparable[] object) {
        // TODO Auto-generated method stub
        for(int i=0;i@Override
    public void output() {
        // TODO Auto-generated method stub
        Root.preOrder();
        System.out.print("\r\n");
    }

}

你可能感兴趣的:(Java,数据结构)