二叉树的自动创建:
#include <stdio.h>//库文件,可忽略
#include<string.h>
#include<iostream>
#include <fstream>
#include<time.h>
#include<math.h>
#define MCX 20//没有什么含义
using namespace std;
template<class T>//模板类,方便各种类型的二叉树
class M_x
{
public:
T q;// 存储的值
M_x* a;// 指向左边子节点的指针
M_x* d;// 指向右边子节点的指针
int m;// 存储这个节点的孩子的个数
};
template<class T/*,class U*/>//T指类名
int max(T *p/*,U x*/)//递归函数
{
cout << "该节点的值是多少" << endl;
T* j = new(T);
cin >> j->q;
if (p->q > j->q)
{
p->a = j;
}
else if (p->q < j->q)
{
p->d = j;
}
//int xx = U;
cout << "您要创建几个儿子" << endl;
cin >> j->m;
int i = j->m;
if (i <= 2&&i!=0)
{
while (i)
{
max(j);
--i;
}
}
else
{
return 0;
}
}
int main()
{
M_x<int>head;
head.q = 10;
max<M_x<int>>(&head);
}