sicily_homeworkNotes

1、二叉树的遍历(simple)

将一组数前中序遍历输出

#include<iostream>

using namespace std;
struct node {
    int data;
    node *left;
    node *right;
    node(int temp) {
        left = right = NULL;
        data = temp;
    }
};
void insert(node *root, int num) {
    if (num <= root->data) {
        if (root->left == NULL) {
            root->left = new node(num);
        } else {
            insert(root->left, num);
        }
    } else {
        if (root->right == NULL) {
            root->right = new node(num);
        } else {
            insert(root->right, num);
        }
    }
}
void inOrder(node *root) {
    if (root != NULL) {
        inOrder(root->left);
        cout << root->data << " " ;
        inOrder(root->right);
    }
}
void preOrder(node *root) {
    if (root != NULL) {
        cout << root->data << " " ;
        preOrder(root->left);
        preOrder(root->right);
    }
}

int main() {
    int n;
    while(cin >> n && n != 0) {
        int num;
        cin >> num;
        node *root;
        root = new node(num);
        for (int i = 1; i < n; i++) {
            cin >> num;
            insert(root,num);
        }

        inOrder(root);
        cout << endl;
        preOrder(root);
        cout << endl;

    }
}

2、二叉树的简单倒置和显示

#include<iostream>

using namespace std;
struct node {
    int data;
    node *left;
    node *right;
    node(int temp) {
        left = right = NULL;
        data = temp;
    }
};
void insert(node *root, int num) {
    if (num <= root->data) {
        if (root->left == NULL) {
            root->left = new node(num);
        } else {
            insert(root->left, num);
        }
    } else {
        if (root->right == NULL) {
            root->right = new node(num);
        } else {
            insert(root->right, num);
        }
    }
}

void interchange(node *root) {
    if (root != NULL) {
        if (root->left != NULL || root->right != NULL) {
            //交换左右子树
            node * temp = root->left;
            root->left = root->right;
            root->right = temp;

            if (root->left != NULL) {
                interchange(root->left);
            }
            if (root->right != NULL) {
                interchange(root->right);
            }
        }
    }
}

void inOrder(node *root, int height) {
    if (root != NULL) {
        inOrder(root->left,height+1);
        for (int i = 0; i < height; i++) {
            cout << "\t";
        }
        cout << ">>"<<root->data<< endl;
        inOrder(root->right, height+1);
    } else {
        return;
    }
}

int main() {
    int n;
    cout << "请输入要创建的二叉树的结点数: "; 
    while(cin >> n && n != 0) {
        int num;
        cout <<"请输入创建的二叉树的具体结点的权值: "<<endl; 
        cin >> num;
        node *root;
        root = new node(num);
        for (int i = 1; i < n; i++) {
            cin >> num;
            insert(root,num);
        }
        cout << "未进行处理的二叉树:(横向输出)"<<endl; 
        inOrder(root,0);
        cout << "\n\n";
        cout << endl;
        interchange(root);
        cout << "左右子树对换后的二叉树:(横向输出)"<<endl;
        inOrder(root,0);
        cout << "\n\n"; 
        cout << endl;
        cout << "请输入要创建的二叉树的结点数: ";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值