package tree.threadBinaryTree;
public class ThreadBinaryTree {
Node root;
Node pre;
public ThreadBinaryTree(Node root) {
this.root = root;
}
//线索化遍历
public void tereadedList() {
Node current = root;
while (current != null) {
while (current.leftType == 0) {
current = current.left;
}
System.out.println(current);
while (current.rightType == 1) {
current = current.right;
System.out.println(current);
}
current = current.right;
}
}
//线索化
public void threadedNode() {
this.threadedNode(root);
}
public void threadedNode(Node node) {
if (node == null)
return;
if (node.left != null)
threadedNode(node.left);
if (node.left == null) {
node.left = pre;
node.leftType = 1;
}
if (pre != null && pre.right == null) {
pre.right = node;
pre.rightType = 1;
}
pre = node;
if (node.right != null)
threadedNode(node.right);
}
}
class Node {
int data;
Node left;
Node right;
int leftType;
int rightType;
public Node(int data) {
this.data = data;
}
@Override
public String toString() {
return "Node [data=" + data + "]";
}
}
线索化二叉树 及线索化遍历
最新推荐文章于 2021-11-04 15:46:24 发布