1.由二叉树的后序遍历序列和中序遍历序列建立二叉树的二叉链表(设二叉树的元素互异)。
代码:
#include
#include
using namespace std;
const int MaxTreeSize = 50;
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
BiTNode* lc, * rc;
}*BiTree;
typedef struct ANode
{
BiTree data;
ANode* next;
}*LQueuePtr;
struct LQueue
{
LQueuePtr front, rear;
};
void BiTreeLists(BiTree T)
{
if (!T)
{
cout << '#';
return;
}
printf("%c", T->data);
if (T->lc || T->rc)
{
cout << '(';
BiTreeLists(T->lc);
cout << ',';
BiTreeLists(T->rc);
cout << ')';
}
}
void CreateBiTree(BiTree& T, char s[], int& i)
{
i++;
if (s[i] == '#')
{
T = NULL;
return;
}
T = new BiTNode;
T->data = s[i];
CreateBiTree(T->lc, s, i);
CreateBiTree(T->rc, s, i);
}
void CreateBiTree(BiTree& T, char s[])
{
int i = -1;
CreateBiTree(T, s, i);
}
bool create1(BiTree& T,char preorder[],char inorder[],int n)
{
int i=0;
if (!n)
{
T = NULL;
return true;
}
while (i < n && inorder[i] != preorder[0])
{
i++;
}
if (i == n)
{
T = NULL;
return false;
}
T = new BiTNode;
T->data = preorder[0];
return create1(T->lc, preorder + 1, inorder, i) &&create1(T->rc, preorder + i + 1, inorder + i + 1, n - i - 1);
}
bool create2(BiTree& T, char P1[], char P2[])
{
int a = strlen(P1);
int b = strlen(P2);
if (a!=b) return false;
else
{
return create1(T, P1, P2, a);;
}
}
BiTNode* buildTree(char inorder[], int il, int ir, char postorder[], int pl, int pr) {
if(pr < pl) return NULL;
if(pr == pl) {
BiTree T;
T = new BiTNode;
T->data = postorder[pl];
return T;
}
char rootVal = postorder[pr];
int index = -1;
for(int i = il; i <= ir; i ++) {
if(inorder[i] == rootVal) {
index = i;
break;
}
}
BiTree root;
root = new BiTNode;
root->data = rootVal;
if(index > il) {
root->lc = buildTree(inorder, il, index - 1, postorder, pl, pl + index - il - 1);
}
if(index < ir) {
root->rc = buildTree(inorder, index + 1, ir, postorder, pl + index - il, pr - 1);
}
return root;
}
BiTNode* create3(char postorder[],char inorder[])
{
int a = strlen(postorder);
int b = strlen(inorder);
if (a!=b) return NULL;
else
{
return buildTree(inorder, 0, b - 1, postorder, 0, a - 1);
}
}
int main()
{
BiTree T;
char preorder[] = "AEDCB";
char inorder[] = "DECBA";
char postorder[] = "DBCEA";
cout << "由先序遍历和中序遍历建立的二叉树为:" ;
create2(T, preorder, inorder);
BiTreeLists(T);
cout << "\n" ;
BiTree T1;
cout << "由后序遍历和中序遍历建立的二叉树为:" ;
T1 = create3(postorder, inorder);
BiTreeLists(T1);
cout << endl;
return 0;
}
2.设树的数据元素为字母,由树的广义表字符串建立树的二叉链表。
代码:
#include
#include
using namespace std;
const int MaxTreeSize = 50;
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
BiTNode* lc, * rc;
}*BiTree;
typedef struct ANode
{
BiTree data;
ANode* next;
}*LQueuePtr;
struct LQueue
{
LQueuePtr front, rear;
};
void BiTreeLists(BiTree T)
{
if (!T)
{
cout << '#';
return;
}
printf("%c", T->data);
if (T->lc || T->rc)
{
cout << '(';
BiTreeLists(T->lc);
cout << ',';
BiTreeLists(T->rc);
cout << ')';
}
}
void CreateBiTree(BiTree& T, char s[], int& i)
{
i++;
if (s[i] == '#')
{
T = NULL;
return;
}
T = new BiTNode;
T->data = s[i];
CreateBiTree(T->lc, s, i);
CreateBiTree(T->rc, s, i);
}
void CreateBiTree(BiTree& T, char s[])
{
int i = -1;
CreateBiTree(T, s, i);
}
bool create1(BiTree& T,char preorder[],char inorder[],int n)
{
int i=0;
if (!n)
{
T = NULL;
return true;
}
while (i < n && inorder[i] != preorder[0])
{
i++;
}
if (i == n)
{
T = NULL;
return false;
}
T = new BiTNode;
T->data = preorder[0];
return create1(T->lc, preorder + 1, inorder, i) &&create1(T->rc, preorder + i + 1, inorder + i + 1, n - i - 1);
}
bool create2(BiTree& T, char P1[], char P2[])
{
int a = strlen(P1);
int b = strlen(P2);
if (a!=b) return false;
else
{
return create1(T, P1, P2, a);;
}
}
int NodeCountOne(BiTree T)
{
if(!T) return 0;
int cnt = 0;
if((T->lc!=NULL&&T->rc==NULL) || (T->lc==NULL&&T->rc!=NULL)) cnt++;
cnt += NodeCountOne(T->lc);
cnt += NodeCountOne(T->rc);
return cnt;
}
int NodeCountTow(BiTree T)
{
if(!T) return 0;
int cnt = 0;
if(T->lc!=NULL&&T->rc!=NULL) cnt++;
cnt+=NodeCountTow(T->lc);
cnt+=NodeCountTow(T->rc);
return cnt;
}
int CreateBiTree(BiTree *T,char *str)
{
BiTree S[MaxTreeSize], p = NULL;
int top = 0, k = 0, j = 0;
char ch;
*T = NULL;
ch = str[j];
while (ch!='@')
{
switch(ch)
{
case '(':
S[top++] = p;
k = 1;
break;
case ')':
top--;
break;
case ',':
k = 2;
break;
default:
p = (BiTree)malloc(sizeof(BiTNode));
p->data = ch;
p->lc = p->rc = NULL;
if (*T==NULL)
{
*T = p;
}
else
{
switch (k)
{
case 1:
S[top - 1]->lc = p;
break;
case 2:
S[top - 1]->rc = p;
break;
}
}
break;
}
ch = str[++j];
}
return 1;
}
int main()
{
BiTree T;
char str[] = "A(B(D),C(F(,E),G))@";
CreateBiTree(&T,str);
cout << "由广义表字符串创建的二叉树为:" ;
BiTreeLists(T);
cout << endl;
return 0;
}
3.打印数的边。
代码:
#include
#include
#include
#include
using namespace std;
// 定义二叉树节点结构体
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// 输出二叉树边的函数
vector printEdges(TreeNode* root) {
vector res;
if (!root) return res;
// 定义队列存储节点和对应的编号
queue> q;
q.push(make_pair(root, 1));
int curLevel = 1;
int curIdx = 1;
while (!q.empty()) {
TreeNode* curNode = q.front().first;
int curNodeIdx = q.front().second;
q.pop();
if (curNodeIdx != curIdx) {
curIdx = curNodeIdx;
curLevel *= 2;
}
if (curNode->left) {
q.push(make_pair(curNode->left, curNodeIdx * 2));
res.push_back(to_string(curNodeIdx) + "->" + to_string(curNodeIdx * 2));
}
if (curNode->right) {
q.push(make_pair(curNode->right, curNodeIdx * 2 + 1));
res.push_back(to_string(curNodeIdx) + "->" + to_string(curNodeIdx * 2 + 1));
}
}
return res;
}
// 测试
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->right = new TreeNode(4);
root->right->left = new TreeNode(5);
root->right->right = new TreeNode(6);
vector res = printEdges(root);
cout << "Edges: ";
for (string edge : res) {
cout << edge << " ";
}
cout << endl;
return 0;
}
4.统计度为1的节点数量和度为2的节点数量。
代码:
#include
#include
using namespace std;
const int MaxTreeSize = 50;
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
BiTNode* lc, * rc;
}*BiTree;
typedef struct ANode
{
BiTree data;
ANode* next;
}*LQueuePtr;
struct LQueue
{
LQueuePtr front, rear;
};
void BiTreeLists(BiTree T)
{
if (!T)
{
cout << '#';
return;
}
printf("%c", T->data);
if (T->lc || T->rc)
{
cout << '(';
BiTreeLists(T->lc);
cout << ',';
BiTreeLists(T->rc);
cout << ')';
}
}
void CreateBiTree(BiTree& T, char s[], int& i)
{
i++;
if (s[i] == '#')
{
T = NULL;
return;
}
T = new BiTNode;
T->data = s[i];
CreateBiTree(T->lc, s, i);
CreateBiTree(T->rc, s, i);
}
void CreateBiTree(BiTree& T, char s[])
{
int i = -1;
CreateBiTree(T, s, i);
}
bool create1(BiTree& T,char preorder[],char inorder[],int n)
{
int i=0;
if (!n)
{
T = NULL;
return true;
}
while (i < n && inorder[i] != preorder[0])
{
i++;
}
if (i == n)
{
T = NULL;
return false;
}
T = new BiTNode;
T->data = preorder[0];
return create1(T->lc, preorder + 1, inorder, i) &&create1(T->rc, preorder + i + 1, inorder + i + 1, n - i - 1);
}
bool create2(BiTree& T, char P1[], char P2[])
{
int a = strlen(P1);
int b = strlen(P2);
if (a!=b) return false;
else
{
return create1(T, P1, P2, a);;
}
}
int NodeCountOne(BiTree T)
{
if(!T) return 0;
int cnt = 0;
if((T->lc!=NULL&&T->rc==NULL) || (T->lc==NULL&&T->rc!=NULL)) cnt++;
cnt += NodeCountOne(T->lc);
cnt += NodeCountOne(T->rc);
return cnt;
}
int NodeCountTow(BiTree T)
{
if(!T) return 0;
int cnt = 0;
if(T->lc!=NULL&&T->rc!=NULL) cnt++;
cnt+=NodeCountTow(T->lc);
cnt+=NodeCountTow(T->rc);
return cnt;
}
int main()
{
BiTree T;
char preorder[] = "AEDCB";
char inorder[] = "DECBA";
char postorder[] = "DBCEA";
cout << "由先序遍历和中序遍历建立的二叉树为:" ;
create2(T, preorder, inorder);
BiTreeLists(T);
cout << "\n度为1的节点数为" ;
cout << NodeCountOne(T);
cout << "\n度为2的节点数为" ;
cout << NodeCountTow(T);
cout << endl;
return 0;
}
5.由广义字符表创建二叉树。
代码:
#include
#include
using namespace std;
const int MaxTreeSize = 50;
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
BiTNode* lc, * rc;
}*BiTree;
typedef struct ANode
{
BiTree data;
ANode* next;
}*LQueuePtr;
struct LQueue
{
LQueuePtr front, rear;
};
void BiTreeLists(BiTree T)
{
if (!T)
{
cout << '#';
return;
}
printf("%c", T->data);
if (T->lc || T->rc)
{
cout << '(';
BiTreeLists(T->lc);
cout << ',';
BiTreeLists(T->rc);
cout << ')';
}
}
void CreateBiTree(BiTree& T, char s[], int& i)
{
i++;
if (s[i] == '#')
{
T = NULL;
return;
}
T = new BiTNode;
T->data = s[i];
CreateBiTree(T->lc, s, i);
CreateBiTree(T->rc, s, i);
}
void CreateBiTree(BiTree& T, char s[])
{
int i = -1;
CreateBiTree(T, s, i);
}
bool create1(BiTree& T,char preorder[],char inorder[],int n)
{
int i=0;
if (!n)
{
T = NULL;
return true;
}
while (i < n && inorder[i] != preorder[0])
{
i++;
}
if (i == n)
{
T = NULL;
return false;
}
T = new BiTNode;
T->data = preorder[0];
return create1(T->lc, preorder + 1, inorder, i) &&create1(T->rc, preorder + i + 1, inorder + i + 1, n - i - 1);
}
bool create2(BiTree& T, char P1[], char P2[])
{
int a = strlen(P1);
int b = strlen(P2);
if (a!=b) return false;
else
{
return create1(T, P1, P2, a);;
}
}
int NodeCountOne(BiTree T)
{
if(!T) return 0;
int cnt = 0;
if((T->lc!=NULL&&T->rc==NULL) || (T->lc==NULL&&T->rc!=NULL)) cnt++;
cnt += NodeCountOne(T->lc);
cnt += NodeCountOne(T->rc);
return cnt;
}
int NodeCountTow(BiTree T)
{
if(!T) return 0;
int cnt = 0;
if(T->lc!=NULL&&T->rc!=NULL) cnt++;
cnt+=NodeCountTow(T->lc);
cnt+=NodeCountTow(T->rc);
return cnt;
}
int CreateBiTree(BiTree *T,char *str)
{
BiTree S[MaxTreeSize], p = NULL;
int top = 0, k = 0, j = 0;
char ch;
*T = NULL;
ch = str[j];
while (ch!='@')
{
switch(ch)
{
case '(':
S[top++] = p;
k = 1;
break;
case ')':
top--;
break;
case ',':
k = 2;
break;
default:
p = (BiTree)malloc(sizeof(BiTNode));
p->data = ch;
p->lc = p->rc = NULL;
if (*T==NULL)
{
*T = p;
}
else
{
switch (k)
{
case 1:
S[top - 1]->lc = p;
break;
case 2:
S[top - 1]->rc = p;
break;
}
}
break;
}
ch = str[++j];
}
return 1;
}
int main()
{
BiTree T;
char str[] = "A(B(D),C(F(,E),G))@";
CreateBiTree(&T,str);
cout << "由广义表字符串创建的二叉树为:" ;
BiTreeLists(T);
cout << endl;
return 0;
}