遍历二叉树(广度,深度,递归,非递归)
import java.util.*;
class Node {
Node left;
Node right;
int key;
public Node(int key) {
this.key = key;
}
}
public class BTree {
Node root;
/**
* the constructor
**/
public BTree() {
this.root = null;
}
public BTree(int[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException();
}
root = new Node(array[0]);
Queue<Node> queue = new LinkedList<Node>(); queue.offer(root);
int i = 1;
while(i < array.length) {
if (!queue.isEmpty()) {
Node p = queue.poll();
p.left = new Node(array[i]);
queue.offer(p.left);
i++;
if (i < array.length) {
p.right = new Node(array[i]);
queue.offer(p.right);
i++;
}
}
}
}
****************
* 广度优先遍历
*
********************************************************************/
public void breadthFirstTraverse() {
Node p = root;
if (p != null) {
Queue<Node> queue = new LinkedList<Node>(); queue.offer(p);
while (!queue.isEmpty()) {
p = queue.poll();
System.out.println(p.key);
if (p.left != null) {
queue.offer(p.left);
}
if (p.right != null) {
queue.offer(p.right);
}
}
}
}
****************
* 深度优先遍历
*
********************************************************************/
/**
* iterative traverse
*
**/
public void iterativePreorder() {
Node p = root;
Stack<Node> travStack = new Stack<Node>();
if (p != null) {
travStack.push(p);
while (!travStack.isEmpty()) {
p = travStack.pop();
System.out.println(p.key);
if (p.right != null)
travStack.push(p.right);
if (p.left != null) // left child pushed after right
travStack.push(p.left); // to be on the top of the stack;
}
}
}
public void iterativeInorder() {
Node p = root;
Stack<Node> travStack = new Stack<Node>();
while (p != null) {
while(p != null) { // stack the right child (if any)
if (p.right != null) // and the node itself when going
travStack.push(p.right); // to the left;
travStack.push(p);
p = p.left;
}
p = travStack.pop(); // pop a node with no left child
while (!travStack.isEmpty() && p.right == null) { // visit it and all
System.out.println(p.key); // nodes with no right child;
p = travStack.pop();
}
System.out.println(p.key); // visit also the first node with
if (!travStack.isEmpty()) // a right child (if any);
p = travStack.pop();
else p = null;
}
}
public void iterativeInorderPlus() {
Node p = root;
Stack<Node> travStack = new Stack<Node>();
travStack.push(p);
while (!travStack.empty()) {
while ((p = travStack.peek()) != null) {//走到尽头
travStack.push(p.left);
}
travStack.pop(); //pop the null element
if (!travStack.empty()) { p = travStack.pop(); //结点,向右一步
System.out.println(p.key);
travStack.push(p.right);
}
}
}
向左访问
public void iterativeInorderPlusPlus() {
Node p = root;
Stack<Node> travStack = new Stack<Node>();
while (p != null || !travStack.empty()) {
if (p != null) { //根指针进栈,遍历左子树
travStack.push(p);
p = p.left;
} else { //根指针退栈,访问根结点,遍历右子树
p = travStack.pop();
System.out.println(p.key);
p = p.right;
}
}
}
public void iterativePostorder() {
Node p = root;
Stack<Node> travStack = new Stack<Node>(), output = new Stack<Node>();
if (p != null) { // left-to-right postorder = right-to-left preorder
travStack.push(p);
while (!travStack.isEmpty()) {
output.push(p);
if (p.left != null)
travStack.push(p.left);
if (p.right != null)
travStack.push(p.right);
}
while (!output.isEmpty()) {
p = output.pop();
System.out.println(p.key);
}
}
}
public void iterativePostorderPlus() {
Node p = root;
Node q = null;
Stack<Node> travStack = new Stack<Node>();
while (p != null || !travStack.empty()) {
if (p != null) { //针进栈,遍历左子树
travStack.push(p);
p = p.left;
} else {
根指
//如果根结点没有右子树,
//或是右子树已经被访问,
//则访问根结点;否则遍历右子树
if (p.right == null || p.right == q) {
p = travStack.pop();
System.out.println(p.key);
q = p;
p = null;
} else {
p = p.right;
}
}
}
}
/**
* recursive traverse
*
*
**/
public void preOrderTraverse(Node p) {
if (p != null) {
System.out.println(p.key);
if (p.left != null) {
preOrderTraverse(p.left);
}
if (p.right != null) {
preOrderTraverse(p.right);
}
}
}
public void inOrderTraverse(Node p) {
if (p != null) {
if (p.left != null) {
inOrderTraverse(p.left);
}
System.out.println(p.key);
if (p.right != null) {
相关推荐:
- [行业范文]美好的法语句子
- [行业范文]描写露珠的句子
- [行业范文]精彩禅语句子图片
- [行业范文]关于满嘴谎言的句子
- [行业范文]关于安静的句子48句
- [行业范文]关于小河的句子
- [行业范文]描写稻田的句子
- [行业范文]思念好朋友的句子
- [行业范文]赞美雪的句子
- [行业范文]早上激励人心的句子
- [行业范文]失恋忧伤的句子
- [行业范文]努力积极向上的句子
- [行业范文]对工作心灰意冷的句子
- [行业范文]失恋让人心疼的句子
- [行业范文]描写珍惜青春的句子
- [行业范文]表达思念的句子简短
- [行业范文]关于父爱的句子范例
- [行业范文]浪漫的英语句子
- [行业范文]关于周末的句子
- [行业范文]思念牵挂的句子
- 有关感恩班会课件简短(二篇)(感恩班会
- 2025年初二下乡军训心得体会800字(15篇
- 关于新员工培训方案汇编(关于新员工培
- 精选高考生寒假学习计划书(精)(高考生
- 毕业实训报告心得体会(3篇)(实训报告心
- 银行工作感悟及心得范文怎么写(四篇)(
- 精选领导干部个人政治画像报告通用(七
- 精选超市11.11活动促销方案(精品超市品
- 2025年怎么做自我介绍汇总(5篇)(至2025
- 最新企业错峰生产方案(26篇)(山西企业
- 最新暑期三下乡社会实践调研报告范本(
- 最新幼儿园大班教育教学总结怎么写(最
- 最新教师节主持词小学(优秀9篇)(教师节
- 关于小学安全教育教学方案(推荐)(关于
- 员工信模板范文怎么写(五篇)(员工信息
- 最新保险销售离职申请书(十六篇)(最新
- 最新XX小学防校园欺凌工作方案怎么写(2
- 有关特岗教师辞职信范文(推荐)(特岗教
- 精选党的建设工作要点简短(党的建设的
- 如何写安康杯竞赛活动总结汇总(4篇)(安




