数据结构_二叉树非递归遍历

package zz;

import java.util.Stack;

/**
 * 二叉树中的二叉搜索树,即一个节点的左子节点关键值小于这个节点,右子节点的关键值大于这个节点
 * 
 * @author Administrator
 *
 */
class TreeNode {
	int value;
	TreeNode leftChild;
	TreeNode rightChild;

	public TreeNode(int value) {
		this.value = value;
		this.leftChild = null;
		this.rightChild = null;
	}
}

public class Test {
	// 求二叉树的深度
	public static int getTreeLength(TreeNode root) {
		if (root == null)
			return 0;
		if (root.leftChild == null && root.rightChild == null)
			return 1;
		else
			return 1 + Math.max(getTreeLength(root.leftChild), getTreeLength(root.rightChild));
	}

	// 插入子节点
	public static void insert(TreeNode root, int data) {
		TreeNode newNode = new TreeNode(data);
		if (root == null) {
			root = newNode;
			root.leftChild = null;
			root.rightChild = null;
		} else {
			TreeNode cur = root;
			TreeNode parent;
			while (true) {
				parent = cur;
				if (data <= cur.value) {
					cur = cur.leftChild;
					if (cur == null) {
						parent.leftChild = newNode;
						return;
					}
				} else {
					cur = cur.rightChild;
					if (cur == null) {
						parent.rightChild = newNode;
						return;
					}
				}
			}
		}
	}

	// 查找二叉搜索树中的某个节点
	public static TreeNode find(TreeNode root, int data) {
		TreeNode cur = root;
		while (data != cur.value) {
			if (data > cur.value) {
				cur = cur.rightChild;
			} else {
				cur = cur.leftChild;
			}
			if (cur == null)
				return null;
		}
		return cur;
	}

	// 先序遍历(递归实现):先访问根节点,再访问左子节点,最后访问右孩子节点
	public static void recPreOrder(TreeNode root) {
		if (root == null)
			return;
		System.out.print(root.value + ",");
		if (root.leftChild != null)
			recPreOrder(root.leftChild);
		if (root.rightChild != null)
			recPreOrder(root.rightChild);
	}

	// 先序遍历(栈实现):先访问根节点,再访问左子节点,最后访问右孩子节点
	public static void PreOrder(TreeNode root) {
		if (root == null)  return;
		Stack<TreeNode> stacks = new Stack<TreeNode>();
		TreeNode node = root;
		while (node != null || stacks.size() > 0) {
			while (node != null) {
				System.out.print(node.value + " ");
				stacks.push(node);
				node = node.leftChild;
			}
			if (stacks.size() > 0) {
				node = stacks.pop();
				//System.out.print(node.value + ",");
				node = node.rightChild;
			}
		}
	}
	// 中序遍历(递归实现):先访问左子节点,再访问根节点,最后访问右子节点
	public static void recInOrder(TreeNode root) {
		if (root == null)
			return;
		if (root.leftChild != null)
			recInOrder(root.leftChild);
		System.out.print(root.value + ",");
		if (root.rightChild != null)
			recInOrder(root.rightChild);
	}
	//中序遍历(栈实现):先访问左子节点,再访问根节点,最后访问右子节点
	public static void InOrder(TreeNode root) {
		if(root == null)  return;
		Stack<TreeNode> stacks = new Stack<TreeNode>();
		TreeNode node = root;
		while(node != null || stacks.size() > 0) {
			while(node != null) {
				stacks.push(node);
			    node = node.leftChild;
			}
			if(!stacks.isEmpty()) {
				node = stacks.pop();
				System.out.print(node.value + " ");
				node = node.rightChild;
			}
		}
	}
	// 后序遍历(递归实现):先访问左子节点,再访问右子节点,最后访问根节点
	public static void recPostOrder(TreeNode root) {
		if (root == null)
			return;
		if (root.leftChild != null)
			recInOrder(root.leftChild);
		if (root.rightChild != null)
			recInOrder(root.rightChild);
		System.out.print(root.value + ",");

	}
	// 后序遍历(栈实现):先访问左子节点,再访问右子节点,最后访问根节点
	public static void PostOrder(TreeNode root) {
		if(root == null)  
			return;
		Stack<TreeNode> stacks = new Stack<TreeNode>();
		TreeNode node = root;
		TreeNode preNode = null;
		while(node != null || !stacks.isEmpty()) {
			while(node != null) {
				stacks.push(node);
				node = node.leftChild;
			}
			node = stacks.peek();
			
			if(node.rightChild == null || node.rightChild == preNode) {
				System.out.print(node.value + " ");
				node = stacks.pop();
				preNode = node;
				node = null;
			} else {
				node = node.rightChild;
			}
		}
	}
	public static void main(String[] args) {
		TreeNode root = new TreeNode(4);
		insert(root, 2);
		insert(root, 6);
		insert(root, 1);
		insert(root, 3);
		insert(root, 5);
		insert(root, 7);
		insert(root, 8);
		
		System.out.println("非递归先序遍历树:");
		PreOrder(root);
		System.out.println();
		System.out.println("非递归中序遍历树");
		InOrder(root);
		System.out.println();
		System.out.println("非递归后序遍历树");
		PostOrder(root);
	}
}

  

posted @ 2017-06-27 16:44  55329  阅读(175)  评论(0编辑  收藏  举报