用c语言建立一个4层完全二叉树,使其具有建立排序二叉树,插入结点和删除节点的功能
时间: 2023-06-18 13:05:49 浏览: 140
首先,我们需要定义一个二叉树结点的数据结构,包括左右子结点指针和数据域。
```c
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
```
然后,我们需要定义一个二叉树的数据结构,包括根节点指针和节点数量。
```c
typedef struct BinaryTree {
Node *root;
int size;
} BinaryTree;
```
接下来,我们可以实现二叉树的初始化函数。
```c
void init(BinaryTree *tree) {
tree->root = NULL;
tree->size = 0;
}
```
接着,我们可以实现二叉树的插入函数。在插入节点时,我们需要比较当前节点的值和插入节点的值,并根据大小关系递归地插入到左子树或右子树。
```c
void insert(BinaryTree *tree, int value) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->left = NULL;
newNode->right= NULL;
if (tree->root == NULL) {
tree->root = newNode;
tree->size++;
return;
}
Node *current = tree->root;
while (1) {
if (value < current->data) {
if (current->left == NULL) {
current->left = newNode;
tree->size++;
break;
}
current = current->left;
} else {
if (current->right == NULL) {
current->right = newNode;
tree->size++;
break;
}
current = current->right;
}
}
}
```
最后,我们可以实现二叉树的删除函数。在删除节点时,我们需要递归地找到要删除的节点,并根据其子节点的情况进行删除操作。
```c
Node* delete(Node *root, int value) {
if (root == NULL) {
return NULL;
}
if (value < root->data) {
root->left = delete(root->left, value);
} else if (value > root->data) {
root->right = delete(root->right, value);
} else {
if (root->left == NULL) {
Node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
Node *temp = root->left;
free(root);
return temp;
}
Node *temp = root->right;
while (temp->left != NULL) {
temp = temp->left;
}
root->data = temp->data;
root->right = delete(root->right, temp->data);
}
return root;
}
void remove(BinaryTree *tree, int value) {
tree->root = delete(tree->root, value);
tree->size--;
}
```
完整代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
typedef struct BinaryTree {
Node *root;
int size;
} BinaryTree;
void init(BinaryTree *tree) {
tree->root = NULL;
tree->size = 0;
}
void insert(BinaryTree *tree, int value) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->left = NULL;
newNode->right= NULL;
if (tree->root == NULL) {
tree->root = newNode;
tree->size++;
return;
}
Node *current = tree->root;
while (1) {
if (value < current->data) {
if (current->left == NULL) {
current->left = newNode;
tree->size++;
break;
}
current = current->left;
} else {
if (current->right == NULL) {
current->right = newNode;
tree->size++;
break;
}
current = current->right;
}
}
}
Node* delete(Node *root, int value) {
if (root == NULL) {
return NULL;
}
if (value < root->data) {
root->left = delete(root->left, value);
} else if (value > root->data) {
root->right = delete(root->right, value);
} else {
if (root->left == NULL) {
Node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
Node *temp = root->left;
free(root);
return temp;
}
Node *temp = root->right;
while (temp->left != NULL) {
temp = temp->left;
}
root->data = temp->data;
root->right = delete(root->right, temp->data);
}
return root;
}
void remove(BinaryTree *tree, int value) {
tree->root = delete(tree->root, value);
tree->size--;
}
void printInorder(Node *root) {
if (root == NULL) {
return;
}
printInorder(root->left);
printf("%d ", root->data);
printInorder(root->right);
}
int main() {
BinaryTree tree;
init(&tree);
insert(&tree, 5);
insert(&tree, 2);
insert(&tree, 8);
insert(&tree, 1);
insert(&tree, 3);
insert(&tree, 6);
insert(&tree, 9);
insert(&tree, 4);
insert(&tree, 7);
printf("Inorder traversal: ");
printInorder(tree.root);
printf("\n");
remove(&tree, 3);
printf("Inorder traversal: ");
printInorder(tree.root);
printf("\n");
return 0;
}
```
阅读全文