用C语言写1.先序遍历创建二叉树; 2.中序线索化二叉树; 3.分别通过 找前驱 和 找后继 的方式,遍历中序线索二叉树。
时间: 2024-02-11 13:09:43 浏览: 67
好的,我来给您提供一份用C语言实现的代码:
1. 先序遍历创建二叉树:
```c
#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
TreeNode* buildTree(int* preorder, int n, int* i) {
if (*i >= n || preorder[*i] == -1) {
(*i)++;
return NULL;
}
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = preorder[(*i)++];
root->left = buildTree(preorder, n, i);
root->right = buildTree(preorder, n, i);
return root;
}
void preorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
printf("%d ", root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
int main() {
int preorder[] = { 1, 2, -1, -1, 3, 4, -1, -1, 5, -1, -1 };
int n = sizeof(preorder) / sizeof(int);
int i = 0;
TreeNode* root = buildTree(preorder, n, &i);
preorderTraversal(root);
return 0;
}
```
2. 中序线索化二叉树:
```c
#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
int left_threaded;
int right_threaded;
} TreeNode;
void inorderThread(TreeNode* root, TreeNode** pre) {
if (root == NULL) {
return;
}
inorderThread(root->left, pre);
if (root->left == NULL) {
root->left_threaded = 1;
root->left = *pre;
}
if (*pre && (*pre)->right == NULL) {
(*pre)->right_threaded = 1;
(*pre)->right = root;
}
*pre = root;
inorderThread(root->right, pre);
}
TreeNode* inorderThreaded(TreeNode* root) {
TreeNode* dummy = (TreeNode*)malloc(sizeof(TreeNode));
dummy->right = root;
TreeNode* pre = dummy;
inorderThread(root, &pre);
pre->right = dummy;
pre->right_threaded = 1;
dummy->left = pre;
return dummy;
}
void inorderTraversal(TreeNode* root) {
TreeNode* p = root;
while (p) {
while (p->left_threaded == 0) {
p = p->left;
}
printf("%d ", p->val);
while (p->right_threaded == 1) {
p = p->right;
printf("%d ", p->val);
}
p = p->right;
}
}
int main() {
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = 1;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->val = 2;
root->left->left = NULL;
root->left->right = NULL;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->val = 3;
root->right->left = (TreeNode*)malloc(sizeof(TreeNode));
root->right->left->val = 4;
root->right->left->left = NULL;
root->right->left->right = NULL;
root->right->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->right->val = 5;
root->right->right->left = NULL;
root->right->right->right = NULL;
inorderTraversal(root); // 2 1 4 3 5
printf("\n");
TreeNode* threadedRoot = inorderThreaded(root);
inorderTraversal(threadedRoot); // 2 1 4 3 5
return 0;
}
```
3. 遍历中序线索二叉树:
```c
#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
int left_threaded;
int right_threaded;
} TreeNode;
void inorderThread(TreeNode* root, TreeNode** pre) {
if (root == NULL) {
return;
}
inorderThread(root->left, pre);
if (root->left == NULL) {
root->left_threaded = 1;
root->left = *pre;
}
if (*pre && (*pre)->right == NULL) {
(*pre)->right_threaded = 1;
(*pre)->right = root;
}
*pre = root;
inorderThread(root->right, pre);
}
TreeNode* inorderThreaded(TreeNode* root) {
TreeNode* dummy = (TreeNode*)malloc(sizeof(TreeNode));
dummy->right = root;
TreeNode* pre = dummy;
inorderThread(root, &pre);
pre->right = dummy;
pre->right_threaded = 1;
dummy->left = pre;
return dummy;
}
void inorderTraversal(TreeNode* root) {
TreeNode* p = root;
while (p) {
while (p->left_threaded == 0) {
p = p->left;
}
printf("%d ", p->val);
while (p->right_threaded == 1) {
p = p->right;
printf("%d ", p->val);
}
p = p->right;
}
}
TreeNode* findPredecessor(TreeNode* p) {
if (p->left_threaded == 1) {
return p->left;
}
p = p->left;
while (p->right_threaded == 0) {
p = p->right;
}
return p;
}
TreeNode* findSuccessor(TreeNode* p) {
if (p->right_threaded == 1) {
return p->right;
}
p = p->right;
while (p->left_threaded == 0) {
p = p->left;
}
return p;
}
int main() {
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = 1;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->val = 2;
root->left->left = NULL;
root->left->right = NULL;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->val = 3;
root->right->left = (TreeNode*)malloc(sizeof(TreeNode));
root->right->left->val = 4;
root->right->left->left = NULL;
root->right->left->right = NULL;
root->right->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->right->val = 5;
root->right->right->left = NULL;
root->right->right->right = NULL;
TreeNode* threadedRoot = inorderThreaded(root);
TreeNode* p = threadedRoot->right->left;
while (p != threadedRoot) {
printf("%d ", p->val);
p = findSuccessor(p);
}
printf("\n");
p = threadedRoot->right->right;
while (p != threadedRoot) {
printf("%d ", p->val);
p = findPredecessor(p);
}
return 0;
}
```
希望这份代码能够帮到您!
阅读全文