建立二叉排序树

//建立二叉排序树
#include<stdio.h>
#include<iostream>
#include<stack>
#include<string.h>
#include <queue>
using namespace std;
char before[100], in[100];
struct Node
{
    Node* lchild;
    Node* rchild;
    int c; //保存数字
}Tree[110];
int loc; //静态数组中被使用元素的个数
Node* create() //申请未使用的结点
{
    Tree[loc].lchild = Tree[loc].rchild = NULL;
    return &Tree[loc++];
}
void postOrder(Node *T) //后序遍历
{
    if(T -> lchild != NULL){
        postOrder(T -> lchild);
    }
    if(T -> rchild != NULL){
        postOrder(T -> rchild);
    }
    printf("%d ", T ->c ) ;
}
void inOrder(Node *T) //中序遍历
{
    if(T -> lchild != NULL){
        inOrder(T -> lchild);
    }
    printf("%d ", T ->c ) ;
    if(T -> rchild != NULL){
        inOrder(T -> rchild);
    }

}
void preOrder(Node *T) //前序遍历
{
    printf("%d ", T ->c ) ;
    if(T -> lchild != NULL){
        preOrder(T -> lchild);
    }
    if(T -> rchild != NULL){
        preOrder(T -> rchild);
    }

}

Node *Insert(Node *T, int x) //插入数字 X
{
    if(T == NULL){ //若当前树为空
        T = create(); //申请结点
        T -> c = x; //数字直接插入其根结点
        return T; //返回其根节点的指针
    }else if( x < T -> c){ //若x小于根节点的数值
        T -> lchild = Insert(T -> lchild, x); //插入到左子树上
    }else if(x > T -> c){
        T -> rchild = Insert(T -> rchild, x); //否则插入到右子树上 (根据题意 如果值相同的 直接忽略即可
    }
    return T; //返回根节点指针
}
int main(){
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n;

    while(scanf("%d", &n) != EOF){
        loc = 0;
        Node *T = NULL; //二叉排序树树根结点为空
        for(int i = 0; i < n; i++){
            int x;
            scanf("%d", &x);
            T = Insert(T, x);
        }
        preOrder(T);
        printf("\n");
        inOrder(T);
        printf("\n");
        postOrder(T);
        printf("\n");
    }

    return 0;
}

好的,我来为您介绍如何使用 C 语言建立二叉排序树二叉排序树也称为二叉查找树,是一种特殊的二叉树结构。它的每个节点都包含一个键值,而且对于任何节点,它的左子树中所有节点的键值都小于该节点的键值,而它的右子树中所有节点的键值都大于该节点的键值。 以下是使用 C 语言建立二叉排序树的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉排序树的节点结构体 struct node { int key; struct node *left, *right; }; // 插入节点函数 struct node* insert(struct node* node, int key) { // 如果树为空,则新建一个节点 if (node == NULL) { node = (struct node*) malloc(sizeof(struct node)); node->key = key; node->left = node->right = NULL; } // 如果插入的值小于当前节点的值,则插入到左子树中 else if (key < node->key) { node->left = insert(node->left, key); } // 如果插入的值大于等于当前节点的值,则插入到右子树中 else { node->right = insert(node->right, key); } return node; } // 中序遍历函数,按升序输出二叉排序树中的所有节点 void inorder(struct node* node) { if (node != NULL) { inorder(node->left); printf("%d ", node->key); inorder(node->right); } } int main() { struct node *root = NULL; int keys[] = {5, 3, 7, 1, 4, 6, 8, 2}; int n = sizeof(keys) / sizeof(keys[0]); // 将所有键值插入到二叉排序树中 for (int i = 0; i < n; i++) { root = insert(root, keys[i]); } // 按升序输出二叉排序树中的所有节点 inorder(root); return 0; } ``` 在上面的代码中,我们先定义了一个 `node` 结构体,它包含了一个键值 `key` 和两个指针 `left` 和 `right`,分别指向左子树和右子树。然后,我们定义了一个 `insert` 函数,用于向二叉排序树中插入新节点。在插入节点时,我们按照二叉排序树的定义,将键值小于当前节点的值的节点插入到左子树中,将键值大于等于当前节点的值的节点插入到右子树中。最后,我们定义了一个 `inorder` 函数,用于按升序输出二叉排序树中的所有节点。 在 `main` 函数中,我们首先定义了一个空的二叉排序树 `root`,然后将所有键值插入到二叉排序树中。最后,我们调用 `inorder` 函数,按升序输出二叉排序树中的所有节点。 希望这个例子对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值