源代码
- #include
- using namespace std;
- class BiTree
- {
- public:
- BiTree(){memset(Tree,0,sizeof(Tree));}
- ~BiTree(){}
- int createbitree();
- int visit(int n);
- int calnode(int i,int &n);
- int depth(int n);
- int max(int a,int b);
- int parent();
- int children();
- void leaves();
- int leavesnode();
- private:
- int Tree[100];
- };
- int BiTree::createbitree()
- {
- int n,i=1;
- cout<<"请按照从上到下的顺序依次输入二叉树各个节点,空节点用0表示,以-1表示输入结束:"<>n && n!=-1)
- {
- Tree[i]=n;
- i++;
- }
- return 0;
- }
- int BiTree::visit(int n)
- {
- for(n=1;n<100;n++)
- {
- if(Tree[n]!=-1 && Tree[n]!=0)
- cout<>x;
- for(n=1;n<100;n++)
- {
- if(Tree[n]==x)
- cout<<"双亲:"<>x;
- for(n=1;n<100;n++)
- {
- if(Tree[n]==x)
- {
- if(Tree[2*n]!=0 && Tree[2*n+1]!=0)
- cout<<"左孩子:"<b?a:b;
- }
- int BiTree::depth(int n)
- {
- int hl,hr;
- for(n=1;n<100;)
- {
- if (Tree[n]!=-1)
- {
- hl = depth(2*n);
- hr = depth(2*n+1);
- }
- }
- return 1+ max(hl, hr);
- }
- int main()
- {
- BiTree tree;
- int n=0;
- tree.createbitree();
- cout<
运行结果