此次作业中公共数据类型定义在如下同一个头文件中:
//bTree.h : 包含二叉树常用的存储结构和二叉树的常用算法
#include <stdio.h>
#define MaxNode 11 //s树or二叉树的最大节点个数
typedef char DataType; //定义节点数据类型
typedef struct seqnode{ //定义顺序储存结构的树的节点
DataType data; //节点数据元素
int parent; //指向父亲的数组伪指针
}seqNode;
typedef seqNode SqTree[MaxNode]; //定义顺序储存结构的树
typedef DataType SqBTree[MaxNode]; //定义顺序存储结构的二叉树
typedef struct chnnode{ //定义链式存储结构的二叉树节点
DataType data; //节点数据元素
struct chnnode * lchild; //左孩子指针
struct chnnode * rchild; //右孩子指针
}chnNode;
typedef struct chnhead{ //定义链式存储节点二叉树的头结点
int nodeCount; //记录节点个数
struct chnnode * root; //指向根节点
}chnHead;
typedef chnHead CnBTree; //定义链式存储结构的二叉树
7.4:
// problem_7.4.cpp : 定义控制台应用程序的入口点。
//
//probem:将n个节点以顺序存放的完全二叉树转化为二叉链存储结构
#include "stdafx.h"
#include "bTree.h"
chnNode * CnBTreeCreate(DataType * sbt,int n){ //递归方法创建完全二叉树,对于错误的完全二叉树,则只会在错误之前的节点正常
chnNode *temp = NULL;
if (n == 0)
temp=CnBTreeCreate(sbt,n+1);
else{
if (*(sbt + n) == '#')
temp = NULL;
else{
temp = (chnNode *)malloc(sizeof(chnNode));
temp->data = *(sbt + n);
if (2 * n < MaxNode)
temp->lchild = CnBTreeCreate(sbt, 2 * n);
else
temp->lchild = NULL;
if (2 * n + 1 < MaxNode)
temp->rchild = CnBTreeCreate(sbt, 2 * n + 1);
else
temp->rchild = NULL;
}
}
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
SqBTree Sbt = "#ABC#EFGHI";
CnBTree Cnt;
Cnt.nodeCount = strlen(Sbt) - 1;
Cnt.root=CnBTreeCreate(Sbt,0);
system("PAUSE");
return 0;
}
// problem_7.5.cpp : 定义控制台应用程序的入口点。
void CnBTree2SeqBTree(chnNode * chn,DataType *sbt,int n){
if (n == 0){
*sbt = '#';
CnBTree2SeqBTree(chn, sbt, n + 1);
}
else{
if (chn == NULL){
*(sbt + n) = '#';
return;
}
else{
*(sbt + n) = chn->data;
if (2 * n < MaxNode - 1)
CnBTree2SeqBTree(chn->lchild, sbt, 2 * n);
if (2 * n + 1 < MaxNode - 1)
CnBTree2SeqBTree(chn->rchild, sbt, 2 * n + 1);
}
}
}
7.6
// problem_7.6.cpp : 定义控制台应用程序的入口点。
//
bool IsCompleteBTree(CnBTree * cnt){
SqBTree St = "##########";
CnBTree2SeqBTree(cnt->root,St,0);
for (int i = 1; i < MaxNode; i++)
if (*(St + i) == '#')
return false;
return true;
}
7.7.
bTree.h新增加两种数据结构
typedef struct{
chnNode * cnNode[MaxNode];
int top;
}BTreeStack;
typedef struct{
struct{
chnNode * cnNode;
int k;
}WayNode[MaxNode];
int top;
}WayStack;
void CreateChnBTree(chnNode * &cn , char * str){
BTreeStack bts;
bts.top = -1;
int i = 0,k=0;
char ch;
chnNode * temp = NULL;
while ((ch=*(str + i)) != '\0'){
switch (ch){
case '(':
bts.cnNode[++bts.top] = temp;
k = 1;
break;
case ',':
k = 2;
break;
case ')':
k = 1;
bts.top--;
break;
default:
temp = (chnNode *)malloc(sizeof(chnNode));
temp->data = ch;
temp->lchild = NULL;
temp->rchild = NULL;
switch (k){
case 0:
cn = temp;
break;
case 1:
bts.cnNode[bts.top]->lchild = temp;
break;
case 2:
bts.cnNode[bts.top]->rchild = temp;
break;
}
break;
}
i++;
}
}
bool FindWaybyStack(WayStack *ws, chnNode * cn, char e){
ws->WayNode[++ws->top].cnNode = cn;
ws->WayNode[ws->top].k = 0;
chnNode * temp = NULL;
while (ws->top != -1){
if (ws->WayNode[ws->top].cnNode->data == e)
//说明找到一条路径
return true;
else{
if (ws->WayNode[ws->top].k == 0){
ws->WayNode[ws->top].k++;
temp = ws->WayNode[ws->top].cnNode->lchild;
if (temp == NULL)
continue;
ws->WayNode[++ws->top].cnNode = temp;
ws->WayNode[ws->top].k = 0;
}
else if (ws->WayNode[ws->top].k == 1){
ws->WayNode[ws->top].k++;
temp = ws->WayNode[ws->top].cnNode->rchild;
if (temp == NULL)
continue;
ws->WayNode[++ws->top].cnNode = temp;
ws->WayNode[ws->top].k = 0;
}
else
ws->top--;
}
}
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
char * btStr = "A(B(D(G),E(,H)),C(,F(,I(J))))";
CnBTree cbt;
cbt.root = NULL;
CreateChnBTree(cbt.root,btStr);
WayStack wSt;
wSt.top = -1;
bool mark=FindWaybyStack(&wSt,cbt.root,'I');
if (mark == true){
printf("路径如下:\n");
for (int i = 0; i <= wSt.top; i++)
printf("->%c", wSt.WayNode[i].cnNode->data);
}
else
printf("该元素不存在!\n");
system("PAUSE");
return 0;
}
7.8:
chnNode * SwapNode(chnNode * cn){
chnNode * temp;
if (cn == NULL)
return NULL;
else{
temp = (chnNode *)malloc(sizeof(chnNode));
temp->data = cn->data;
temp->lchild = SwapNode(cn->rchild);
temp->rchild = SwapNode(cn->lchild);
return temp;
}
}
7.9:
利用7.8的交换函数,直接将两个左子树进行比较即可
bool IsSmmetry(chnNode *cn1,chnNode *cn2){
if ((cn1 == NULL) ^ (cn2 == NULL))
return false;
else{
if (cn1 != NULL&&cn2 != NULL)
return IsSmmetry(cn1->lchild, cn2->lchild) & IsSmmetry(cn1->rchild, cn2->rchild);
else
return true;
}
}
7.10:
bool IsUnionAncestor(char e,char e1,char e2,chnNode *cn){
WayStack ws1, ws2;
bool mark1 = false, mark2 = false;
ws1.top = ws2.top = -1;
if (FindWaybyStack(&ws1, cn, e1) && FindWaybyStack(&ws2, cn, e2)){
for (int i = 0; i <= ws1.top; i++)
if (ws1.WayNode[i].cnNode->data == e){
mark1 = true;
break;
}
for (int i = 0; i <= ws2.top; i++)
if (ws2.WayNode[i].cnNode->data == e){
mark2 = true;
break;
}
return mark1 && mark2;
}
else
return false;
}
一点点作业,记录一下
二叉树
最新推荐文章于 2021-11-10 21:04:50 发布