dfs 非递归java_Java数据结构——二叉树的递归与非递归遍历(DFS)

二叉树的遍历分为递归遍历和非递归遍历

一、递归实现前、中、后序遍历

Node.java:

public class Node {

private Object data;

Node richild;

Node lechild;

public Object getData() {

return data;

}

public void setData(Object data) {

this.data = data;

}

public Node getRichild() {

return richild;

}

public void setRichild(Node richild) {

this.richild = richild;

}

public Node getLechild() {

return lechild;

}

public void setLechild(Node lechild) {

this.lechild = lechild;

}

public Node(Object data, Node lechild, Node richild) {

super();

this.data = data;

this.richild = richild;

this.lechild = lechild;

}

public Node() {

super();

}

}

递归遍历:

public class BTree {

private static Node root;

//构造树

public static void init() {

Node node1 = new Node("A", null, null);

Node node2 = new Node("B", node1, null);

Node node3 = new Node("C", null, null);

Node node4 = new Node("D", node2, node3);

Node node5 = new Node("E", null, null);

Node node6 = new Node("F", null, node5);

Node node7 = new Node("G", node4, node6);

root = node7;

}

//访问节点

public static void visited(Node n) {

System.out.print(n.getData() + " ");

}

//前序遍历

public static void preOrder(Node n) {

if (n != null) {

visited(n);

preOrder(n.getLechild());

preOrder(n.getRichild());

}

}

//中序遍历

public static void inOrder(Node n) {

if (n != null) {

inOrder(n.getLechild());

visited(n);

inOrder(n.getRichild());

}

}

//后序遍历

public static void postOrder(Node n) {

if (n != null) {

postOrder(n.getLechild());

postOrder(n.getRichild());

visited(n);

}

}

public static void main(String[] args) {

init();

System.out.print("递归前序:");

preOrder(root);

System.out.println();

System.out.print("递归中序:");

inOrder(root);

System.out.println();

System.out.print("递归后序:");

postOrder(root);

System.out.println();

}

}

二、非递归实现前、中、后序遍历

import java.util.Stack;

public class BTree2 {

private static Node root;

//构造树

public static void init() {

Node node1 = new Node("A", null, null);

Node node2 = new Node("B", node1, null);

Node node3 = new Node("C", null, null);

Node node4 = new Node("D", node2, node3);

Node node5 = new Node("E", null, null);

Node node6 = new Node("F", null, node5);

Node node7 = new Node("G", node4, node6);

root = node7;

}

//前序遍历

public static void preOrder(Node n) {

System.out.print("非递归前序:");

Stack stack = new Stack<>();

int index = 0;

while (n != null || index > 0) {

while (n != null) {

System.out.print(n.getData() + " ");

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.pop();

index--;

n = n.getRichild();

}

}

//中序遍历

public static void inOrder(Node n) {

System.out.print("非递归中序:");

Stack stack = new Stack<>();

int index = 0;

while (n != null || index > 0) {

while (n != null) {

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.pop();

System.out.print(n.getData() + " ");

index--;

n = n.getRichild();

}

}

//后序遍历

public static void postOrder(Node n) {

System.out.print("非递归后序:");

Stack stack = new Stack<>();

int index = 0;

Node lastVisited = null;

while (n != null || index > 0) {

while (n != null) {

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.peek();

if (n.getRichild() == null || n.getRichild() == lastVisited) {

System.out.print(n.getData() + " ");

lastVisited = n;

index--;

stack.pop();

n = null;

} else {

n = n.getRichild();

}

}

}

public static void main(String[] args) {

init();

preOrder(root);

System.out.println();

inOrder(root);

System.out.println();

postOrder(root);

System.out.println();

}

}

三、查找最大值

// 查找最大值

public static Node maxNode() {

Node node = root;

Node maxNode = node;

while (node != null) {

maxNode = node;

node = node.getRichild();

}

return maxNode;

}

四、查找最小值

// 查找最小值

public static Node minNode() {

Node node = root;

Node minNode = node;

while (node != null) {

minNode = node;

node = node.getLechild();

}

return minNode;

}

五、插入节点

// 插入节点

public static boolean insert(Object data, Node parent) {

Node node = new Node(data, null, null);

if (root == null || parent == null) {

root = node;

return true;

} else if (parent.getLechild() != null && parent.getRichild() != null) {

return false;

} else {

if (parent.getLechild() != null) {

parent.setRichild(node);

} else {

parent.setLechild(node);

}

return true;

}

}

六、查找节点

// 查找节点

public static void find(Node n, Object data) {

if (n != null) {

if (n.getData() == data) {

System.out.println(n);

return;

} else {

find(n.getLechild(), data);

find(n.getRichild(), data);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值