【java】【二叉树】【线索化】


/**
 **	线索化二叉树
 ** 代码是在之前二叉树代码的基础上增加,
 **	重点关注threadedNodes()方法。
 **	线索化直接理解代码有点困难。
 **	但是可以记一个重点:
 **	关键在于充分利用节点的左右指针。
 **	可以配合画图、代码一起理解,效果较好
 **
 */
public class BinaryTreeDemo {

	public static void main(String[] args) {
		HeroNode1 root = new HeroNode1(1, "tom");
		HeroNode1 node2 = new HeroNode1(3, "jack");
		HeroNode1 node3 = new HeroNode1(6, "smith");
		HeroNode1 node4 = new HeroNode1(8, "marry");
		HeroNode1 node5 = new HeroNode1(10, "king");
		HeroNode1 node6 = new HeroNode1(14, "dim");
		
		root.setLeft(node2);
		root.setRight(node3);
		node2.setLeft(node4);
		node2.setRight(node5);
		node3.setLeft(node6);
		
		BinaryTree1 threadedTree = new BinaryTree1();
		threadedTree.setRoot(root);
		threadedTree.threadedNodes(root);
		
		System.out.println( "前驱节点是:" + node5.getLeft() );
		System.out.println( "后继节点是:" + node5.getRight() );
		
	}

}

class BinaryTree1 {
	private HeroNode1 node;
	private HeroNode1 pre = null;

	public void setRoot(HeroNode1 root) {
		this.node = root;
	}
	
	public void threadedNodes(HeroNode1 node){
		if( node == null ){
			return;
		}
		
		threadedNodes(node.getLeft());
		
		if( node.getLeft() == null ){
			node.setLeft(pre);
			node.setLeftType(1);
		}
		
		if( pre !=null && pre.getRight() == null ){
			pre.setRight(node);
			pre.setRightType(1);
		}
		
		pre = node;
		
		threadedNodes(node.getRight());
		
	}

	public void preOrder() {
		if (node != null) {
			this.node.preOrder();
		} else {
			System.out.println("二叉树为空,无法遍历");
		}
	}

	public void infixOrder() {
		if (node != null) {
			this.node.infixOrder();
		} else {
			System.out.println("二叉树为空,无法遍历");
		}
	}

	public void postOrder() {
		if (node != null) {
			this.node.postOrder();
		} else {
			System.out.println("二叉树为空,无法遍历");
		}
	}

	public HeroNode1 preSearch(int num) {
		return this.node.preSearch(num);
	}

	public HeroNode1 infixSearch(int num) {
		return this.node.infixSearch(num);
	}

	public HeroNode1 postSearch(int num) {
		return this.node.postSearch(num);
	}

	public void deleteNote(int num) {
		if (node != null) {
			if (node.getNum() == num) {
				node = null;
			} else {
				node.deleteNode(num);
			}
		} else {
			System.out.println("空树,无法删除~~~~~");
		}
	}
}

class HeroNode1 {
	private int num;
	private String name;
	private HeroNode1 left;
	private HeroNode1 right;
	
	private int leftType;
	private int rightType;
	
	public int getLeftType() {
		return leftType;
	}

	public void setLeftType(int leftType) {
		this.leftType = leftType;
	}

	public int getRightType() {
		return rightType;
	}

	public void setRightType(int rightType) {
		this.rightType = rightType;
	}

