二叉排序树(C++)

问题:

   给定一个结点序列,确定一棵二叉排序树,并给出其前序,中序,后序遍历。

输入:

 5
1 6 5 9 8

思路:

  首先,需要了解二叉排序树的特点。比根节点小的结点在其左子树,比根节点大的结点在其右子树。其中序遍历(左根右)刚好是一个升序序列。

  其次:明白建树过程就是一个插入的过程,有三种情况:1.插入第一个节点,作根节点。2.比根节点的值小,插入其左子树。3.比根节点的值大,插入其右子树。

  最后,遍历。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

int n,x;


struct Node{
    int value;
    Node* lchild;
    Node* rchild;
};


Node* insertNode(Node* root,int x)
{
    if(root==NULL)//为空建树
    {
        root=new Node({x,NULL,NULL});
        return root;
    }
    if(x<root->value)//小于插左子树
        root->lchild=insertNode(root->lchild,x);
    else if(x>root->value)//大于插右子树
        root->rchild=insertNode(root->rchild,x);
    return root;

}

void preTraverse(Node* root)
{
    if(root==NULL)
        return;
    cout<<root->value<<" ";
    if(root->lchild!=NULL)
        preTraverse(root->lchild);
    if(root->rchild!=NULL)
        preTraverse(root->rchild);
}

void inTraverse(Node* root)
{
    if(root==NULL)
        return;
    if(root->lchild!=NULL)
        inTraverse(root->lchild);
    cout<<root->value<<" ";
    if(root->rchild!=NULL)
        inTraverse(root->rchild);
}
void postTraverse(Node* root)
{
    if(root==NULL)
        return;
    if(root->lchild!=NULL)
        postTraverse(root->lchild);
    if(root->rchild!=NULL)
        postTraverse(root->rchild);
    cout<<root->value<<" ";
}




int main()
{
    cin>>n;
    Node* root=NULL;
    while(n--)
    {
        cin>>x;
        root=insertNode(root,x);
    }
    cout<<"Pre: ";
    preTraverse(root);
    cout<<endl;
    cout<<"In: ";
    inTraverse(root);
    cout<<endl;
    cout<<"Post: ";
    postTraverse(root);
    cout<<endl;

    return 0;
}

结果:

 

下面是一个简单的二叉排序树C++ 实现: ``` #include <iostream> using namespace std; struct TreeNode { int data; TreeNode* left; TreeNode* right; }; void insert(TreeNode*& node, int data) { if (!node) { node = new TreeNode{ data, nullptr, nullptr }; } else if (data < node->data) { insert(node->left, data); } else if (data > node->data) { insert(node->right, data); } } void inorder(TreeNode* node) { if (node) { inorder(node->left); cout << node->data << " "; inorder(node->right); } } void preorder(TreeNode* node) { if (node) { cout << node->data << " "; preorder(node->left); preorder(node->right); } } void postorder(TreeNode* node) { if (node) { postorder(node->left); postorder(node->right); cout << node->data << " "; } } int main() { TreeNode* root = nullptr; int arr[] = { 5, 3, 7, 1, 9, 4, 6 }; int n = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < n; i++) { insert(root, arr[i]); } cout << "Inorder traversal: "; inorder(root); cout << endl; cout << "Preorder traversal: "; preorder(root); cout << endl; cout << "Postorder traversal: "; postorder(root); cout << endl; return 0; } ``` 上面的代码实现了二叉排序树的插入和三种遍历方式(中序遍历、先序遍历和后序遍历)。在 `insert` 函数中,如果当前节点为空,就新建一个节点并把数据插入该节点;否则,根据数据大小递归向左或向右子树插入数据。 在 `main` 函数中,创建了一个二叉排序树并插入了一些数据。然后分别对该树进行了三种遍历,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值