64,654
社区成员
发帖
与我相关
我的任务
分享
// csdn2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef char elemtype;
struct node
{
elemtype data;
struct node *left;
struct node *right;
};
void createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q')
{
*r=NULL;
return;
}
else
{
if(!(*r=new node))
cout<<"内存分配失败"<<endl;
(*r)->data=ch;
}
createbitr(&(*r)->left);
createbitr(&(*r)->right);
//return *r;
//delete *r;
}
void preorder(node *r)
{
if(r)
{
cout<<r->data<<'\t';
preorder(r->left);
preorder(r->right);
}
//return *r;
}
int _tmain(int argc, _TCHAR* argv[])
{
node *root=NULL;
createbitr(&root);
preorder(root);
return 0;
}
#include<stdio.h>
#include<malloc.h>
#define MaxSize 100
typedef struct node{
char data;
struct node *lchild;
struct node *rchild;
}BTree;
void creatree(BTree *&b,char *str)//由广义表样式创建二叉树
{
BTree *stack[MaxSize],*p;
int top=-1,j=0;//栈空
int k;//左右指标志
char ch;
b=NULL;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case '(': top++;stack[top]=p;k=1;break;
case ')': top--;break;
case ',': k=2;break;
default: p=(BTree*)malloc(sizeof(BTree));
p->data=ch;p->lchild=p->rchild=NULL;
if(b==NULL)
b=p;//根结点
else
{
if(k==1) stack[top]->lchild=p;
if(k==2) stack[top]->rchild=p;
}
}
j++;
ch=str[j];
}
}
void printree(BTree *b)//由广义表形式输出二叉树
{
if(b!=NULL)
{
printf("%c",b->data);
if(b->lchild!=NULL||b->rchild!=NULL)
{
printf("(");
printree(b->lchild);
if(b->rchild!=NULL)
printf(",");
printree(b->rchild);
printf(")");
}
}
}
void Preorder(BTree *b){
if(b!=NULL){
printf("%c",b->data);
Preorder(b->lchild);
Preorder(b->rchild);
}
}
void zhongxu(BTree *b){
if(b){
zhongxu(b->lchild);
printf("%c",b->data);
zhongxu(b->rchild);
}
}
void houxu(BTree *b)
{
if(b!=NULL){
houxu(b->lchild);
houxu(b->rchild);
printf("%c",b->data);
}
}
int Depth(BTree *b){
int depl,depr;
if(!b)
return 0;
else{
depl=Depth(b->lchild);
depr=Depth(b->rchild);
return (depl>depr)?(depl+1):(depr+1);
}
}
int main()
{
BTree *b;
creatree(b,"(A(B(,C),D(E,F)))");
printf("广义表表示法为:\n");
printree(b);
system("pause");
}
node * createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q')
//*r=NULL;
return NULL;
else
{
if(!(*r=new node))
cout<<"内存分配失败"<<endl;
(*r)->data=ch;
createbitr(&(*r)->left);
createbitr(&(*r)->right);
return *r;
}
//delete *r;
}
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef char elemtype;
struct node
{
elemtype data;
struct node *left;
struct node *right;
};
node * createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q')
*r=NULL;
else
{
if(!(*r=new node))
cout<<"内存分配失败"<<endl;
(*r)->data=ch;
createbitr(&(*r)->left);
createbitr(&(*r)->right);
}
return *r;
// delete *r;
}
node * preorder(node **r)
{
if(*r)
{
cout<<(*r)->data<<'\t';
preorder(&(*r)->left);
preorder(&(*r)->right);
}
return *r;
}
int main(int argc, char* argv[])
{
node *root;
createbitr(&root);
preorder(&root);
return 0;
}
node * createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q') *r=NULL;
else
{
if(!(*r=new node)) cout<<"内存分配失败"<<endl;
else
{
(*r)->data=ch;
createbitr(&(*r)->left);
createbitr(&(*r)->right);
}
}
return *r;
//delete *r;
}