BinaryTree-Traverse

本文介绍了一种使用Java实现二叉树的递归及非递归遍历的方法,包括前序、中序和后序遍历。通过具体的代码示例展示了如何构建二叉树并进行不同方式的遍历。
首先定义Node类
public class Node {
Node lchild;
Node rchild;
char data;
int status;
}

主函数:
public class BinaryTree {
static int index = 0;
public static void main(String[] args) {
char[] str = {'a','b','c','#','#','d','e','#','g','#','#','f','#','#','h','i','#','#','j','#','#'};
Node tree = null;
tree = CreateTree(str, tree);

PreOrder(tree);
System.out.println();
UnrecursivePreOrder(tree);
System.out.println();
UnrecursivePreOrder2(tree);
System.out.println();

inOrder(tree);
System.out.println();
UnrecursiveInorder(tree);
System.out.println();


PostOrder(tree);
System.out.println();
UnrecursivePostorder(tree);
System.out.println();
UnrecursivePostorder2(tree);
System.out.println();
UnrecursivePostorder3(tree);
System.out.println();
UnrecursivePostorder4(tree);
System.out.println();
}

public static Node CreateTree(char[] str, Node tree){
if(str[index] == '#'){
tree = null;
index++;
}
else{
tree = new Node();
tree.data = str[index]; index++;
tree.lchild = CreateTree(str, tree.lchild);
tree.rchild = CreateTree(str, tree.rchild);
}
return tree;
}

public static void PreOrder(Node tree){
if(tree != null){
System.out.print(tree.data+" ");
PreOrder(tree.lchild);
PreOrder(tree.rchild);
}
}


public static void PostOrder(Node tree){
if(tree != null){
PostOrder(tree.lchild);
PostOrder(tree.rchild);
System.out.print(tree.data+" ");
}
}

public static void inOrder(Node tree){
if(tree != null){
inOrder(tree.lchild);
System.out.print(tree.data+" ");
inOrder(tree.rchild);
}
}

public static void UnrecursivePreOrder(Node tree){
Stack<Node> stack = new Stack<Node>();
if (tree != null) {
stack.push(tree);
while (!stack.empty()) {
tree = stack.pop();
System.out.print(tree.data+" ");
if (tree.rchild != null)
stack.push(tree.rchild);
if (tree.lchild != null)
stack.push(tree.lchild);
}
}
}

public static void UnrecursivePreOrder2(Node tree) {
Stack<Node> stack = new Stack<Node>();
Node node = tree;
while (node != null || stack.size() > 0) {
while (node != null) {
System.out.print(node.data+" ");
stack.push(node);
node = node.lchild;
}
if (stack.size() > 0) {
node = stack.pop();
node = node.rchild;
}
}
}

public static void UnrecursiveInorder(Node tree) {
Stack<Node> stack = new Stack<Node>();
while (tree != null) {
while (tree != null) {
if (tree.rchild != null)
stack.push(tree.rchild);
stack.push(tree);
tree = tree.lchild;
}
tree = stack.pop();
while (!stack.empty() && tree.rchild == null) {
System.out.print(tree.data+" ");
tree = stack.pop();
}
System.out.print(tree.data+" ");
if (!stack.empty())
tree = stack.pop();
else
tree = null;
}
}

public static void UnrecursivePostorder(Node tree) {
Node q = tree;
Stack<Node> stack = new Stack<Node>();
while (tree != null) {
for (; tree.lchild != null; tree = tree.lchild)
stack.push(tree);
while (tree != null && (tree.rchild == null || tree.rchild == q)) {
System.out.print(tree.data+" ");
q = tree;
if (stack.empty())
return;
tree = stack.pop();
}
stack.push(tree);
tree = tree.rchild;
}
}

public static void UnrecursivePostorder2(Node tree) {
Stack<Node> lstack = new Stack<Node>();
Stack<Node> rstack = new Stack<Node>();
Node node = tree, right;
do {
while (node != null) {
right = node.rchild;
lstack.push(node);
rstack.push(right);
node = node.lchild;
}
node = lstack.pop();
right = rstack.pop();
if (right == null) {
System.out.print(node.data+" ");
} else {
lstack.push(node);
rstack.push(null);
}
node = right;
} while (lstack.size() > 0 || rstack.size() > 0);
}

public static void UnrecursivePostorder3(Node tree) {
Stack<Node> stack = new Stack<Node>();
Node node = tree, prev = tree;
while (node != null || stack.size() > 0) {
while (node != null) {
stack.push(node);
node = node.lchild;
}
if (stack.size() > 0) {
Node temp = stack.peek().rchild;
if (temp == null || temp == prev) {
node = stack.pop();
System.out.print(node.data+" ");
prev = node;
node = null;
} else {
node = temp;
}
}
}
}

public static void UnrecursivePostorder4(Node tree) {
Stack<Node> stack = new Stack<Node>();
Stack<Node> temp = new Stack<Node>();
Node node = tree;
while (node != null || stack.size() > 0) {
while (node != null) {
temp.push(node);
stack.push(node);
node = node.rchild;
}
if (stack.size() > 0) {
node = stack.pop();
node = node.lchild;
}
}
while (temp.size() > 0) {
node = temp.pop();
System.out.print(node.data+" ");
}
}
}
【无人车路径跟踪】基于神经网络的数据驱动迭代学习控制(ILC)算法,用于具有未知模型和重复任务的非线性单输入单输出(SISO)离散时间系统的无人车的路径跟踪(Matlab代码实现)内容概要:本文介绍了一种基于神经网络的数据驱动迭代学习控制(ILC)算法,用于解决具有未知模型和重复任务的非线性单输入单输出(SISO)离散时间系统的无人车路径跟踪问题,并提供了完整的Matlab代码实现。该方法无需精确系统模型,通过数据驱动方式结合神经网络逼近系统动态,利用迭代学习机制不断提升控制性能,从而实现高精度的路径跟踪控制。文档还列举了大量相关科研方向和技术应用案例,涵盖智能优化算法、机器学习、路径规划、电力系统等多个领域,展示了该技术在科研仿真中的广泛应用前景。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的研究生、科研人员及从事无人车控制、智能算法开发的工程技术人员。; 使用场景及目标:①应用于无人车在重复任务下的高精度路径跟踪控制;②为缺乏精确数学模型的非线性系统提供有效的控制策略设计思路;③作为科研复现与算法验证的学习资源,推动数据驱动控制方法的研究与应用。; 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注神经网络与ILC的结合机制,并尝试在不同仿真环境中进行参数调优与性能对比,以掌握数据驱动控制的核心思想与工程应用技巧。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值