数据结构大题
}
}
void PostOrder(BTREE BT)//后跟遍历
{
if(BT)
{
PostOrder(BT->lchild);
PostOrder(BT->rchild);
cout<<BT->data<<"\t";
}
}
int count2(BTREE BT)//返回度为2的节点的个数
{
if(BT->lchild==NULL&&BT->rchild==NULL)
{
position n =new node;
n->element=p2->element;
n->next=NULL;
p1->next=n;
p2=p2->next;
p1=p1->next;
}
p1=NULL;
}
void Read(LIST &L)
{
position p=L;
cout<<"请输入数据以-1结尾"<<endl;
{
cout<<BT->data<<"\t";
PreOrder(BT->lchild);
PreOrder(BT->rchild);
}
}
void InOrder(BTREE BT)//中根遍历
{
if(BT)
{
InOrder(BT->lchild);
cout<<BT->data<<"\t";
InOrder(BT->rchild);
if (value == -1){
cerr << "该链表中无此元素!" << endl;
return -1;
}
else
cout << "该链表中值为" << n << "的节点已删除,它的节点位置为" << value << endl;
cout<<"删除后线性表序列"<<endl;
Print(L1);
int delete(LIST &L, int x);如果x在该链表中,则删除对应结点,并返回其在链表中的位置(逻辑位置,第一个结点的逻辑位置为1),否则返回-1。
要求:1、定义线性表的节点的结构以及节点的型和位置的型。
2、定义线性表的基本操作
3Байду номын сангаас在1,2的基础上,完成本题。
4、在main函数中进行测试:先构建一个线性表,然后调用函数删除值等于给定值的节点。
{
position p=L->next;
while(p!=NULL)
{
cout<<p->element<<"\t";
p=p->next;
}
cout<<endl;
}
void Copy(LIST &L1,LIST L2)
{
position p1=L1;
position p2=L2->next;
while(p2!=NULL)
PostOrder(BT);
cout<<endl;
cout<<"度为2的节点的个数:"<<count2(BT)<<endl;
cout<<"叶子节点个数:"<<leafnum(BT)<<endl;
}
#include<iostream>
#include<iomanip>
using namespace std;
typedef int elementtype;
struct node{
elementtype element;
node *next;
};
typedef node *LIST;
typedef node *position;
}
树
五、已知非空二叉树T,写一个算法,求度为2的结点的个数。
要求:
1、定义二叉树的抽象数据类型和型BTREE,并定义基本操作。
2、编写函数count2(BTREE T),返回度为2的节点的个数。
3、在主函数中,构建一个二叉树,并验证所编写的算法。
六、用递归方法写一个算法,求二叉树的叶子结点数int leafnum(BTREE T)。
int loc=0;
while(p->next)
{
if(p->next->element==x)
{
position q=p->next;
p->next=q->next;
delete q;
return ++loc;
}
++loc;
p=p->next;
}
return -1;
}
int main()
{
LIST L1 = new node;
else
return false;
}
BTREE CreateBT(datatype v,BTREE ltr,BTREE rlr)//左右子树建立二叉树
{
BTREE root;
root->data=v;
root->lchild=ltr;
root->rchild-rlr;
return root;
}
BTREE Lchild(BTREE BT)//返回右子树
return count2(BT->rchild);
}
int leafnum(BTREE BT)//返回叶节点个数
{
static int count=0;
if(BT->lchild==NULL&&BT->rchild==NULL)
{
return ++count;
}
else
{
leafnum(Lchild(BT));
leafnum(Rchild(BT));
}
}
void CreateBTREE(BTREE &BT,char *str)//先根输入树
{
char ch;
ch=*str++;
if(ch=='#')
BT=NULL;
else
{
BT=new node;
BT->data=ch;
CreateBTREE(BT->lchild,str);
L1->next = NULL;
LIST L2 = new node;
L2->next = NULL;
Read(L2);
Print(L2);
Copy(L1, L2);
Print(L1);
int n;
cout << "请输入要删除的数: " << endl;
cin >> n;
int value = deleteElement(L1, n);
要求:
1、定义二叉树的抽象数据类型和型BTREE,并定义基本操作。
2、编写函数leafnum(BTREE T),返回树T的叶子节点的个数。
在主函数中,构建一个二叉树,并验证所编写的算法。
#include<iostream>
#include<iomanip>
using namespace std;
typedef char datatype;
struct node{
node *lchild;
node *rchild;
datatype data;
};
typedef node*BTREE;
void Empty(BTREE &BT)//置零
{
BT=NULL;
}
bool isEmpty(BTREE BT)//判断是否为空
{
if(BT)
return true;
position End(LIST L)//求末尾节点
{
position p=L;
while(p->next!=NULL)
{
p=p->next;
}
return p;
}
void Insert(elementtype x,position p)//插入
{
position q=new node;
q->element=x;
CreateBTREE(BT->rchild,str);
}
}
int main()
{
BTREE BT=NULL;
char *str="abc##d##ef##g##";
CreateBTREE(BT,str);
PreOrder(BT);
cout<<endl;
InOrder(BT);
cout<<endl;
return 0;
if(BT->lchild&&BT->rchild)
return 1+count2(BT->lchild)+count2(BT->rchild);
if(BT->lchild&&BT->rchild==NULL)