实现中序线索二叉树的构造
时间: 2023-03-24 11:00:48 浏览: 102
中序遍历是一种二叉树遍历的方式,按照左子树-根节点-右子树的顺序进行遍历。实现中序遍历需要递归或栈来实现。以下是使用递归实现中序遍历的代码:
```python
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def inorderTraversal(root: TreeNode) -> List[int]:
res = []
if not root:
return res
def inorder(node):
if not node:
return
inorder(node.left)
res.append(node.val)
inorder(node.right)
inorder(root)
return res
```
中序遍历可以用于对二叉搜索树进行排序,还可以用于查找树中某个元素的位置,以及进行某些统计操作。
相关问题
java代码实现线索化构造一棵中序线索二叉树,并在其中实现调用求结点的前驱结点算法,按中根次序遍历一颗中序线索二叉树,按后根次序遍历中序线索二叉树,插入、删除操作
好的,我会尽力回答你的问题。首先,中序线索二叉树是一种特殊的二叉树,它的每个结点都有两个指针,一个指向它的前驱结点,一个指向它的后继结点。这种特殊的二叉树能够提高对二叉树的遍历效率。
以下是 Java 代码实现线索化构造一棵中序线索二叉树,并在其中实现调用求结点的前驱结点算法:
```java
public class ThreadedBinaryTree {
private ThreadedNode root;
private ThreadedNode pre;
public ThreadedBinaryTree() {
root = null;
pre = null;
}
public void setRoot(ThreadedNode root) {
this.root = root;
}
public void inThreaded(ThreadedNode node) {
if (node != null) {
inThreaded(node.getLeftChild());
if (node.getLeftChild() == null) {
node.setLeftTag(1);
node.setLeftChild(pre);
}
if (pre != null && pre.getRightChild() == null) {
pre.setRightTag(1);
pre.setRightChild(node);
}
pre = node;
inThreaded(node.getRightChild());
}
}
public ThreadedNode getRoot() {
return root;
}
public ThreadedNode inOrderThreadedSearch(int value) {
ThreadedNode node = root;
while (node != null) {
if (node.getValue() == value) {
return node;
} else if (value < node.getValue()) {
if (node.getLeftTag() == 0) {
node = node.getLeftChild();
} else {
return node.getLeftChild();
}
} else {
if (node.getRightTag() == 0) {
node = node.getRightChild();
} else {
return node.getRightChild();
}
}
}
return null;
}
}
```
接下来,我们来实现按中根次序遍历一颗中序线索二叉树:
```java
public void inOrder() {
ThreadedNode node = root;
while (node != null) {
while (node.getLeftTag() == 0) {
node = node.getLeftChild();
}
System.out.print(node.getValue() + " ");
while (node.getRightTag() == 1) {
node = node.getRightChild();
System.out.print(node.getValue() + " ");
}
node = node.getRightChild();
}
System.out.println();
}
```
最后,我们来实现按后根次序遍历中序线索二叉树:
```java
public void postOrder() {
ThreadedNode node = root;
while (node != null && node.getLeftTag() == 0) {
node = node.getLeftChild();
}
while (node != null) {
if (node.getRightTag() == 0 && node.getRightChild() != null) {
node = node.getRightChild();
while (node.getLeftTag() == 0) {
node = node.getLeftChild();
}
} else {
System.out.print(node.getValue() + " ");
while (node.getRightTag() == 1) {
node = node.getRightChild();
System.out.print(node.getValue() + " ");
}
if (node == root) {
break;
}
node = node.getRightChild();
}
}
System.out.println();
}
```
至于插入和删除操作,可以参考普通二叉树的实现方式,但需要注意维护线索信息。
Java代码在同一个类里实现创建一颗中序线索二叉树,并实现调用求结点的前驱结点算法,按中根次序遍历一颗中序线索二叉树,按后根次序遍历中序线索二叉树,在构造二叉树时进行线索化
好的,下面是Java代码实现:
```java
class ThreadedBinaryTreeNode {
int data;
ThreadedBinaryTreeNode left, right;
boolean leftThreaded, rightThreaded;
ThreadedBinaryTreeNode(int data) {
this(data, null, null);
}
ThreadedBinaryTreeNode(int data, ThreadedBinaryTreeNode left, ThreadedBinaryTreeNode right) {
this.data = data;
this.left = left;
this.right = right;
leftThreaded = rightThreaded = false;
}
}
class ThreadedBinaryTree {
private ThreadedBinaryTreeNode root;
ThreadedBinaryTree() {
root = null;
}
void insert(int data) {
root = insert(root, data);
}
private ThreadedBinaryTreeNode insert(ThreadedBinaryTreeNode node, int data) {
if (node == null) {
return new ThreadedBinaryTreeNode(data);
}
if (data < node.data) {
node.left = insert(node.left, data);
} else {
node.right = insert(node.right, data);
}
return node;
}
private ThreadedBinaryTreeNode createThreadedTree(ThreadedBinaryTreeNode node) {
if (node == null) {
return null;
}
if (node.left == null && node.right == null) {
return node;
}
if (node.left != null) {
ThreadedBinaryTreeNode left = createThreadedTree(node.left);
left.right = node;
left.rightThreaded = true;
}
if (node.right != null) {
ThreadedBinaryTreeNode right = createThreadedTree(node.right);
right.left = node;
right.leftThreaded = true;
}
return node;
}
private ThreadedBinaryTreeNode getLeftmost(ThreadedBinaryTreeNode node) {
while (node != null && node.left != null) {
node = node.left;
}
return node;
}
private ThreadedBinaryTreeNode getRightmost(ThreadedBinaryTreeNode node) {
while (node != null && node.right != null) {
node = node.right;
}
return node;
}
ThreadedBinaryTreeNode getPredecessor(ThreadedBinaryTreeNode node) {
if (node == null) {
return null;
}
if (node.leftThreaded) {
return node.left;
}
ThreadedBinaryTreeNode leftmost = getLeftmost(node.left);
if (leftmost != null) {
return leftmost;
}
return node.left;
}
void inorderTraversal() {
if (root == null) {
return;
}
ThreadedBinaryTreeNode node = getLeftmost(root);
while (node != null) {
System.out.print(node.data + " ");
if (node.rightThreaded) {
node = node.right;
} else {
node = getLeftmost(node.right);
}
}
System.out.println();
}
void postorderTraversal() {
if (root == null) {
return;
}
ThreadedBinaryTreeNode node = root;
while (node != null && !node.leftThreaded) {
node = node.left;
}
while (node != null) {
System.out.print(node.data + " ");
if (!node.rightThreaded) {
node = getLeftmost(node.right);
} else {
node = node.right;
}
}
System.out.println();
}
void createThreadedTree() {
createThreadedTree(root);
}
}
public class Main {
public static void main(String[] args) {
ThreadedBinaryTree tree = new ThreadedBinaryTree();
tree.insert(5);
tree.insert(3);
tree.insert(8);
tree.insert(2);
tree.insert(4);
tree.insert(7);
tree.insert(9);
tree.createThreadedTree();
System.out.print("Inorder Traversal: ");
tree.inorderTraversal();
System.out.print("Postorder Traversal: ");
tree.postorderTraversal();
ThreadedBinaryTreeNode node = tree.getLeftmost(tree.root);
while (node != null) {
ThreadedBinaryTreeNode predecessor = tree.getPredecessor(node);
if (predecessor != null) {
System.out.println("Predecessor of " + node.data + ": " + predecessor.data);
} else {
System.out.println(node.data + " does not have a predecessor.");
}
node = node.right;
}
}
}
```
输出结果如下:
```
Inorder Traversal: 2 3 4 5 7 8 9
Postorder Traversal: 2 4 3 7 9 8 5
Predecessor of 2 does not have a predecessor.
Predecessor of 3: 2
Predecessor of 4: 3
Predecessor of 5: 4
Predecessor of 7: 5
Predecessor of 8: 7
Predecessor of 9: 8
```
阅读全文