1.设计一个实现一棵二叉树复制的算法。
#include<iostream>
using namespace std;
//二叉树节点结构体
struct TreeNode{
char element;
TreeNode *left;
TreeNode *right;
};
//创建二叉树
void creatTree(TreeNode *&root){
char c;
cin>>c;
if(c=='#'){
root=NULL;
}
else{
root=new TreeNode;
root->element=c;
creatTree(root->left);
creatTree(root->right);
}
}
//交换左右子树
void change(TreeNode *root){
if(root==NULL){
return ;
}
swap(root->left,root->right);
change(root->left);
change(root->right);
}
//中序遍历
void InOrder(TreeNode *root){
if(root==NULL){
return ;
}
InOrder(root->left);
cout<<root->element<<" ";
InOrder(root->right);
}
//判断二叉树是否相等
bool isEqual(TreeNode *root1,TreeNode *root2){
if(root1==NULL&&root2==NULL)
{
return true;
}
return root1->element==root2->element&&
isEqual(root1->left,root2->left)&&
isEqual(root1->right,root2->right);
}
//复制二叉树的算法
void CopyTree(TreeNode *tree,TreeNode *&newTree){
if(tree==NULL){
newTree=NULL;
}
else{
newTree=new TreeNode;
newTree->element=tree->element;
CopyTree(tree->left,newTree->left);
CopyTree(tree->right,newTree->right);
}
}
int main(){
TreeNode *root1=new TreeNode;
creatTree(root1);
cout<<"root1:中序遍历"<<endl;
InOrder(root1);
cout<<endl;
TreeNode *root2=new TreeNode;
CopyTree(root1,root2);
cout<<"root2:中序遍历"<<endl;
InOrder(root2);
cout<<endl;
}
2.编写一个将二叉树的所有叶子结点从左向右链接成单链表的算法。
#include<iostream>
using namespace std;
//二叉树节点结构体
struct TreeNode{
char element;
TreeNode *left;
TreeNode *right;
};
//创建二叉树
void creatTree(TreeNode *&root){
char c;
cin>>c;
if(c=='#'){
root=NULL;
}
else{
root=new TreeNode;
root->element=c;
creatTree(root->left);
creatTree(root->right);
}
}
struct ListNode{
char element;
ListNode *next=NULL;
};
void addNode(ListNode *head,char data){
ListNode *p=head;
while(p->next!=NULL){
p=p->next;
}
ListNode *newNode=new ListNode;
newNode->element=data;
p->next=newNode;
}
void printList(ListNode *head){
ListNode *p=head->next;
while(p){
cout<<p->element<<" ";
p=p->next;
}
cout<<endl;
}
void leafToList(TreeNode *root,ListNode *head){
if(root==NULL){
return ;
}
if(root->left==NULL&&root->right==NULL){
addNode(head,root->element);
}
else{
leafToList(root->left,head);
leafToList(root->right,head);
}
}
int main(){
TreeNode *root=new TreeNode;
creatTree(root);
ListNode *head=new ListNode;
leafToList(root,head);
printList(head);
return 0;
}
3.设具有n个结点的完全二叉树采用顺序存储结构,试写一个算法将该顺序存储结构转换为二叉链式存储结构。
/*
设具有n个结点的二叉树采用顺序存储结构,
试写一个算法将该顺序存储结构转换为二叉完全链式存储结构
*/
#include<iostream>
#include<queue>
using namespace std;
struct TreeNode{
char val;
TreeNode *left=NULL;
TreeNode *right=NULL;
};
struct TreeArray{
char *val;
int size;
int maxsize;
TreeArray(int _maxsize){
size=0;
maxsize=_maxsize;
val=new char[maxsize];
}
};
void PreOrder(TreeNode* root){
if(root==NULL){
return ;
}
cout<<root->val<<" ";
PreOrder(root->left);
PreOrder(root->right);
}
//顺序存储结构转换为二叉链式存储结构
void creatTreelist(TreeNode *&root,int i,TreeArray *treearray){
if(i>(treearray->size)){
root=NULL;
}
else{
root=new TreeNode;
root->val=treearray->val[i];
creatTreelist(root->left,2*i,treearray);
creatTreelist(root->right,2*i+1,treearray);
}
}
int main(){
TreeArray *treearray=new TreeArray(16);
int n;
cout<<"输入完全二叉树的节点"<<endl;
cin>>n;
cout<<"输入完全二叉树的节点值"<<endl;
for(int i=1;i<=n;i++){
cin>>treearray->val[i];
treearray->size++;
}
TreeNode *root=new TreeNode;
creatTreelist(root,1,treearray);
PreOrder(root);
return 0;
}
4.设具有n个结点的二叉树采用二叉链式存储结构,试写出一个算法将该二叉链式存储结构转换为顺序存储结构。
/*
设具有n个结点的二叉树采用二叉链式存储结构,
试写出一个算法将该二叉链式存储结构转换为顺序存储结构序存储结构转换为二叉完全链式存储结构
*/
#include<iostream>
#include<queue>
using namespace std;
struct TreeNode {
char val;
TreeNode* left = NULL;
TreeNode* right = NULL;
};
struct TreeArray {
char* val;
int size;
int maxsize;
TreeArray(int _maxsize) {
size = 0;
maxsize = _maxsize;
val = new char[maxsize];
}
};
void creatTree(TreeNode*& root) {
char c;
cin >> c;
if (c == '#') {
root = NULL;
}
else {
root = new TreeNode;
root->val = c;
creatTree(root->left);
creatTree(root->right);
}
}
void PreOrder(TreeNode* root) {
if (root == NULL) {
return;
}
cout << root->val << " ";
PreOrder(root->left);
PreOrder(root->right);
}
//一个算法将该二叉链式存储结构转换为顺序存储结构序存储结构转换为二叉完全链式存储结构
void creatTreeArray(TreeNode* root, TreeArray* Tarray) {
queue<TreeNode*> q;
q.push(root);
int i = 1;
while (!q.empty()) {
root = q.front();
Tarray->val[i]=root->val;
q.pop();
Tarray->size++;
i++;
if (root->left != NULL) {
q.push(root->left);
}
if (root->right != NULL) {
q.push(root->right);
}
}
}
int main() {
TreeArray* treearray = new TreeArray(16);
TreeNode* root = new TreeNode;
cout << "输入完全二叉树前序序列" << endl;
creatTree(root);
PreOrder(root);
cout<<endl;
creatTreeArray(root, treearray);
for (int i = 1; i <= treearray->size; i++) {
cout << treearray->val[i] << " ";
}
return 0;
}