目录
线索二叉树的目的是去利用没有利用到的空指针
n个结点有n+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());
- }
- //中序遍历二叉树
- 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();
-
- }
- }