二叉树的遍历(递归与非递归)java实现
2016-12-08 21:30
411 查看
三种遍历的递归实现:
[java] view
plain copy
public class Node {//二叉树节点
private int data;
private Node leftNode;
private Node rightNode;
public Node(int data, Node leftNode, Node rightNode){
this.data = data;
this.leftNode = leftNode;
this.rightNode = rightNode;
}
public int getData(){
return data;
}
public void setData(int data){
this.data = data;
}
public Node getLeftNode(){
return leftNode;
}
public void setLeftNode(Node leftNode){
this.leftNode = leftNode;
}
public Node getRightNode(){
return rightNode;
}
public void setRightNode(Node rightNode){
this.rightNode = rightNode;
}
}
三种遍历的递归实现:
[java] view
plain copy
public class BinaryTree_DiGui {
/*
* 二叉树先序中序后序排序
* 方式:递归。
*/
//注意必须逆序简历,先建立子节点,再逆序往上建立,
//因为非叶子节点会使用到下面的节点,而初始化是按顺序初始化得,不逆序建立会报错
public static Node init(){
Node J = new Node(8, null, null);
Node H = new Node(4, null, null);
Node G = new Node(2, null, null);
Node F = new Node(7, null, J);
Node E = new Node(5, H, null);
Node D = new Node(1, null, G);
Node C = new Node(9, F, null);
Node B = new Node(3, D, E);
Node A = new Node(6, B, C);
return A; //返回根节点
}
//打印节点数值
public static void printNode(Node node){
System.out.print(node.getData());
}
//先序遍历
public static void preOrder(Node root){
printNode(root);//打印根节点
if(root.getLeftNode() != null){//使用递归遍历左孩子
preOrder(root.getLeftNode());
}
if(root.getRightNode() != null){//使用递归遍历右孩子
preOrder(root.getRightNode());
}
}
//中序遍历
public static void inOrder(Node root){
if(root.getLeftNode() != null){//使用递归遍历左孩子
inOrder(root.getLeftNode());
}
printNode(root);//打印根节点
if(root.getRightNode() != null){//使用递归遍历右孩子
inOrder(root.getRightNode());
}
}
//后续遍历
public static void postOrder(Node root){
if(root.getLeftNode() != null){//使用递归遍历左孩子
postOrder(root.getLeftNode());
}
if(root.getRightNode() != null){//使用递归遍历右孩子
postOrder(root.getRightNode());
}
printNode(root);//打印根节点
}
public static void main(String[] args){
// BinaryTree tree = new BinaryTree();//注释掉本行后类中方法需变为static
Node root = init();
System.out.println("先序遍历");
preOrder(root);
System.out.println("");
System.out.println("中序遍历");
inOrder(root);
System.out.println("");
System.out.println("后序遍历");
postOrder(root);
System.out.println("");
}
}
通过栈实现三种遍历(非递归):
[java] view
plain copy
public class BinaryTree_Zhan {
/*
*
* 二叉树先序中序后序排序
* 方式:采用非递归方式。
*/
//注意必须逆序简历,先建立子节点,再逆序往上建立,
//因为非叶子节点会使用到下面的节点,而初始化是按顺序初始化得,不逆序建立会报错
public static Node init(){
Node J = new Node(8, null, null);
Node H = new Node(4, null, null);
Node G = new Node(2, null, null);
Node F = new Node(7, null, J);
Node E = new Node(5, H, null);
Node D = new Node(1, null, G);
Node C = new Node(9, F, null);
Node B = new Node(3, D, E);
Node A = new Node(6, B, C);
return A; //返回根节点
}
//打印节点数值
public static void printNode(Node node){
System.out.print(node.getData());
}
public static void preOrder_stack(Node root){//先序遍历
Stack<Node> stack = new Stack<Node>();
Node node = root;
while(node != null || stack.size()>0){//将所有左孩子压栈
if(node != null){//压栈之前先访问
printNode(node);
stack.push(node);
node = node.getLeftNode();
}else{
node = stack.pop();
node = node.getRightNode();
}
}
}
public static void inOrder_Stack(Node root){//中序遍历
Stack<Node> stack = new Stack<Node>();
Node node = root;
while(node != null || stack.size()>0){
if(node != null){
stack.push(node);//直接压栈
node = node.getLeftNode();
}else{
node = stack.pop();//出栈并访问
printNode(node);
node = node.getRightNode();
}
}
}
public static void postOrder_Stack(Node root){//后续遍历
Stack<Node> stack = new Stack<Node>();
Stack<Node> output = new Stack<Node>();//构造一个中间栈来存储逆后续遍历的结果
Node node = root;
while(node != null || stack.size()>0){
if(node != null){
output.push(node);
stack.push(node);
node = node.getRightNode();
}else{
node = stack.pop();
node = node.getLeftNode();
}
}
while(output.size()>0){
printNode(output.pop());
}
}
public static void main(String[] args){
Node root = init();
System.out.println("先序遍历");
preOrder_stack(root);
System.out.println("");
System.out.println("中序遍历");
inOrder_Stack(root);
System.out.println("");
System.out.println("后序遍历");
postOrder_Stack(root);
System.out.println("");
}
}
[java] view
plain copy
public class Node {//二叉树节点
private int data;
private Node leftNode;
private Node rightNode;
public Node(int data, Node leftNode, Node rightNode){
this.data = data;
this.leftNode = leftNode;
this.rightNode = rightNode;
}
public int getData(){
return data;
}
public void setData(int data){
this.data = data;
}
public Node getLeftNode(){
return leftNode;
}
public void setLeftNode(Node leftNode){
this.leftNode = leftNode;
}
public Node getRightNode(){
return rightNode;
}
public void setRightNode(Node rightNode){
this.rightNode = rightNode;
}
}
三种遍历的递归实现:
[java] view
plain copy
public class BinaryTree_DiGui {
/*
* 二叉树先序中序后序排序
* 方式:递归。
*/
//注意必须逆序简历,先建立子节点,再逆序往上建立,
//因为非叶子节点会使用到下面的节点,而初始化是按顺序初始化得,不逆序建立会报错
public static Node init(){
Node J = new Node(8, null, null);
Node H = new Node(4, null, null);
Node G = new Node(2, null, null);
Node F = new Node(7, null, J);
Node E = new Node(5, H, null);
Node D = new Node(1, null, G);
Node C = new Node(9, F, null);
Node B = new Node(3, D, E);
Node A = new Node(6, B, C);
return A; //返回根节点
}
//打印节点数值
public static void printNode(Node node){
System.out.print(node.getData());
}
//先序遍历
public static void preOrder(Node root){
printNode(root);//打印根节点
if(root.getLeftNode() != null){//使用递归遍历左孩子
preOrder(root.getLeftNode());
}
if(root.getRightNode() != null){//使用递归遍历右孩子
preOrder(root.getRightNode());
}
}
//中序遍历
public static void inOrder(Node root){
if(root.getLeftNode() != null){//使用递归遍历左孩子
inOrder(root.getLeftNode());
}
printNode(root);//打印根节点
if(root.getRightNode() != null){//使用递归遍历右孩子
inOrder(root.getRightNode());
}
}
//后续遍历
public static void postOrder(Node root){
if(root.getLeftNode() != null){//使用递归遍历左孩子
postOrder(root.getLeftNode());
}
if(root.getRightNode() != null){//使用递归遍历右孩子
postOrder(root.getRightNode());
}
printNode(root);//打印根节点
}
public static void main(String[] args){
// BinaryTree tree = new BinaryTree();//注释掉本行后类中方法需变为static
Node root = init();
System.out.println("先序遍历");
preOrder(root);
System.out.println("");
System.out.println("中序遍历");
inOrder(root);
System.out.println("");
System.out.println("后序遍历");
postOrder(root);
System.out.println("");
}
}
通过栈实现三种遍历(非递归):
[java] view
plain copy
public class BinaryTree_Zhan {
/*
*
* 二叉树先序中序后序排序
* 方式:采用非递归方式。
*/
//注意必须逆序简历,先建立子节点,再逆序往上建立,
//因为非叶子节点会使用到下面的节点,而初始化是按顺序初始化得,不逆序建立会报错
public static Node init(){
Node J = new Node(8, null, null);
Node H = new Node(4, null, null);
Node G = new Node(2, null, null);
Node F = new Node(7, null, J);
Node E = new Node(5, H, null);
Node D = new Node(1, null, G);
Node C = new Node(9, F, null);
Node B = new Node(3, D, E);
Node A = new Node(6, B, C);
return A; //返回根节点
}
//打印节点数值
public static void printNode(Node node){
System.out.print(node.getData());
}
public static void preOrder_stack(Node root){//先序遍历
Stack<Node> stack = new Stack<Node>();
Node node = root;
while(node != null || stack.size()>0){//将所有左孩子压栈
if(node != null){//压栈之前先访问
printNode(node);
stack.push(node);
node = node.getLeftNode();
}else{
node = stack.pop();
node = node.getRightNode();
}
}
}
public static void inOrder_Stack(Node root){//中序遍历
Stack<Node> stack = new Stack<Node>();
Node node = root;
while(node != null || stack.size()>0){
if(node != null){
stack.push(node);//直接压栈
node = node.getLeftNode();
}else{
node = stack.pop();//出栈并访问
printNode(node);
node = node.getRightNode();
}
}
}
public static void postOrder_Stack(Node root){//后续遍历
Stack<Node> stack = new Stack<Node>();
Stack<Node> output = new Stack<Node>();//构造一个中间栈来存储逆后续遍历的结果
Node node = root;
while(node != null || stack.size()>0){
if(node != null){
output.push(node);
stack.push(node);
node = node.getRightNode();
}else{
node = stack.pop();
node = node.getLeftNode();
}
}
while(output.size()>0){
printNode(output.pop());
}
}
public static void main(String[] args){
Node root = init();
System.out.println("先序遍历");
preOrder_stack(root);
System.out.println("");
System.out.println("中序遍历");
inOrder_Stack(root);
System.out.println("");
System.out.println("后序遍历");
postOrder_Stack(root);
System.out.println("");
}
}
相关文章推荐
- 我用Java实现的二叉树的遍历(递归和非递归)
- java语言实现的二叉树的各种操作(包括递归与非递归遍历二叉树,求二叉树的高度,节点总数,叶子节点等)
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归)
- java实现的二叉树(前序、中序、后序)递归和非递归遍历,包含层序遍历
- Java实现二叉树的遍历(递归和非递归)
- 二叉树的建树、遍历(先序、中序、后序、层次)(递归和非递归)--Java实现
- 二叉树的几种遍历递归与非递归java实现
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归) 层次遍历
- JAVA实现二叉树的前、中、后序遍历(递归与非递归)
- java实现二叉树的前中后遍历(递归和非递归)
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- 二叉树先中后序遍历(递归、非递归方法)、层序遍历 Java实现
- 【python中二叉树的实现】python中二叉树的创建、三种方式递归遍历和非递归遍历
- 算法:二叉树的先(根)序遍历、中(根)序遍历、后(根)序遍历(递归及压栈出栈实现的非递归方式)的java代码实现
- Java实现的二叉树常用操作【前序建树,前中后递归非递归遍历及层序遍历】
- 用java实现二叉树的前序、中序、后序、层次遍历(递归和非递归版)
- 二叉树的前序,中序,后续,递归及非递归遍历的python实现
- Java实现二叉树后序非递归遍历
- 超级详细的二叉树的3种遍历方法的递归和非递归的实现
- Java实现非递归遍历二叉树