class BinaryTree {
private HeroNode2 root;
//为了实现线索化,需要创建要给指向当前节点的前驱节点的指针
private HeroNode2 pre;
public void setRoot(HeroNode2 root) {
this.root = root;
}
//编写对二叉树进行中序线索化的方法
public void threadedNodes(HeroNode2 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 perOrder() {
if (root != null) {
root.perOrder();
} else {
System.out.println("二叉树为空");
}
}
// 前序查找
public HeroNode2 perOrderSearch(int no) {
if(root!=null) {
return root.perOrderSearch(no);
}else {
return null;
}
}
// 中序遍历
public void midOrder() {
if (root != null) {
root.midOrder();
} else {
System.out.println("二叉树为空");
}
}
// 中序查找
public HeroNode2 midOrderSearch(int no) {
if(root!=null) {
return root.perOrderSearch(no);
}else {
return null;
}
}
// 后序遍历
public void postOrder() {
if (root != null) {
root.postOrder();
} else {
System.out.println("二叉树为空");
}
}
// 后序查找
public HeroNode2 postOrderSearch(int no) {
if(root!=null) {
return root.perOrderSearch(no);
}else {
return null;
}
}
// 添加元素
public void add(HeroNode2 heroNode) {
if (root == null) {
root = heroNode;
return;
}
HeroNode2 head = root;
while (true) {
if (head.getNo() > heroNode.getNo()) {
if (head.getLeft() == null) {
head.setLeft(heroNode);
return;
}
head = head.getLeft();
}
if (head.getNo() < heroNode.getNo()) {
if (head.getRight() == null) {
head.setRight(heroNode);
return;
}
head = head.getRight();
}
}
}
}
class HeroNode2 {
private int no;
private String name;
private HeroNode2 left;
private HeroNode2 right;
//说明
//1.如果leftType==0表示指向的是左子树,如果1则表示指向前驱节点
//2.如果rightType==0表示指向的是右子树,如果1则表示指向后驱节点
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 HeroNode2(int no, String name) {
super();
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 HeroNode2 getLeft() {
return left;
}
public void setLeft(HeroNode2 left) {
this.left = left;
}
public HeroNode2 getRight() {
return right;
}
public void setRight(HeroNode2 right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + "]";
}
// 编写前序遍历的方法
public void perOrder() {
System.out.println(this.toString());
if (this.left != null) {
this.left.perOrder();
}
if (this.right != null) {
this.right.perOrder();
}
}
// 前序查找
public HeroNode2 perOrderSearch(int no) {
if (this.no == no) {
return this;
}
// if(this.left!=null) {
// return this.left.perOrderSearch(no);
// }
// if(this.right!=null) {
// return this.right.perOrderSearch(no);
// }
// return null;
// 不能这样写,如果查找的元素在右边回使提前返回null,导致提前结束
HeroNode2 heroNode = null;
// 不能HeroNode2 heroNode;要初始化
if (this.left != null) {
heroNode = this.left.perOrderSearch(no);
}
if (heroNode != null) {
return heroNode;
}
if (this.right != null) {
heroNode = this.right.perOrderSearch(no);
}
return heroNode;
}
// 中序遍历
public void midOrder() {
if (this.left == null) {
this.left.midOrder();
}
System.out.println(this.toString());
if (this.right == null) {
this.right.midOrder();
}
}
// 中序查找
public HeroNode2 midOrderSearch(int no) {
if (this.no == no) {
return this;
}
HeroNode2 heroNode = null;
if (this.left != null) {
heroNode = this.left.midOrderSearch(no);
}
if (heroNode != null) {
return heroNode;
}
if (this.right != null) {
heroNode = this.right.midOrderSearch(no);
}
return heroNode;
}
// 后序遍历
public void postOrder() {
if (this.left == null) {
this.left.postOrder();
}
if (this.right == null) {
this.right.postOrder();
}
System.out.println(this.toString());
}
// 后序查找
public HeroNode2 postOrderSearch(int no) {
if (this.no == no) {
return this;
}
HeroNode2 heroNode = null;
if (this.left != null) {
heroNode = this.left.postOrderSearch(no);
}
if (heroNode != null) {
return heroNode;
}
if (this.right != null) {
heroNode = this.right.postOrderSearch(no);
}
return heroNode;
}
}
Java数据结构与算法(线索二叉树)
最新推荐文章于 2022-09-02 19:00:52 发布