二叉树

来源:互联网 发布:菲律宾妹子 知乎 编辑:程序博客网 时间:2024/06/06 19:20
//二叉树好多操作
#include <iostream>#include <stdio.h>#include <stack>#include <cstdlib>#include <queue>using namespace std;typedef struct BinaryTreeNode{    int data;    struct BinaryTreeNode *leftchild, *rightchild;} BinaryTreeNode;BinaryTreeNode* CreatBinaryTree(){    BinaryTreeNode *root;    int num;    cin >> num;    if (num == 0)    {        root = NULL;    }    else    {        root = new BinaryTreeNode();        root->data = num;        cout << "请输入" << root->data << "结点的左子结点" << endl;        root->leftchild = CreatBinaryTree();        cout << "请输入" << root->data << "结点的右子结点" << endl;        root->rightchild = CreatBinaryTree();    }    return root;}void levelorder(BinaryTreeNode *root){    using std::queue;    queue<BinaryTreeNode *> nodeQueue;    BinaryTreeNode *pointer = root;    if(pointer)    {        nodeQueue.push(pointer);    }    while(!nodeQueue.empty())    {        pointer = nodeQueue.front();        printf("%3d", pointer->data);        nodeQueue.pop();        if(pointer->leftchild)        {            nodeQueue.push(pointer->leftchild);        }        if(pointer->rightchild)        {            nodeQueue.push(pointer->rightchild);        }    }}void preorder_Recursion(BinaryTreeNode* root){    if (root == NULL)    {        return;    }    else    {        printf("%3d", root->data);        preorder_Recursion(root->leftchild);        preorder_Recursion(root->rightchild);    }}void preorder(BinaryTreeNode* root){    using std::stack;    stack<BinaryTreeNode*> nodeStack;    BinaryTreeNode *pointer = root;    while (!nodeStack.empty() || pointer)    {        if (pointer)        {            printf("%3d", pointer->data);            if (pointer->rightchild != NULL)            {                nodeStack.push(pointer->rightchild);            }            pointer = pointer->leftchild;        }        else        {            pointer = nodeStack.top();            nodeStack.pop();        }    }}void inorder_Recursion(BinaryTreeNode * root){    if (root == NULL)    {        return;    }    else    {        inorder_Recursion(root->leftchild);        printf("%3d", root->data);        inorder_Recursion(root->rightchild);    }}void inorder(BinaryTreeNode * root){    using std::stack;    stack<BinaryTreeNode*>nodeStack;    BinaryTreeNode *pointer = root;    while (!nodeStack.empty() || pointer)    {        if (pointer)        {            nodeStack.push(pointer);            pointer = pointer->leftchild;        }        else        {            pointer = nodeStack.top();            printf("%3d\n", pointer->data);            pointer = pointer->rightchild;            nodeStack.pop();        }    }}void postorder_Recursion(BinaryTreeNode * root){    if (root == NULL)    {        return;    }    else    {        postorder_Recursion(root->leftchild);        postorder_Recursion(root->rightchild);        printf("%3d\n", root->data);    }}void postorder(BinaryTreeNode * root){    using std::stack;    stack<BinaryTreeNode*>nodeStack;    BinaryTreeNode *pointer = root;    BinaryTreeNode *pre = root;    while (pointer)    {        for (; pointer->leftchild != NULL; pointer = pointer->leftchild)        {            nodeStack.push(pointer);        }        while (pointer != NULL && (pointer->rightchild == NULL || pointer->rightchild == pre))        {            printf("%3d", pointer->data);            pre = pointer;            if (nodeStack.empty())            {                return;            }            pointer = nodeStack.top();            nodeStack.pop();        }        nodeStack.push(pointer);        pointer = pointer->rightchild;    }}int leaf_num(BinaryTreeNode * root){    static int leaf_count = 0;    if (root)    {        if (root->leftchild == NULL && root->rightchild == NULL)        {            leaf_count++;        }        leaf_num(root->leftchild);        leaf_num(root->rightchild);    }    return leaf_count;}int Node_num(BinaryTreeNode *root){    static int Node_count = 0;    if (root)    {        ++Node_count;        Node_num(root->leftchild);        Node_num(root->rightchild);    }    else    {        return Node_count;    }}int depth_BinaryTree(BinaryTreeNode *&root){    static int h1=0;    static int h2=0;    if (root==NULL)    {        return -1;    }    h1 = depth_BinaryTree(root->leftchild);    h2 = depth_BinaryTree(root->rightchild);    return h1>h2 ? (h1 + 1) : (h2 + 1);}BinaryTreeNode* Find_Tree_Node(BinaryTreeNode *root, int Find_data, int *flags){    BinaryTreeNode *parent_node = root;    BinaryTreeNode *temp = root;    *flags = 0;    while(temp != NULL)    {        if(temp->data == Find_data)        {            return parent_node;        }        else        {            parent_node = temp;            if(temp->data > Find_data)            {                temp = temp -> leftchild;                *flags = -1;            }            else            {                temp = temp -> rightchild;                *flags = 1;            }        }    }    return NULL;}BinaryTreeNode* Delete_Tree_Node(BinaryTreeNode *root ,BinaryTreeNode *parent_node,int flags){    BinaryTreeNode *deleteNode = NULL;    if(parent_node == NULL)    {        cout<<"未找到结点!"<<endl;        return root;    }    switch(flags)    {    case -1:    {        deleteNode = parent_node -> leftchild;        break;    }    case 0:    {        deleteNode = parent_node;        break;    }    case 1:    {        deleteNode = parent_node -> rightchild;        break;    }    default:    {        cout<<"ERROR!"<<endl;        exit(1);    }    }    if(deleteNode -> leftchild == NULL && deleteNode ->rightchild == NULL)    {        if(parent_node -> leftchild == deleteNode)        {            parent_node -> leftchild = NULL;        }        else if(parent_node -> rightchild == deleteNode)        {            parent_node->rightchild = NULL;        }        else        {            parent_node = NULL;        }        free(deleteNode);    }    else if(deleteNode -> leftchild == NULL && deleteNode -> rightchild != NULL)    {        if(deleteNode -> data == root -> data)        {            root = deleteNode -> rightchild;        }        else        {            if(parent_node -> leftchild -> data == deleteNode -> data)            {                parent_node -> leftchild = deleteNode -> rightchild;            }            else            {                parent_node -> rightchild = deleteNode -> rightchild;            }        }        free(deleteNode);    }    else if(deleteNode -> leftchild != NULL && deleteNode -> rightchild == NULL)    {        if(deleteNode -> data == root -> data)        {            root = deleteNode -> leftchild;        }        else        {            if(parent_node -> leftchild -> data == deleteNode -> data)            {                parent_node -> leftchild = deleteNode -> leftchild;            }            else            {                parent_node -> rightchild = deleteNode -> leftchild;            }        }        free(deleteNode);    }    else    {        BinaryTreeNode *temp = deleteNode -> leftchild;        BinaryTreeNode *tempParent = deleteNode;        while(temp -> rightchild != NULL)        {            tempParent = temp;            temp = temp -> rightchild;        }        deleteNode -> data = temp -> data;        if(tempParent -> leftchild == temp)        {            tempParent -> leftchild = temp -> leftchild;        }        else        {            tempParent -> rightchild = temp -> leftchild;        }        printf("temp = %d\n", temp -> data);        free(temp);    }    return root;}void Show_Binarytree(BinaryTreeNode * root){    BinaryTreeNode *stack[100];    BinaryTreeNode *p;    int level[100][2];    int top, n, i;    int width = 4;    if (root != NULL)    {        printf("\n二叉树的表示法f:\n");        top = 1;        stack[top] = root;        level[top][0] = width;        while (top>0)        {            p = stack[top];            n = level[top][0];            for (i = 1; i <= n; i++)                printf(" ");            printf("%d", p->data);            for (i = n + 1; i<60; i += 2)                printf("*");            printf("\n\t\t");            top--;            if (p->rightchild != NULL)            {                top++;                stack[top] = p->rightchild;                level[top][0] = n + width;                level[top][1] = 2;            }            if (p->leftchild != NULL)            {                top++;                stack[top] = p->leftchild;                level[top][0] = n + width;                level[top][1] = 1;            }        }    }}void Destroy_BinaryTree(BinaryTreeNode *root){    if (root -> leftchild == NULL && root -> rightchild == NULL)    {        delete root;    }    else    {        if(root->leftchild)        {            Destroy_BinaryTree(root->leftchild);            root->leftchild=NULL;        }        if(root->rightchild)        {            Destroy_BinaryTree(root->rightchild);            root->rightchild=NULL;        }    }}int Degress_one(BinaryTreeNode *root){    if(root == NULL)    {        return 0;    }    if(root->leftchild!=NULL&&root->rightchild==NULL||root->leftchild==NULL&&root->rightchild!=NULL)    {        return 1+Degress_one(root->leftchild)+Degress_one(root->rightchild);    }    return Degress_one(root->leftchild)+Degress_one(root->rightchild);}int Degress_double(BinaryTreeNode *root){    int count = 0;    if(root)    {        if(root->leftchild!=NULL)        {            count+=Degress_double(root->leftchild);        }        if(root->rightchild!=NULL)        {            count+=Degress_double(root->rightchild);        }        if(root->leftchild!=NULL&&root->rightchild!=NULL)        {            count+=1;        }    }    return count;}int Width(BinaryTreeNode *root){    static int n[20] = {0};    static int i = 0;    static int max = 0;    if(root!=NULL)    {        i++;        n[i]++;        if(n[i]>max)        {            max = n[i];        }        Width(root->leftchild);        Width(root->rightchild);        i--;        return max;    }    return 0;}int Max(BinaryTreeNode *root){    static int max_node = 0;    if(root == NULL)    {        return 0;    }    else    {        if(max_node<Max(root->leftchild))        {            max_node=Max(root->leftchild);        }        if(max_node<Max(root->rightchild))        {            max_node=Max(root->rightchild);        }        if(max_node<root->data)        {            max_node=root->data;        }    }    return max_node;}void Swap(BinaryTreeNode *root){    if(root!=NULL)    {        BinaryTreeNode *temp;        temp = root -> leftchild;        root -> leftchild = root -> rightchild;        root -> rightchild = temp;        Swap(root->leftchild);        Swap(root->rightchild);    }}int isFull(BinaryTreeNode *root){    using std::queue;    BinaryTreeNode *p = root;    queue<BinaryTreeNode*> q;    int tag = 0;    if(p==NULL) return true;    q.push(p);    while(!q.empty())    {        p = q.front();        q.pop();        if(p -> leftchild && !tag)        {            q.push(p->leftchild);        }        else if(p->leftchild)        {            return 0;        }        else        {            tag = 1;        }        if(p -> rightchild && !tag)        {            q.push(p->rightchild);        }        else if(p->rightchild)        {            return 0;        }        else        {            tag = 1;        }    }    return 1;}int menu(){    int choice;    cout << "~二叉树~ ;)" << endl;    cout << "***************************" << endl;    cout << "*********主菜单************" << endl;    cout << " * 1  建立二叉树          *" << endl;    cout << " * 2  层序遍历            *" << endl;    cout << " * 3  先序遍历——递归    *" << endl;    cout << " * 4  先序遍历——非递归  *" << endl;    cout << " * 5  中序遍历——递归    *" << endl;    cout << " * 6  中序遍历——非递归  *" << endl;    cout << " * 7  后序遍历——递归    *" << endl;    cout << " * 8  后序遍历——非递归  *" << endl;    cout << " * 9  叶子结点个数        *" << endl;    cout << " * 10 结点个数            *" << endl;    cout << " * 11 显示深度            *" << endl;    cout << " * 12 显示二叉树          *" << endl;    cout << " * 13 删除二叉树结点      *" << endl;    cout << " * 14 销毁二叉树          *" << endl;    cout << " * 15 度为1的结点         *" << endl;    cout << " * 16 度为2的结点         *" << endl;    cout << " * 17 二叉树宽度为:      *" << endl;    cout << " * 18 节点中最大的数据:   *" << endl;    cout << " * 19 交换左右结点:       *" << endl;    cout << " * 20 判断是否为完全二叉树*" << endl;    cout << " * 21 退出程序            *" << endl;    cout << "***************************" << endl;    cout << "请选择~" << endl;    cin >> choice;    return choice;}int main(){    BinaryTreeNode *root = NULL;    BinaryTreeNode *parent_node = NULL;    int choice;    static int flags = 0;    do    {        choice = menu();        if (choice == 1)        {            system("cls");            cout << "二叉树的建立,以输入“0”表示结束:\n";            cout << "输入根结点" << endl;            root = CreatBinaryTree();            cout << "successfully~" << endl;        }        else if (choice == 2)        {            system("cls");            levelorder(root);            system("pause");        }        else if (choice == 3)        {            system("cls");            preorder_Recursion(root);            system("pause");        }        else if (choice == 4)        {            system("cls");            preorder(root);            system("pause");        }        else if (choice == 5)        {            system("cls");            inorder_Recursion(root);            system("pause");        }        else if (choice == 6)        {            system("cls");            inorder(root);            system("pause");        }        else if (choice == 7)        {            system("cls");            postorder_Recursion(root);            system("pause");        }        else if (choice == 8)        {            system("cls");            postorder(root);            system("pause");        }        else if (choice == 9)        {            system("cls");            cout << "二叉树叶子数为" << leaf_num(root) << endl;            system("pause");        }        else if (choice == 10)        {            system("cls");            cout << "二叉树结点数为:" << Node_num(root) << endl;            system("pause");        }        else if (choice == 11)        {            system("cls");            cout << "二叉树深度为" << depth_BinaryTree(root) << endl;            system("pause");        }        else if (choice == 12)        {            system("cls");            Show_Binarytree(root);            system("pause");        }        else if (choice == 13)        {            system("cls");            cout << "请输入要删除的结点 :" << endl;            int Find_data;            cin >> Find_data;            parent_node = Find_Tree_Node(root,Find_data,&flags);            Delete_Tree_Node(root,parent_node,flags);            cout << "已删除!" << endl;            system("pause");        }        else if (choice == 14)        {            system("cls");            Destroy_BinaryTree(root);            delete root;            root = NULL;            cout << "二叉树已死!" << endl;            system("pause");        }        else if (choice == 15)        {            system("cls");            cout << "二叉树度为1结点个数为:" << Degress_one(root)<< endl;            system("pause");        }        else if (choice == 16)        {            system("cls");            cout << "二叉树度为2结点个数为:" << Degress_double(root) << endl;            system("pause");        }        else if (choice == 17)        {            system("cls");            cout << "二叉树宽度:" << Width(root)<< endl;            system("pause");        }        else if (choice == 18)        {            system("cls");            cout << "节点中最大的数据为:" << Max(root) << endl;            system("pause");        }        else if (choice == 19)        {            system("cls");            Swap(root);            system("pause");        }        else if (choice == 20)        {            system("cls");            if(isFull(root))            {                cout<<"此树为完全二叉树"<<endl;            }            else            {                cout<<"不是完全二叉树"<<endl;            }            system("pause");        }        else if (choice == 21)        {            exit(0);        }    }    while (choice <= 21);    return 0;}

原创粉丝点击