	public HeroNode1(int num, String name) {
		this.num = num;
		this.name = name;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public HeroNode1 getLeft() {
		return left;
	}

	public void setLeft(HeroNode1 left) {
		this.left = left;
	}

	public HeroNode1 getRight() {
		return right;
	}

	public void setRight(HeroNode1 right) {
		this.right = right;
	}

	@Override
	public String toString() {
		return "HeroNode1 [num=" + num + ", name=" + name + "]";
	}

	/**
	 * 前序遍历
	 */
	public void preOrder() {
		System.out.println(this);
		if (this.left != null) {
			this.left.preOrder();
		}

		if (this.right != null) {
			this.right.preOrder();
		}
	}

	/**
	 * 中序遍历
	 */
	public void infixOrder() {
		if (this.left != null) {
			this.left.infixOrder();
		}

		System.out.println(this);

		if (this.right != null) {
			this.right.infixOrder();
		}
	}

	/**
	 * 后序遍历
	 */
	public void postOrder() {
		if (this.left != null) {
			this.left.postOrder();
		}

		if (this.right != null) {
			this.right.postOrder();
		}

		System.out.println(this);
	}

	/**
	 * 前序搜索
	 * 
	 * @param num
	 * @return
	 */
	public HeroNode1 preSearch(int num) {
		System.out.println("前序搜索~~~~~~~~");
		if (this.num == num) {
			return this;
		}
		HeroNode1 resNode = null;
		if (this.left != null) {
			resNode = this.left.preSearch(num);
		}

		if (resNode != null) {
			return resNode;
		}

		if (this.right != null) {
			resNode = this.right.preSearch(num);
		}
		return resNode;
	}

	/**
	 * 中序搜索
	 * 
	 * @param num
	 * @return
	 */
	public HeroNode1 infixSearch(int num) {

		HeroNode1 resultNode = null;
		if (this.left != null) {
			resultNode = this.left.infixSearch(num);
		}

		if (resultNode != null) {
			return resultNode;
		}
		System.out.println("中序搜索开始~~~~");
		if (this.num == num) {
			return this;
		}

		if (this.right != null) {
			resultNode = this.right.infixSearch(num);
		}
		return resultNode;
	}

	/**
	 * 后序搜索
	 * 
	 * @param num
	 * @return
	 */
	public HeroNode1 postSearch(int num) {

		HeroNode1 resultNode = null;

		if (this.left != null) {
			resultNode = this.left.postSearch(num);
		}

		if (resultNode != null) {
			return resultNode;
		}

		if (this.right != null) {
			resultNode = this.right.postSearch(num);
		}

		if (resultNode != null) {
			return resultNode;
		}
		System.out.println("后序搜索开始~~~~");
		if (this.num == num) {
			return this;
		}
		return resultNode;
	}

	// 删除节点
	public void deleteNode(int num) {
		if (this.left != null && this.left.num == num) {
			this.left = null;
			return;
		}

		if (this.right != null && this.right.num == num) {
			this.right = null;
			return;
		}

		if (this.left != null) {
			this.left.deleteNode(num);
		}

		if (this.right != null) {
			this.right.deleteNode(num);
		}
	}

}

内容概要:该论文深入研究了液压挖掘机动臂下降势能回收技术,旨在解决传统液压挖掘机能耗高的问题。提出了一种新型闭式回路势能回收系统,利用模糊PI自整定控制算法控制永磁无刷直流电动机,实现了变转速容积调速控制,消除了节流和溢流损失。通过建立数学模型和仿真模型,分析了不同负载下的系统性能,并开发了试验平台验证系统的高效性和节能效果。研究还涵盖了执行机构能量分布分析、系统元件参数匹配及电机控制性能优化,为液压挖掘机节能技术提供了理论和实践依据。此外,通过实验验证,该系统相比传统方案可降低28%的能耗,控制系统响应时间缩短40%,为工程机械的绿色化、智能化发展提供了关键技术支撑。 适合人群:从事工程机械设计、制造及维护的工程师和技术人员,以及对液压系统节能技术感兴趣的科研人员。 使用场景及目标:①理解液压挖掘机闭式回路动臂势能回收系统的原理和优势;②掌握模糊PI自整定控制算法的具体实现;③学习如何通过理论建模、仿真和实验验证来评估和优化液压系统的性能。 其他说明:此研究不仅提供了详细的理论分析和数学建模,还给出了具体的仿真代码和实验数据,便于读者在实际工作中进行参考和应用。研究结果表明,该系统不仅能显著提高能源利用效率,还能延长设备使用寿命,降低维护成本,具有重要的工程应用价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值