树的深度优先遍历和广度优先遍历

来源:互联网 发布:注册表修改mac地址没用 编辑:程序博客网 时间:2024/05/03 18:16


1 定义一个树的接点
public class GrapNode {
    public String nodeId;//节点唯一标识
    public boolean start=false;//起点标记
    public boolean end=false;//终点标记
    public GrapNode parentNode;//父节点
    public GrapNode childNode;//子节点
    public boolean leaf=false;
    public Integer val=0;//权值
    public boolean wasVisited =false;
    }
2 定义一个栈stack

public class Stack {
  private final int SIZE=20;
  private int[] st;
  private GrapNode[] stObj;
  private int top;
  public Stack(){
      st = new int[SIZE];
      stObj = new GrapNode[SIZE];
      top=-1;
  }
  public void push(int idx){
      st[++top]=idx;
  }
  public int pop(){
      return st[top--];
  }
  public int peek(){
      return st[top];
  }
 
  public void pushObj(GrapNode obj){
      stObj[++top]=obj;
  }
  public GrapNode popObj(){
      return stObj[top--];
  }
  public GrapNode peekObj(){
      return stObj[top];
  }
  public boolean isEmpty(){
      return top == -1;
  }
}

3 定义一个队列

public class Queue {
  private final int SIZE=20;
  private GrapNode[] queObjArr;
  private int front;
  private int rear;
  public Queue(){
      queObjArr = new GrapNode[SIZE];
      front = 0;
      rear = -1;
  }
 
  public void insert(GrapNode g){
      if(rear == SIZE-1){
          rear =-1;
      }
      queObjArr[++rear]=g;
  }
  public GrapNode remove(){
      GrapNode tmp = queObjArr[front++];
      
      if(front == SIZE){
          front = 0;
      }
      return tmp;
  }
 
  public boolean isEmpty(){
      return (rear+1==front||front+SIZE-1==rear);
  }
}

4 定义一个测试类
public class TestTree {
    private final int MAX_VERTS = 20;//最大定点个数
    private GrapNode vs[];//树的节点定义
    private int nVerts;//当前节点个数
    private GrapNode matrixG[][];//节点矩阵图
    private Stack stack;
    private Queue queue;
    public Graph() {
        nVerts = 0;
        vs = new GrapNode[MAX_VERTS];
        stack = new Stack();
        queue = new Queue();
        matrixG = new GrapNode[MAX_VERTS][MAX_VERTS];
        for (int i = 0; i < MAX_VERTS; i++) {
            for (int j = 0; j < MAX_VERTS; j++) {
                GrapNode g = new GrapNode();
                matrixG[i][j] = g;
            }
        }
    }
    
    public void displayMatrix(){
        if(nVerts==0){
            System.out.println("当前树没有节点,无法展示矩阵!");
        }else{
            for (int i = 0; i < nVerts; i++) {
                for (int j = 0; j < nVerts; j++) {
                    System.out.print(matrixG[i][j].getVal()+" ") ;
                }
                System.out.println();
            }
        }
    }
    
    public void displayVertex(){
        System.out.print("树节点信息:");
        for(int i=0;i<nVerts;i++){
            System.out.print(i+"-"+this.vs[i].getNodeId()+",");
        }
        System.out.println();
    }
    
    public boolean addVertex(GrapNode g){
        if(nVerts>=MAX_VERTS-1){
            return false;
        }else{
            this.vs[nVerts++]=g;
            System.out.println("添加顶点:"+g.getNodeId());
            return true;
        }
    }
    
    public boolean addEdge(int start,int end,int qz,boolean leaf){
        if(start>nVerts||end>nVerts){
            System.out.println("不能添加边!("+start+","+end+")");
            return false;
        }else{
            System.out.println("成功添加边:"+vs[start].getNodeId()+vs[end].getNodeId()+"("+start+""+end+")");
            matrixG[start][end].setVal(qz);
            matrixG[start][end].setLeaf(leaf);
            return true;
        }
    }
    //深度优先遍历
    public void sdbl(){
        this.vs[0].setWasVisited(true);
        displayNode(vs[0]);//显示节点信息
        stack.pushObj(vs[0]);//向堆栈中压入一个节点
        while(!stack.isEmpty()){
            //获取vs[0]的下一个未被访问且存在边的顶点
            int v = getUnvisitedVertex(getIndexOfVertex(stack.peekObj()));
            if(v == -1){//不存在则弹出当前节点vs[v]
                GrapNode gg = stack.popObj();
                //System.out.println("#"+gg.getNodeId());
            }else{
                vs[v].wasVisited  = true;
                displayNode(vs[v]);//显示节点信息
                stack.pushObj(vs[v]);
            }
        }
        for(int j=0;j<nVerts;j++){//重置值
            vs[j].setWasVisited(false);
        }
    }
    //广度优先遍历
    public void gdbl(){
        vs[0].setWasVisited(true);
        displayNode(vs[0]);
        queue.insert(vs[0]);
        int v2;
        while(!queue.isEmpty()){
            GrapNode g2 = queue.remove();
            int v1 = getIndexOfVertex(g2);
            while((v2=getUnvisitedVertex(v1))!=-1){
                vs[v2].setWasVisited(true);
                displayNode(vs[v2]);
                queue.insert(vs[v2]);
            }
        }
        for(int j=0;j<nVerts;j++){//重置值
            vs[j].setWasVisited(false);
        }
    }
    public int getIndexOfVertex(GrapNode g){
        int idx = -1;
        if(g!=null){
        for(int j=0;j<nVerts;j++){
            if(vs[j].getNodeId()==g.getNodeId()){
                idx = j;
                break;
            }
        }}
        return idx;
    }
    public int getUnvisitedVertex(int v){//获取存与v相邻的且没有被访问的节点
        for(int j=0;j<nVerts;j++){
            if(matrixG[v][j].getVal()!=0&&vs[j].wasVisited==false){
                return j;
            }
        }
        return -1;
    }
    public  void displayNode(GrapNode g){
        if(g.isLeaf()){
          System.out.print(g.getNodeId()+"#,");
        }else{
          System.out.print(g.getNodeId()+",");
        }
    }
    public int getnVerts() {
        return nVerts;
    }

    public void setnVerts(int nVerts) {
        this.nVerts = nVerts;
    }

    public GrapNode[] getVs() {
        return vs;
    }

    public Stack getStack() {
        return stack;
    }

    public void setStack(Stack stack) {
        this.stack = stack;
    }

    public void setVs(GrapNode[] vs) {
        this.vs = vs;
    }
}



0 0
原创粉丝点击