迭代创建
#include <stdio.h>
#include <stdlib.h>
//二叉树节点结构定义
struct Node {
int data;
struct Node *left;
struct Node *right;
};
//定义的root指针存二叉树节点的地址
struct Tree{
struct Node *root;
};
//二叉排序树的创建
void create_tree(struct Tree *tree, int val)
{
//定于一个节点,并初始化
struct Node *node = (struct Node*)malloc(sizeof(struct Node));
node->data = val;
node->left = NULL;
node->right = NULL;
//如果根为空,就将这个节点放到根节点
if (tree->root == NULL) {
tree->root = node;
}
//如果根不为空
else {
struct Node *temp = tree->root; //定义指针指向根节点
while (temp != NULL) {
//如果要放入的值小于根节点
if (val < temp->data) {
//如果根节点左孩为空,就直接放入
if (temp->left == NULL) {
temp->left = node;
return;
}
//如果 根节点左孩不为空,就指向下一个左孩节点
else {
temp = temp->left;
}
}
//如果 要放入的值大于根节点
else {
if (temp->right == NULL) { //右孩为空,直接放入
temp->right = node;
return;
}
else {
temp = temp->right; //右孩不为空,指向下一个右孩节点
}
}
}
}
}
//中序遍历(递归)
void inorder(struct Node *node)
{
if (node != NULL) {
inorder(node->left);
printf("%d ",node->data);
inorder(node->right);
}
}
int main()
{
int a[100],i;
int len;
printf("请输入数字个数:");
scanf("%d",&len);
printf("\n请输入数字:");
for(i=0;i<len;i++)
scanf("%d",&a[i]);
struct Tree *tree;
tree->root = NULL;
for (i=0; i<len; i++) {
create_tree(tree, a[i]);
}
printf("\n中序遍历(从小到大):");
inorder(tree->root);
return 0;
}
递归创建
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树结点
struct Node {
int data;
struct Node *left;
struct Node *right;
} ;
// 创建新结点
struct Node* createNode(int data) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 递归插入节点
struct Node* insert(struct Node *root, int data) {
if (root == NULL) {
root = createNode(data);
return root;
}
if (data <= root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
//中序遍历(递归)
void inorder(struct Node *bt)
{
if (bt != NULL) {
inorder(bt->left);
printf("%d ",bt->data);
inorder(bt->right);
}
}
int main() {
struct Node *root = NULL;
int n, data;
printf("请输入要插入的数字个数:");
scanf("%d", &n);
printf("\n请输入数字:");
for (int i = 0; i < n; i++) {
scanf("%d", &data);
root = insert(root, data);
}
printf("中序遍历结果为:");
inorder(root);
printf("\n");
return 0;
}
查找
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
// 定义二叉树结点
struct Node {
int data;
struct Node *left;
struct Node *right;
} ;
// 创建新结点
struct Node* createNode(int data) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 递归插入节点
struct Node* insert(struct Node *root, int data) {
if (root == NULL) {
root = createNode(data);
return root;
}
if (data <= root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
//中序遍历(递归)
void inorder(struct Node *bt)
{
if (bt != NULL) {
inorder(bt->left);
printf("%d ",bt->data);
inorder(bt->right);
}
}
//查找
bool Search(struct Node *bt, int target)
{
if(bt==NULL)return false;
else
{
if(target==bt->data)return true;
else if(target<bt->data) Search(bt->left, target);
else return Search(bt->right, target);
}
}
int main() {
struct Node *root = NULL;
int n, data;
printf("请输入要插入的数字个数:");
scanf("%d", &n);
printf("\n请输入数字:");
for (int i = 0; i < n; i++) {
scanf("%d", &data);
root = insert(root, data);
}
printf("中序遍历结果为:");
inorder(root);
printf("\n请输入要查找的数:");
scanf("%d",&data);
if(Search(root,data)) printf("查找成功");
else printf("查找失败");
return 0;
}
插入一个数字
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树结点
struct Node {
int data;
struct Node *left;
struct Node *right;
} ;
// 创建新结点
struct Node* createNode(int data) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 递归插入节点
struct Node* insert(struct Node *root, int data) {
if (root == NULL) {
root = createNode(data);
return root;
}
if (data <= root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
//中序遍历(递归)
void inorder(struct Node *bt)
{
if (bt != NULL) {
inorder(bt->left);
printf("%d ",bt->data);
inorder(bt->right);
}
}
int main() {
struct Node *root = NULL;
int n, data;
printf("请输入数字个数:");
scanf("%d", &n);
printf("\n请输入数字:");
for (int i = 0; i < n; i++) {
scanf("%d", &data);
root = insert(root, data);
}
printf("中序遍历结果为:");
inorder(root);
printf("\n");
printf("请输入要插入的数字:");
scanf("%d",&data);
root=insert(root, data);
printf("插入后中序遍历结果为:");
inorder(root);
return 0;
}