java二叉树创建层序创建,遍历(递归,非递归版)

自闭症网瘾萝莉.ら 提交于 2020-03-06 12:47:49

使用队列工具 进行二叉树层序创建
后再补充二叉树先序创建,和 二叉树后序非递归遍历
有问题的,或者有更好的实现方式,欢迎指正!!!

class TNode<E>;		//结点信息
TNode<Character> createBiTree(char[] array);		//层序创建二叉树
int GetHeight(TNode BT);			//求树高
boolean isEmpty(TNode root); 	//二叉判空
void PreorderTraversal(TNode BT);	//先序递归遍历
void InorderTraversal(TNode BT);		//中序递归遍历
void PostorderTraversal(TNode BT);	//后序递归遍历
//我们要借鉴递归的思想去理解程序,
//递归的优点大家都晓得:代码简洁
//递归缺点,递归层数较多时,空间占用大,性能太低
//所以 要善于将递归 改为非递归程序
void PreorderTraversalIteration(TNode BT);	//先序二叉树非递归遍历
void InorderTraversalIteration(TNode BT);	//中序二叉树非递归遍历
void LevelorderTraversalIteration(TNode BT);//层序非递归遍历
void PreorderPrintLeaves(TNode BT);		//输出叶子结点
package tree;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;

public class BiTree {
	public class TNode<E>{
		E data;
		TNode<E> leftNode;
		TNode<E> rightNode;
		public TNode(E data) {
			this.data = data;
			leftNode = null;
			rightNode = null;
		}
		public TNode() {
			leftNode = null;
			rightNode = null;
		}
	}
	//使用队列进行层序创建,层序创建
	public TNode<Character> createBiTree(char[] array) {
		ArrayDeque<TNode<Character>> queue = new ArrayDeque<>();
		int index = 0;
		TNode<Character> childNode = null;
		TNode<Character> node = null;
		if (queue.size() == 0) {
			node =  new TNode<>(array[0]);
			queue.addLast(node);
			index++;
		}
		while(queue.isEmpty() == false) {
			childNode = queue.pollFirst();
			if(array[index] != '#') {
				childNode.leftNode = new TNode<>();
				childNode.leftNode.data = array[index];
				queue.addLast(childNode.leftNode);
				index++;
			}else {
				index++;
			}
			if(array[index] != '#') {
				childNode.rightNode = new TNode<>();
				childNode.rightNode.data = array[index];
				queue.addLast(childNode.rightNode);
				index++;
			}else {
				index++;
			}
		}
		return node;
	}
	//求高度
	int GetHeight(TNode BT) {
		int HL,HR,MAXH;
		if(BT != null) {
			HL = GetHeight(BT.leftNode);
			HR = GetHeight(BT.rightNode);
			MAXH = HL > HR ? HL : HR;
			return MAXH +1;
		}else {
			return 0;
		}
	}
	public boolean isEmpty(TNode root) {
		if (root == null) {
			return true;
		}
		return false;
	}
	//递归遍历
	void InorderTraversal(TNode BT) {
		if (BT != null) {
			InorderTraversal(BT.leftNode);
			System.out.println(BT.data);
			InorderTraversal(BT.rightNode);
		}
	}
	void PreorderTraversal(TNode BT) {
		if (BT != null) {
			System.out.println(BT.data);
			PreorderTraversal(BT.leftNode);
			PreorderTraversal(BT.rightNode);
		}
	}
	void PostorderTraversal(TNode BT) {
		if (BT != null) {
			PostorderTraversal(BT.leftNode);
			PostorderTraversal(BT.rightNode);
			System.out.println(BT.data);
		}
	}

	//非递归遍历
	void InorderTraversalIteration(TNode BT) {
		Stack<TNode> stack = new Stack<>();
		while (BT != null || stack.isEmpty() == false) {
			while(BT!=null) {
				stack.push(BT);
				BT = BT.leftNode;
			}
			BT = stack.pop();
			System.out.println(BT.data);
			BT = BT.rightNode;
		}
	}
	void PreorderTraversalIteration(TNode BT) {
		Stack<TNode> stack = new Stack<>();
		while (BT != null || stack.isEmpty() == false) {
			while(BT!=null) {
				stack.push(BT);
				System.out.println(BT.data);
				BT = BT.leftNode;
			}
			BT = stack.pop();
			BT = BT.rightNode;
		}
	}
	void PostorderTraversalIteration(TNode BT) {
		
	}
	//层序遍历
	void LevelorderTraversalIteration(TNode BT) {
		ArrayDeque<TNode> queue = new ArrayDeque<>();
		if (BT == null) {
			return ;
		}
		queue.addLast(BT);
		while(queue.isEmpty() != true) {
			BT = queue.pollFirst();
			System.out.println(BT.data);
			if(BT.leftNode != null) {
				queue.addLast(BT.leftNode);
			}
			if (BT.rightNode != null) {
				queue.addLast(BT.rightNode);
			}
		}
	}
	//输出叶子结点
	void PreorderPrintLeaves(TNode BT) {
		if (BT != null) {
			if (BT.leftNode == null && BT.rightNode == null) {
				System.out.println(BT.data);
			}
			PreorderPrintLeaves(BT.leftNode);
			PreorderPrintLeaves(BT.rightNode);
		}
	}
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!