二叉树的递归和非递归遍历(java)

 
  
package com.wy.binarytree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

import com.wy.binarytree.BinaryTree.TreeNode;

public class BinaryTree {
	private TreeNode root = null;

	public BinaryTree() {
		root = new TreeNode(1, "A");
	}

	/**
	 * 构建二叉树 A B C D E F
	 */
	public void createBinaryTree() {
		TreeNode nodeB = new TreeNode(2, "B");
		TreeNode nodeC = new TreeNode(3, "C");
		TreeNode nodeD = new TreeNode(4, "D");
		TreeNode nodeE = new TreeNode(5, "E");
		TreeNode nodeF = new TreeNode(6, "F");
		root.leftChild = nodeB;
		root.rightChild = nodeC;
		nodeB.leftChild = nodeD;
		nodeB.rightChild = nodeE;
		nodeC.rightChild = nodeF;
	}

	/**
	 * 求二叉树的高度
	 *
	 */
	public int getHeight() {
		return getHeight(root);
	}

	private int getHeight(TreeNode node) {
		if (node == null) {
			return 0;
		} else {
			int i = getHeight(node.leftChild);
			int j = getHeight(node.rightChild);
			return (i < j) ? j + 1 : i + 1;
		}
	}

	/**
	 * 获取二叉树的结点数
	 *
	 */
	public int getSize() {
		return getSize(root);
	}

	private int getSize(TreeNode node) {
		if (node == null) {
			return 0;
		} else {
			return 1 + getSize(node.leftChild) + getSize(node.rightChild);
		}
	}

	/**
	 * 前序遍历——迭代
	 *
	 */
	public void preOrder(TreeNode node) {
		if (node == null) {
			return;
		} else {
			System.out.print(node.getData());
			preOrder(node.leftChild);
			preOrder(node.rightChild);
		}
	}

	/**
	 * 中序遍历——迭代
	 *
	 */
	public void midOrder(TreeNode node) {
		if (node == null) {
			return;
		} else {
			midOrder(node.leftChild);
			System.out.print(node.getData());
			midOrder(node.rightChild);
		}
	}

	/**
	 * 后序遍历——迭代
	 * 
	 * @author Administrator
	 *
	 */
	public void postOrder(TreeNode node) {
		if (node == null) {
			return;
		} else {
			postOrder(node.leftChild);
			postOrder(node.rightChild);
			System.out.print(node.getData());
		}
	}

	/**
	 * 前序遍历——非迭代
	 */
	public void nonRecOrder(TreeNode node) {
		if (node == null) {
			return;
		}
		Stack stack = new Stack();
		stack.push(node);
		while (!stack.isEmpty()) {
			TreeNode n = stack.pop();// 弹出根结点
			System.out.print(n.getData());
			if (n.rightChild != null) {
				stack.push(n.rightChild);
			}
			if (n.leftChild != null) {
				stack.push(n.leftChild);
			}
		}
	}

	/**
	 * 非递归前序遍历
	 */
	public void nonRecursivePreOrder(TreeNode node) {
		Stack stack = new Stack();
		TreeNode current;
		current = node;
		while ((current != null) || (!stack.isEmpty())) {
			if (current != null) {
				System.out.print(current.data);
				stack.push(current);
				current = current.leftChild;
			} else {
				current = (TreeNode) stack.peek();
				stack.pop();
				current = current.rightChild;
			}
		}
	}

	/**
	 * 非递归中序遍历
	 */
	public void nonRecursiveMidOrder(TreeNode node) {
		Stack stack = new Stack();
		TreeNode current;
		current = node;
		while ((current != null) || (!stack.empty())) {
			if (current != null) {
				stack.push(current);
				current = current.leftChild;
			} else {
				current = (TreeNode) stack.pop();
				;
				System.out.print(current.data);
				current = current.rightChild;
			}
		}
	}

