关于二叉树排列数的一道问题
发布网友
发布时间:2024-03-27 13:09
我来回答
共1个回答
热心网友
时间:2024-03-30 23:23
#include<iostream>
using namespace std;
struct BiNode
{
int data;
BiNode *lchild, *rchild;
};
class BiSortTree
{
public:
BiSortTree(int a[], int n);
BiNode* InsertBST(int elem, BiNode *s);
void InOrder(){ InOrder(root); }
private:
BiNode *root;
void InOrder(BiNode *bt);
};
BiNode* BiSortTree:: InsertBST(int elem, BiNode *root)
{
if(root==NULL)
{
root=new BiNode;
root->data=elem;
root->lchild=root->rchild=NULL;
}
else if (elem < root->data)
root->lchild=InsertBST(elem, root->lchild);
else
root->rchild=InsertBST(elem,root->rchild);
return root;
}
BiSortTree::BiSortTree(int a[], int n):root(NULL)
{
for (int i = 0; i < n; i++)
root=InsertBST(a[i], root);
}
void BiSortTree::InOrder(BiNode *bt)
{
if (bt == NULL) return;
else
{
InOrder(bt->lchild);
cout << bt->data<<endl;
InOrder(bt->rchild);
}
}
int main()
{
int w[] = { 9, 2, 10, 4, 11 };
BiSortTree T(w, 5);
T.InOrder();
}