#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
typedef struct TreeNode
{int data;struct TreeNode* left;struct TreeNode* right;
}TreeNode;
//创建链表
TreeNode* createNode(int data)
{TreeNode* newnode = (TreeNode*)malloc(sizeof(TreeNode));if (newnode== NULL){perror("error");exit(1);}newnode->data = data;newnode->left = NULL;newnode->right = NULL;return newnode;
}
//插入操作
TreeNode* insertNode(TreeNode* root, int data)
{if (root == NULL){return createNode(data);}if (data < root->data){root->left = insertNode(root->left, data);}else if (data > root->data){root->right = insertNode(root->right, data);}return root;
}
//遍历 中序
void inorderTraversal(TreeNode* root)
{if (root != NULL){inorderTraversal(root->left);printf("%d ", root->data);inorderTraversal(root->right);}
}
//释放空间
void freeNode(TreeNode* root)
{if (root == NULL){return;}free(root->left);free(root->right);free(root);
}
int main()
{TreeNode* root = NULL;root = insertNode(root, 50);insertNode(root, 30);insertNode(root, 20);insertNode(root, 40);insertNode(root, 70);insertNode(root, 60);insertNode(root, 80);printf("中序遍历如下\n");inorderTraversal(root);printf("\n");freeNode(root);printf("释放空间成功\n");return 0;
}