	/**
	 * 非递归后序遍历
	 */
	public void nonRecursivePostOrder(TreeNode node) {
		Stack s = new Stack();
		Stack s2 = new Stack();
		Integer i = new Integer(1);
		while (node != null || !s.empty()) {
			while (node != null) {
				s.push(node);
				s2.push(new Integer(0));
				node = node.leftChild;
			}
			while (!s.empty() && s2.peek().equals(i)) {
				s2.pop();
				System.out.print(s.pop().data);
			}

			if (!s.empty()) {
				s2.pop();
				s2.push(new Integer(1));
				node = s.peek();
				node = node.rightChild;
			}
		}
	}

	/**
	 * 树的层次遍历
	 */
	public void bfs(TreeNode node) {
		Queue queue = new LinkedList();
		TreeNode current;
		current = node;
		while ((current != null) || (!queue.isEmpty())) {
			if (current != null) {
				System.out.print(current.data);
				queue.add(current.leftChild);
				queue.add(current.rightChild);
				current = queue.poll();
			} else {
				current = queue.poll();
			}
		}
	}

	public class TreeNode {
		private int index;
		private String data;
		private TreeNode leftChild;
		private TreeNode rightChild;

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}

		public String getData() {
			return data;
		}

		public void setData(String data) {
			this.data = data;
		}

		public TreeNode(int index, String data) {
			this.index = index;
			this.data = data;
			this.leftChild = null;
			this.rightChild = null;
		}
	}

	public static void main(String[] args) {
		BinaryTree binaryTree = new BinaryTree();
		 binaryTree.createBinaryTree();
		 int height = binaryTree.getHeight();
		 System.out.println("treeHeihgt:"+height);
		 int size = binaryTree.getSize();
		 System.out.println("treeSize:"+size);
		 System.out.println();
		 System.out.print("前序-递归:");
		 binaryTree.preOrder(binaryTree.root);
		 System.out.println();
		 System.out.print("中序-递归:");
		 binaryTree.midOrder(binaryTree.root);
		 System.out.println();
		 System.out.print("后序-递归:");
		 binaryTree.postOrder(binaryTree.root);
		 System.out.println();
		 System.out.print("前序-非递归:");
		 binaryTree.nonRecursivePreOrder(binaryTree.root);
		 System.out.println();
		 System.out.print("中序-非递归:");
		 binaryTree.nonRecursiveMidOrder(binaryTree.root);
		 System.out.println();
		 System.out.print("后序-非递归:");
		 binaryTree.nonRecursivePostOrder(binaryTree.root);
		 System.out.println();
		 System.out.print("树的层次遍历:");
		 binaryTree.bfs(binaryTree.root);

		// 前序生成二叉树;
		String[] data = new String[] { "A", "B","D", "#","#",  "E", "#", "#", "C", "#","F","#","#"};
		ArrayList arrayList = new ArrayList<>();
		for (String s : data) {
			arrayList.add(s);
		}
		TreeNode tree = binaryTree.createBinaryTreePre(arrayList);
		System.out.print("前序生成二叉树:");
		binaryTree.preOrder(tree);
	}

	/**
	 * 通过前序遍历的数据反向生成二叉树
	 * 
	 * @param arrayList
	 */
	private TreeNode createBinaryTreePre(ArrayList data) {

		return createBinaryTree(data.size(), data);
	}

	private TreeNode createBinaryTree(int size, ArrayList data) {
		if (data.size() == 0)
			return null;
		String d = data.get(0);
		int index = size-data.size();
		if (d.equals("#")) {
			data.remove(0);
			return null;
		}
		TreeNode node = new TreeNode(index, d);
		if (index == 0) {
			root = node;
		}
		data.remove(0);
		node.leftChild = createBinaryTree(size, data);
		node.rightChild = createBinaryTree(size, data);

		return node;
	}

}

输出:
treeHeihgt:3
treeSize:6

前序-递归:ABDECF
中序-递归:DBEACF
后序-递归:DEBFCA
前序-非递归:ABDECF
中序-非递归:DBEACF
后序-非递归:DEBFCA
树的层次遍历:ABCDEF前序生成二叉树:ABDECF

 
 

你可能感兴趣的:(数据结构和算法)