数据结构——二叉树——反转二叉树

#include<iostream>
using namespace std;
#define maxsize 1000
struct binary_tree_node
{
    char data;
    binary_tree_node* left;
    binary_tree_node* right;
};
class queue
{
private:
    binary_tree_node* bt[maxsize];
    int tail;
    int head;
public:
    queue();
    void push(binary_tree_node* btn);
    binary_tree_node* pop();
    bool is_empty();
};

queue::queue()
{   
    head = 0;
    tail = 0;
}
void queue::push(binary_tree_node *btn)
{
    bt[tail] = btn;
    tail++;
}

binary_tree_node* queue::pop()
{
    binary_tree_node* b = bt[head];
    head++;
    return b;

}
bool queue::is_empty()
{
    if (head==tail)
        return true;
    return false;
}
binary_tree_node* creat_binary_tree(int n)
{
    int i=0, j;
    char ch;
    binary_tree_node * T, *p, *s[maxsize];
    **cin.ignore();**
    while(**ch = getchar(),ch!='\n'**)
    {
        i++;
        if (ch != '*')
        {
            p = new binary_tree_node;
            p->data = ch;
            p->left = NULL;
            p->right = NULL;
            s[i] = p;
            if (i == 1)
                T = p;
            else
            {
                if (i % 2 == 0)
                {
                    j = i / 2;
                    s[j]->left = p;
                }
                if (i % 2 != 0)
                {
                    j = i / 2;
                    s[j]->right = p;
                }
            }
        }
    }
    return T;
}
binary_tree_node* reverse(binary_tree_node* T)
{
    if (T->left == NULL&&T->right == NULL)
        return T;
    else
    {   
        **if(T->left!=NULL)
        reverse(T->left);**
        if(T->right!=NULL)
        reverse(T->right);
        binary_tree_node*temp;
        temp = T->left;
        T->left = T->right;
        T->right = temp;
        return T;
    }
}
再反转的时候用的是递归,只有当该节点不为NULL的时候才能够再反转他的左右子树,先序后序中序都可以。
void print(binary_tree_node*T,int k)
{
    queue bt_queue;
    bt_queue.push(T);
    int i = 1;
    while (!bt_queue.is_empty() && i <= k)
    {   
        binary_tree_node* t;
        t=bt_queue.pop();
        **if (t->data!= '*') i++;**
        cout << t->data;
        binary_tree_node* n;
        n = new binary_tree_node;
        n->data ='*';
        n->left = NULL;
        n->right = NULL;
        if (t->left != NULL)
        {
            bt_queue.push(t->left);
        }
        **else
        {
            bt_queue.push(n);
        }**
        if (t->right != NULL)
        {
            bt_queue.push(t->right);
        }
        else
        {
            bt_queue.push(n);
        }
    }
}
//在print函数中出错最多,应为是层次遍历,所以会有NULL但是按照输出要求还要有*,所以数目很重要,并且NULL也要进入队列
int main()
{
    int n;
    cout << "please input n:";
    cin >> n;
    cout << "please input data:";
    int k;
    binary_tree_node*t=creat_binary_tree(n);
    binary_tree_node*result=reverse(t);
    print(result,n);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值