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

15 篇文章 0 订阅
9 篇文章 0 订阅

/**
 **	线索化二叉树
 ** 代码是在之前二叉树代码的基础上增加,
 **	重点关注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);
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值