# 数据结构基础-二叉树的遍历

public class BinaryTreeNode{
private int data;
private BinaryTreeNode left;
private BinaryTreeNode right;
public BinaryTreeNode(int val){
this.data = val;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
this.right = right;
}
}

• 前序遍历：
• 后序遍历：
• 中序遍历：
• 层次遍历：

private void preOrder(BinaryTreeNode root){
System.out.print(""+root.getData());
preOrder(root.getLeft());
preOrder(root.getRight());
}

private void preOrderNoRecursive(BinaryTreeNode root){
if (root == null) {
return;
}
Stack<BinaryTreeNode> stack = new Stack<>();
while (true) {
while (root != null) {
System.out.print(""+root.getData());
stack.push(root);
root = root.getLeft();
}
if (stack.isEmpty()) {
break;
}
root = stack.pop();
root = root.getRight();
}
return;
}

private void inOrder(BinaryTreeNode root){
inOrder(root.getLeft());
System.out.print(""+root.getData());
inOrder(root.getRight());
}

private void inOrderNonRecursive(BinaryTreeNode root){
if (root == null) {
return;
}
Stack<BinaryTreeNode> stack = new Stack();
while (true) {
while (root != null) {
stack.push(root);
root = root.getLeft;
}
if (stack.isEmpty()) {
break;
}
root = stack.pop();
System.out.print(""+root.getData());
root = root.getRight();
}
}

private void postOrder(BinaryTreeNode root){
postOrder(root.getLeft());
postOrder(root.getRight());
System.out.print(""+root.getData());
}

private void postOrderNonRecursive(BinaryTreeNode root){
Stack<BinaryTreeNode> stack = new Stack<>();
while (true) {
if (root != null) {
stack.push(root);
root = root.getLeft();
}else {
if (stack.isEmpty()) {
System.out.print("empty tree!");
return;
}else {
if(stack.peek().getRight() == null) {
root = stack.pop();
System.out.print(root.getData());
if (root == stack.peek().getRight()) {
System.out.print(root.getData());
stack.pop();
}
}
}
if (!stack.isEmpty()) {
root = stack.peek().getRight();
}else{
root = null;
}
}
}
}

private void levelOrder(BinaryTreeNode root){
if (root == null) {
return;
}
Queue<BinaryTreeNode> queue = new Queue<>();
while(!queue.isEmpty()){
}
}
}
}

public TreeNode createTree(String preOrder, String inOrder){
if (preOrder.isEmpty()){
return null;
}
char rootValut = preOrder.charAt(0);
int rootIndex = inOrder.indexOf(rootValut);
TreeNode root = new TreeNode(rootValue);
root.setLeft(createTree(preOrder.substring(1,1+rootIndex), inOrder.substring(0,rootIndex)));
root.setRight(createTree(preOrder.substring(1+rootIndex),inOrder.substring(1 + rootIndex)));
return root;
}

public String postOrder(String preOrder, String inOrder){
if (preOrder.isEmpty()){
return null;
}
char rootValut = preOrder.charAt(0);
int rootIndex = inOrder.indexOf(rootValut);
return postOrder(preOrder.substring(1, 1+rootIndex), inOrder.substring(0, rootIndex)) +
postOrder(preOrder.substring(1+rootIndex), inOrder.substring(1 + rootIndex))
+ rootValut;
}

？1、构建二叉树唯一吗？
？2、不唯一的话有多少个？
？3、能够输出全部可能中序吗？

private int findMax(BinaryTreeNode root){
int rootValue,left,right,max = 0;
if (root != null) {
rootValue = root.getData();
left = findMax(root.getLeft());
right = findMax(root.getRight());
if (left > right) {
max = left;
}else {
max = right;
}
if (rootValue > max) {
max = rootValue;
}
}
return max;
}

private int findMaxUsingLevelOrder(BinaryTreeNode root){
int max;
BinaryTreeNode tmp;
queue.offer(root);

while (!queue.isEmpty()) {
tmp = queue.pop();
if (tmp.getData() > max) {
max = tmp.getData();
}
if (tmp.getLeft != null) {
queue.offer(tmp.getLeft());
}
if (tmp.getRight() != null) {
queue.offer(tmp.getRight());
}
}
return max;
}

private boolean findInBinaryTreeNodeRecursion(BinaryTreeNode root, int data){
if (root == null) {
return false;
}
if (root.getData() == data) {
return true;
}
return findInBinaryTreeNodeRecursion(root.getLeft(),data) || findInBinaryTreeNodeRecursion(root.getRight(),data);
}

private boolean findInBinaryUsingLevelOrder(BinaryTreeNode root, int data){
BinaryTreeNode tmp;
queue.offer(root);
while(!queue.isEmpth()){
tmp = queue.poll();
if (tmp.getData == data) {
return true;
}
if (tmp.getLeft() != null) {
queue.offer(tmp.getLeft());
}
if (tmp.getRight() != null) {
queue.offer(tmp.getRight());
}
}
return false;
}

private BinaryTreeNode parent;
public void setLeft(BinaryTreeNode left) {
this.left = left;
if (left != null)
this.left.setParent(this);
}
public void setRight(BinaryTreeNode right) {
this.right = right;
if (right != null)
this.right.setParent(this);
}
public BinaryTreeNode getParent() {
return parent;
}
public void setParent(BinaryTreeNode parent) {
this.parent = parent;
}

public BinaryTreeNode next(BinaryTreeNode node){
if (node.getRight() != null) {
node = node.getRight();
while(node.getLeft() != null){
node = node.getLeft();
}
return node;
}else {
if (node.getParent().getLeft() == node) {
return node.getParent();
}else {
while (node.getParent() != null && node.getParent().getLeft() != node) {
node = node.getParent();
}
return node.getParent;
}
}
}

### 推荐阅读更多精彩内容

• 栈 1. 栈（stack）又名堆栈，它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被...
程序员高级码农阅读 4,217评论 0 11
• 一些概念 数据结构就是研究数据的逻辑结构和物理结构以及它们之间相互关系，并对这种结构定义相应的运算，而且确保经过这...
Winterfell_Z阅读 2,464评论 0 10
• 数据结构和算法--二叉树的实现 几种二叉树 1、二叉树 和普通的树相比，二叉树有如下特点： 每个结点最多只有两棵子...
sunhaiyu阅读 3,431评论 0 13
• 目录 1、什么是树 2、相关术语 3、二叉树 3.1、二叉树的类型 3.2、二叉树的性质 3.3、二叉树的结构 3...
我哈啊哈啊哈阅读 1,158评论 0 9
• 开发进展 支持链式交易：可以在内存池中之前交易的基础上进行新交易 Script 支持多重签名 在 integrat...
ContentBox阅读 77评论 0 0