java线索二叉树(含线索二叉树代码展示)

目录

一:java线索二叉树的介绍

二:java线索二叉树的展示

三:java线索二叉树的案例

四:java线索化二叉树代码展示

4.1 二叉树的线索化代码

4.2 中序遍历二叉树


一:java线索二叉树的介绍

线索二叉树的目的是去利用没有利用到的空指针

n个结点有n+1个空指针

对二叉树进行(先序、中序、后序的遍历)让其空指针指向前驱和后继的过程就是线索二叉树。

二:java线索二叉树的展示

中序遍历

三:java线索二叉树的案例

第一步:把普通的二叉树线索成线索二叉树

第二步:输出线索二叉树

四:java线索化二叉树代码展示

4.1 二叉树的线索化代码

//中序线索化二叉树
	public void clueNode(Node node) {
		if(node == null) {
			return;
		}
		
		clueNode(node.getLeft());
		if(node.getLeft() == null) {
			node.setLeft(pre);
			node.setLeftno(1);
		}
		
		if(pre != null && pre.getRight() == null) {
			pre.setRight(node);
			pre.setRightno(1);
		}
		
		pre = node;
		
		clueNode(node.getRight());
	}
	

4.2 中序遍历二叉树

//中序遍历二叉树
	public void preSelect() {
		Node node =root;
		while(node != null) {
			while(node.getLeftno() == 0) {
				node = node.getLeft();
			}
			System.out.println(node);
			while(node.getRightno() == 1) {
				node = node.getRight();
				System.out.print(node);
			}
			node = node.getRight();
		}
	}

全部代码

package tree;

public class Node {
	private int no;
	private String name;
	
	private Node left;
	private Node right;
	
	private int leftno;
	private int rightno;
	
	public Node(int no, String name) {
		this.no = no;
		this.name = name;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

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

	public Node getLeft() {
		return left;
	}

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

	public Node getRight() {
		return right;
	}

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

	public int getLeftno() {
		return leftno;
	}

	public void setLeftno(int leftno) {
		this.leftno = leftno;
	}

	public int getRightno() {
		return rightno;
	}

	public void setRightno(int rightno) {
		this.rightno = rightno;
	}

	@Override
	public String toString() {
		return "Node [no=" + no + ", name=" + name + "]";
	}
	
	public void preSelect() {
		System.out.println(this);
		if(this.getLeft() != null) {
			this.getLeft().preSelect();
		}
		if(this.getRight() != null) {
			this.getRight().preSelect();
		}
	}
	public void indexSelect() {
		if(this.getLeft() != null) {
			this.getLeft().indexSelect();
		}
		System.out.println(this);
		if(this.getRight() != null) {
			this.getRight().indexSelect();
		}
	}
	public void postSelect() {
		if(this.getLeft() != null) {
			this.getLeft().postSelect();
		}
		if(this.getRight() != null) {
			this.getRight().postSelect();
		}
		System.out.println(this);
	}
	public Node preId(int no) {
		if(this.getNo() == no) {
			return this;
		}
		Node node = null;
		if(this.getLeft() != null) {
			node = this.getLeft().preId(no);
		}
		if(node != null) {
			return node;
		}
		if(this.getRight() != null) {
			node = this.getRight().preId(no);
		}
		return node;
	}
	public Node indexId(int no) {
		Node node = null;
		if(this.getLeft() != null) {
			node = this.getLeft().indexId(no);
		}
		if(node != null) {
			return node;
		}
		if(this.getNo() == no) {
			return this;
		}
		if(this.getRight() != null) {
			node = this.getRight().indexId(no);
		}
		return node;
	}
	public Node postId(int no) {
		Node node = null;
		if(this.getLeft() != null) {
			node = this.getLeft().postId(no);
		}
		if(node != null) {
			return node;
		}
		if(this.getRight() != null) {
			node = this.getRight().postId(no);
		}
		if(node != null) {
			return node;
		}
		if(this.getNo() == no) {
			return this;
		}
		return node;
	}
	//删除
	public void delNode(int no) {
		if(this.getLeft().getNo() == no) {
			this.left = null;
			return;
		}
		if(this.getRight().getNo() == no) {
			this.right = null;
			return;
		}
		if(this.getLeft() != null) {
			this.left.delNode(no);
		}
		if(this.getRight() != null) {
			this.right.delNode(no);
		}
	}
	
	

}
package tree;

public class ClueTree {
	private Node root;
	private Node pre;
	
	public void setRoot(Node node) {
		this.root = node;
	}
	
	public void clueNode() {
		this.clueNode(root);
	}
	//中序线索化二叉树
	public void clueNode(Node node) {
		if(node == null) {
			return;
		}
		
		clueNode(node.getLeft());
		if(node.getLeft() == null) {
			node.setLeft(pre);
			node.setLeftno(1);
		}
		
		if(pre != null && pre.getRight() == null) {
			pre.setRight(node);
			pre.setRightno(1);
		}
		
		pre = node;
		
		clueNode(node.getRight());
	}
	
	//中序遍历二叉树
	public void preSelect() {
		Node node =root;
		while(node != null) {
			while(node.getLeftno() == 0) {
				node = node.getLeft();
			}
			System.out.println(node);
			while(node.getRightno() == 1) {
				node = node.getRight();
				System.out.print(node);
			}
			node = node.getRight();
		}
	}
	
}
package tree;

public class Test {
	public static void main(String[] args) {
		Node root = new Node(1,"吕布");
		Node node2 = new Node(3,"貂蝉");
		Node node3 = new Node(6,"曹操");
		Node node4 = new Node(8,"刘备");
		Node node5 = new Node(10,"关羽");
		Node node6 = new Node(14,"张飞");
		
		root.setLeft(node2);
		root.setRight(node3);
		node2.setLeft(node4);
		node2.setRight(node5);
		node3.setLeft(node6);
		
		ClueTree cl = new ClueTree();
		
		cl.setRoot(root);
		
		cl.clueNode();
		
		Node left = node5.getLeft();
		Node right = node5.getRight();
		System.out.println("前驱:" + left.getNo() + ",后继:" + right.getNo());
		
		cl.preSelect();
		
	}
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值