堆栈和通过一个数组还原二叉树

import java.util.Scanner;
class Node{
private int iData;

public Node(int key){
    iData = key;
}

public int getKey(){
    return iData;
}

public void setKey(int id){
    iData = id;
}

}

class Heap{
private Node[] heapArray;
private int maxSize;
private int currentSize;

public Heap(int mx){
    maxSize = mx;
    currentSize = 0;
    heapArray = new Node[maxSize];
}

public boolean isEmpty(){
    return currentSize == 0;
}

public boolean insert(int key){
    if(currentSize<maxSize){
        heapArray[currentSize] = new Node(key);
        trickleUp(currentSize++);
        return true;
    }

    return false;
}

public void trickleUp(int index){
    Node bottom = heapArray[index];
    int current = index;
    int parent = (index-1)/2;

    while(parent>=0){
        if(heapArray[parent].getKey()>bottom.getKey()){
            heapArray[current] = bottom;
            return;
        }
        heapArray[current]=heapArray[parent];
        current = parent;
        parent = (parent-1)/2;
    }
    heapArray[0]=bottom;
}

public Node rmove(){
    if(currentSize==0){
        return null;
    }

    Node root = heapArray[0];
    heapArray[0] = heapArray[currentSize-1];
    currentSize--;
    trickleDown(0);
    return root;
}

private void trickleDown(int index) {

    Node temp = heapArray[index];
    while(index<currentSize/2){
        int left = index*2+1;
        int right = left+1;

        int biggerChild;
        if(right<currentSize
                &&heapArray[right].getKey()>heapArray[left].getKey()){
            biggerChild = right;
        }else{
            biggerChild = left;
        }

        if(temp.getKey()>heapArray[biggerChild].getKey()){
            break;
        }


        heapArray[index]=heapArray[biggerChild];
        index = biggerChild;
    }

    heapArray[index] = temp;

}

public boolean change(int index,int newValue){
    if(index>=currentSize){
        return false;
    }

    int preKey = heapArray[index].getKey();
    heapArray[index].setKey(newValue);

    if(preKey>newValue){
        trickleDown(index);
    }
    if(preKey<newValue){
        trickleUp(index);
    }

    return true;
}

}

public class TestForString{
private static class TreeNode{
int height;
char val;
TreeNode left;
TreeNode right;
public TreeNode(char data){
this.val=data;
height=0;
}
}
private static class Tree{
TreeNode root;
int height=0;

public Tree(){
    //Scanner scan = new Scanner(System.in);
    String strForRoot = scan.next();

    if(strForRoot.charAt(0)=='#'){
        return;
    }else{
        root=new TreeNode(strForRoot.charAt(0));
        Tree left=new Tree();
        Tree right=new Tree();
        root.left = left.root;
        root.right= right.root;

        height = 1+ Math.max(left.height, right.height);

    }
}

public int getHeight(){
    return height;
}

}

public static Scanner scan = new Scanner(System.in);
public static void main(String[] arg){
    int height=1;
    while(height>0){
        Tree tree = new Tree();
        height = tree.getHeight();
        System.out.println(height);
    }


}

}

class DataItem{
private int iData;

public DataItem(int data){
    iData = data;
}

public int getKey(){
    return iData;
}

}

class HashTable{
private DataItem[] hashArray;
private int arraySize;
private DataItem nonItem;

public HashTable(int size){
    arraySize = size;
    hashArray = new DataItem[arraySize];
    nonItem = new DataItem(-1);
}

public void displayTable(){
    System.out.print("Table: ");
    for(int j=0;j<arraySize;j++){
        if(hashArray[j]!=null){
            System.out.print(hashArray[j].getKey()+" ");
        }else{
            System.out.print("** ");
        }
    }
    System.out.println();
}

public int hashFunc2(int key){
    return 5-key%5;
}

public int hashFunc1(int key){
    return key%arraySize;
}
/**
 * 这个函数只能假设表示未满的
 * @param item
 */
public void insert(DataItem item){
    int key = item.getKey();
    int hashVal = hashFunc1(key);
    int step=hashFunc2(key);
    while(hashArray[hashVal]!=null
            &&hashArray[hashVal].getKey()!=-1){
        hashVal+=step;
        hashVal%=arraySize;
    }
    hashArray[hashVal]=item;

}

public DataItem delete(int key){
    int hashVal = hashFunc1(key);
    int step = hashFunc2(key);
    while(hashArray[hashVal]!=null){
        if(hashArray[hashVal].getKey()==key){
            DataItem temp = hashArray[hashVal];
            hashArray[hashVal]=nonItem;
            return temp;
        }
        hashVal+=step;
        hashVal%=arraySize;
    }

    return null;
}

public DataItem find(int key){
    int hashVal = hashFunc1(key);
    int step = hashFunc2(key);
    while(hashArray[hashVal]!=null){
        if(hashArray[hashVal].getKey()==key){
            return hashArray[hashVal];
        }
        hashVal+=step;
        hashVal%=arraySize;
    }
    return null;
}

private static class Link{
    public Link next;
    private int iData;

    public Link(int iData){
        this.iData = iData;
    }

    public int getKey(){
        return iData;
    }

    public void displayLink(){
        System.out.print(iData+" ");
    }
}

private static class SortedList{
    Link first;

    public SortedList(){
        first = null;
    }

    public void insert(Link theLink){
        int key = theLink.getKey();
        Link pre = null;
        Link current = first;

        while(current!=null&&current.getKey()<key){
            pre = current;
            current = current.next;
        }

        if(pre!=null){
            pre.next=theLink;
            theLink.next = current;
            return;
        }
        first = theLink;
    }

    public void delete(Link theLink){
        if(first == null){
            return;
        }

        Link pre = null;
        Link current = first;
        int key = theLink.getKey();

        while(current!=null&&current.getKey()<key){
            pre = current;
            current = current.next;
        }

        if(pre==null&&current.getKey()==key){
            first=current.next;
        }
        if(pre!=null&&current!=null&&current.getKey()==key){
            pre.next=current.next;
        }
    }

    public Link find(int key){
        Link current = first;
        while(current!=null&&current.getKey()<key){
            current = current.next;
        }

        if(current!=null&&current.getKey()==key){
            return current;
        }

        return null;
    }

    public void displayList(){
        System.out.print("List (first-->last): ");
        Link current = first;
        while(current!=null){
            current.displayLink();
            current = current.next;
        }
        System.out.println();
    }

}

private static class LinkedHashTable{
    private SortedList[] hashArray;
    private int arraySize;

    public LinkedHashTable(int size){
        arraySize = size;
        hashArray = new SortedList[arraySize];
        for(int i=0;i<arraySize;i++){
            hashArray[i]=new SortedList();
        }
    }

    public void displayTable(){
        for(int i=0;i<arraySize;i++){

            hashArray[i].displayList();
        }
    }

    public int hashFunction(int key){
        return key%arraySize;
    }

    public void insert(Link theLink){
        int hash = hashFunction(theLink.getKey());
        hashArray[hash].insert(theLink);
    }

    public void delete(Link theLink){
        int hash = hashFunction(theLink.getKey());
        hashArray[hash].delete(theLink);
    }

    public Link find(int key){
        int hash = hashFunction(key);
        return hashArray[hash].find(key);
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值