#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef int TElement;
typedef struct TNode {
TElement data;
struct TNode* left;
struct TNode* right;
} TNode;
#define KEY(n) ((n)->data)
#define VisitNode(n) (printf("%d ", (n)->data))
TNode* create_tree(TElement data, TNode* left, TNode* right)
{
TNode* n = (TNode*)malloc(sizeof(TNode));
n->data = data;
n->left = left;
n->right = right;
return n;
}
void insert_bst(TNode* root, TNode* n)
{
if (KEY(n) < KEY(root)) {
if (root->left == NULL)
root->left = n;
else
insert_bst(root->left, n);
}
else if (KEY(n) > KEY(root)) {
if (root->right == NULL)
root->right = n;
else
insert_bst(root->right, n);
}
}
void inorder(TNode* n)
{
if (n != NULL) {
inorder(n->left);
VisitNode(n);
inorder(n->right);
}
}
int main()
{
int keys[] = { 10, 55, 44, 88, 33, 22, 99 };
TNode* root = create_tree(keys[0], NULL, NULL);
for (int i = 1; i < 7; i++) {
TNode* n = create_tree(keys[i], NULL, NULL);
insert_bst(root, n);
}
inorder(root);
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: