【数据结构和算法】二叉树操作题


title: 二叉树操作三道题(分享)
author: Codemon
date: 2022-11-12 09:06:11
tags:


二叉树操作三道题

A.二叉树左右孩子交换
Time Limit: 1000 MSMemory Limit: 32768 KTotal Submit: 116 (57 users)Total Accepted: 55 (52 users)Special Judge: No
Description
根据输入利用二叉链表创建二叉树,并将所有结点的左右孩子交换,并输出。说明:输入的第一行为根结点;第二行以后每行的第二元为第一元的左孩子,第三元为第一元的右孩子, 0表示空;其中#为输入结束标记。输出时按结点层次顺序输出。
Sample Input
A
A B C
B 0 D
C 0 E
D 0 0
E 0 0
#
Sample Output
A C B
C E 0
B D 0
E 0 0
D 0 0
Hint
输出有换行符
#include<iostream>
#include<map>
#include<queue>
#include<stdio.h>
#include<malloc.h>
using namespace std;
typedef struct node
{
    char val;
    struct node* left;
    struct node* right;
} node;
#define ALLOC_(name, xx) \
node * name = (node*)malloc(sizeof(node)); \
name->val = xx; \
name->left = name->right = NULL;

void buildTree(node*& t)
{
    char a, b, c;
    cin >> a;
    if (a == '#')
        return;
    map<char, node*> mp;
    ALLOC_(root, a);
    mp[root->val] = root;
    while (cin >> a)
    {
        if (a == '#')
            break;
        cin >> b >> c;
        auto parent = mp.find(a);
        if (b != '0') {
            auto left = mp.find(b);
            if (left == mp.end())
            {
                ALLOC_(p, b);
                mp[p->val] = p;
                left = mp.find(p->val);
            }
            parent->second->left = left->second;
        }
        if (c != '0') {
            auto right = mp.find(c);
            if (right == mp.end())
            {
                ALLOC_(p, c);
                mp[p->val] = p;
                right = mp.find(p->val);
            }
            parent->second->right = right->second;
        }
       
        

    }
    t = root;
    return;

}

void Swap(node*& T1, node*& T2)//交换两个二叉树结点指针的指向
{
    node* t = T1;
    T1 = T2;
    T2 = t;
}
void NodeSwap(node*& T)//交换二叉树每个结点的左孩子和右孩子
{
    //此算法根据二叉树先序遍历算法改造而来
    if (T != NULL)
    {
        if (T->left != NULL && T->right != NULL)//如果T的左孩子和右孩子都不空
        {
            //将"交换二叉树每个结点的左孩子和右孩子"转换为"交换二叉树每个结点的左孩子的数据域和右孩子的指针域".
            Swap(T->left, T->right);
        }
        else if (T->left != NULL && T->right == NULL)//如果T的左孩子不空且右孩子为空
        {
            //将T的左子树变为右子树
            T->right = T->left;
            T->left = NULL;
        }
        else if (T->left == NULL && T->right != NULL)//如果T的左孩子为空且右孩子不为空
        {
            //将T的右子树变为左子树
            T->left = T->right;
            T->right = NULL;
        }
        else//如果T的左孩子和右孩子都为空
        {
            //空操作
            ;
        }
        NodeSwap(T->left);
        NodeSwap(T->right);
    }
    else
    {
        ;
    }
}

void FloorPrint_QUEUE(node*& Tree) //层序遍历_队列实现
{
    queue <node*> q;
    if (Tree != NULL)
    {
        q.push(Tree);   //根节点进队列
    }

    while (q.empty() == false)  //队列不为空判断
    {
        cout << q.front()->val << " ";
    
        if (q.front()->left != NULL)   //如果有左孩子,leftChild入队列
        {
            q.push(q.front()->left);
            cout << q.front()->left->val<<" ";
        }
        else {
            cout << "0" << " ";
        }
        if (q.front()->right != NULL)   //如果有右孩子,rightChild入队列
        {
            q.push(q.front()->right);
            cout << q.front()->right->val << endl;
        }
        else {
            cout << "0" << endl;
        }
        q.pop();  //已经遍历过的节点出队列
    }

}

int main() {
    node* T;
    buildTree(T);
    NodeSwap(T);
    FloorPrint_QUEUE(T);
    return 0;
}
B.计算二叉树高度
Time Limit: 1000 MSMemory Limit: 32768 KTotal Submit: 89 (52 users)Total Accepted: 50 (49 users)Special Judge: No
Description
根据输入利用二叉链表创建二叉树,并输出二叉树的高度。说明:输入的第一行为根结点;第二行以后每行的第二元为第一元的左孩子,第三元为第一元的右孩子, 0表示空;其中#为输入结束标记。
Input
Output
Sample Input
A
A B D
B 0 C
D 0 0
C E 0
E 0 F
F 0 0
#
Sample Output
5
#include<iostream>
#include<map>
#include<stdio.h>
#include<malloc.h>
using namespace std;
typedef struct node
{
    char val;
    struct node* left;
    struct node* right;
} node;
#define ALLOC_(name, xx) \
node * name = (node*)malloc(sizeof(node)); \
name->val = xx; \
name->left = name->right = NULL;

void buildTree(node*& t)
{
    char a, b, c;
    cin >> a;
    if (a == '#')
        return;
    map<char, node*> mp;
    ALLOC_(root, a);
    mp[root->val] = root;
    while (cin >> a)
    {
        if (a == '#')
            break;
        cin >> b >> c;
        auto parent = mp.find(a);
        if (b != '0') {
            auto left = mp.find(b);
            if (left == mp.end())
            {
                ALLOC_(p, b);
                mp[p->val] = p;
                left = mp.find(p->val);
            }
            parent->second->left = left->second;
        }
        if (c != '0') {
            auto right = mp.find(c);
            if (right == mp.end())
            {
                ALLOC_(p, c);
                mp[p->val] = p;
                right = mp.find(p->val);
            }
            parent->second->right = right->second;
        }
       
        

    }
    t = root;
    return;

}

int GetHeight(node* BT) {
    int h1;
    int h2;
    if (!BT)
        return 0;
    else {
        h1 = GetHeight(BT->left);
        h2 = GetHeight(BT->right);
        return h1 > h2 ? ++h1 : ++h2;
    }
}

int main() {
    node* T;
    buildTree(T);
    cout << GetHeight(T) << endl;
    return 0;
}
C.计算二叉树叶子结点数
Time Limit: 1000 MSMemory Limit: 32768 KTotal Submit: 213 (57 users)Total Accepted: 64 (54 users)Special Judge: No
Description
Input
Sample Input
A B C 0 D 0 E #
Sample Output
2
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
#define len1 sizeof(BiTNode)
#define len2 sizeof(queue)
typedef char ElemType;//元素类型


typedef struct BiTNode {
 ElemType data;
 struct BiTNode* lchild, * rchild;//左右孩子指针
}BiTNode, * BiTree;

//辅助队列
typedef struct SqQueue {
 BiTree p;  //树的结点的指针
 struct SqQueue* pnext;
}SqQueue, * queue;


void createTree(BiTree& T)
{
 ElemType x;
 BiTree tree = NULL;//树根
 BiTree pnew = NULL;
 queue phead = NULL, ptail = NULL, listnew = NULL, pcur = NULL;
 int temp;
 do
 {
  scanf("%c", &x);
  if (x == '#')break;
  temp = getchar();
  pnew = (BiTree)calloc(1, len1);//新的树的节点
  pnew->data = x;
  listnew = (queue)calloc(1, len2);
  listnew->p = pnew;//树的结点入列
  if (tree == NULL)
  {
   tree = pnew;
   phead = listnew;
   ptail = listnew;
   pcur = listnew;
   continue;
  }
  else
  {
   ptail->pnext = listnew;  //尾插法使listnew入列
   ptail = listnew;
  }

  if (pcur->p->lchild == NULL )
  { 
   pcur->p->lchild = pnew;
  }
  else if (pcur->p->rchild == NULL )
  {
   pcur->p->rchild = pnew;
   pcur = pcur->pnext;
  }

 } while (1);
 T = tree;
}
int getLeafNum(BiTree root)
{
 if (NULL == root || root->data == '0')
 {
  return 0;
 }
 if ((NULL == root->lchild || root->lchild->data == '0') && (NULL == root->rchild|| root->rchild->data == '0'))
 {
  return 1;
 }

 int leftLeafNum = getLeafNum(root->lchild);
 int rightLeafNum = getLeafNum(root->rchild);
 int leafNum = leftLeafNum + rightLeafNum;
 return leafNum;
}

int main() {
 BiTree T;
 createTree(T);
 int s;
 s = getLeafNum(T);
 cout << s << endl;
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Codingemon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值