树的前序、中序、后序遍历的方法

先准备自定义的Queue队列类


import java.util.Iterator;

public class Queue<T> implements Iterable<T>{
    //记录首结点
    private Node head;
    //记录最后一个结点
    private Node last;
    //记录队列中元素的个数
    private int N;


    private class Node{
        public T item;
        public Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
    public Queue() {
        this.head = new Node(null,null);
        this.last=null;
        this.N=0;
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return N==0;
    }

    //返回队列中元素的个数
    public int size(){
        return N;
    }

    //向队列中插入元素t
    public void enqueue(T t){

        if (last==null){
            //当前尾结点last为null
            last= new Node(t,null);
            head.next=last;
        }else {
            //当前尾结点last不为null
            Node oldLast = last;
            last = new Node(t, null);
            oldLast.next=last;
        }

        //元素个数+1
        N++;
    }

    //从队列中拿出一个元素
    public T dequeue(){
        if (isEmpty()){
            return null;
        }

        Node oldFirst= head.next;
        head.next=oldFirst.next;
        N--;

        //因为出队列其实是在删除元素,因此如果队列中的元素被删除完了,需要重置last=null;

        if (isEmpty()){
            last=null;
        }
        return oldFirst.item;
    }


    @Override
    public Iterator<T> iterator() {
        return new QIterator();
    }

    private class QIterator implements Iterator{
        private Node n;

        public QIterator(){
            this.n=head;
        }
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

        @Override
        public Object next() {
            n = n.next;
            return n.item;
        }
    }


}

在博客https://blog.csdn.net/Xeon_CC/article/details/108642782 中的BinaryTree类的基础上,加上下面几个关键的遍历方法

//获取整个树中所有的键
	public Queue<Key> preErgodic(){
		Queue<Key> keys = new Queue<Key>();
		
		preErgodic(root, keys);
		return keys;
	}
	
	//获取指定树x中的所有键,并放到keys队列中
	public void preErgodic(Node x, Queue<Key> keys) {
		
		if(x == null) {
			return;
		}
		
		//把x节点的key放入到keys中
		keys.enqueue(x.key);
		
		//递归便利x节点的左子树
		if(x.left != null) {
			preErgodic(x.left, keys);
		}
		
		//递归便利x节点的右子树
		if(x.right != null) {
			preErgodic(x.right, keys);
		}
		
	}
	
	public Queue<Key> midErgodic(){
		Queue<Key> keys = new Queue<Key>();
		midErgodic(root,keys);
		return keys;
		
	}
	
	public void midErgodic(Node x, Queue<Key> keys) {
		if(x == null) {
			return;
		}
		
		
		//递归便利x节点的左子树
		if(x.left != null) {
			midErgodic(x.left, keys);
		}
		
		//把x节点的key放入到keys中
		keys.enqueue(x.key);
		
		//递归便利x节点的右子树
		if(x.right != null) {
			midErgodic(x.right, keys);
		}
		
	}
	
	public Queue<Key> afterErgodic(){
		Queue<Key> keys = new Queue<Key>();
		afterErgodic(root,keys);
		return keys;
		
	}
	
	public void afterErgodic(Node x, Queue<Key> keys) {
		if(x == null) {
			return;
		}
		
		//递归便利x节点的左子树
		if(x.left != null) {
			afterErgodic(x.left, keys);
		}
		
		//递归便利x节点的右子树
		if(x.right != null) {
			afterErgodic(x.right, keys);
		}
		
		//把x节点的key放入到keys中
		keys.enqueue(x.key);
		
	}

在main方法测试前序、中序、后序遍历

//创建树对象
		BinaryTree<String, String> tree = new BinaryTree<>();
		tree.put("E", "5");
		tree.put("B", "2");
		tree.put("G", "7");
		tree.put("A", "1");
		tree.put("D", "4");
		tree.put("F", "6");
		tree.put("H", "8");
		tree.put("C", "3");
		//往树中添加数据
		
		System.out.println("前序遍历");
		//前序遍历
		Queue<String> prekeys = tree.preErgodic();
		for(String key:prekeys) {
			String value = tree.get(key);
			System.out.println(key+"----"+value);
		}
		
		System.out.println("中序遍历");
		//中序遍历
		Queue<String> midkeys = tree.midErgodic();
		for(String key:midkeys) {
			String value = tree.get(key);
			System.out.println(key+"----"+value);
		}
		
		System.out.println("后序遍历");
		//后序遍历
		Queue<String> afterkeys = tree.afterErgodic();
		for(String key:afterkeys) {
			String value = tree.get(key);
			System.out.println(key+"----"+value);
		}
		

猜你喜欢

转载自blog.csdn.net/Xeon_CC/article/details/108698941