zoukankan      html  css  js  c++  java
  • 二叉树及排序二叉树的相关操作汇总

    前记:由于种种原因,以前一看到什么树啊链表啊,那就相当的恐惧,真是惭愧,最近仔细研究了一下这些东西,发现也就那样,或许是我之前根本就没怎么花心思学。。

    话不多说,下面就直接上代码吧,也没什么好解释的,只要我自己理解代码就行了,哈哈哈。。。

    代码参考《C和C++程序员面试秘笈》一书

    // Tree.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <stack>
    #include <queue>
    #include <iostream>
    using namespace std;
    
    class TreeNode
    {
    public:
        int data;
        TreeNode *parent;
        TreeNode *left;
        TreeNode *right;
    
        TreeNode(int num):data(num),parent(NULL),left(NULL),right(NULL){};
    };
    
    TreeNode *root;
    
    //将data插入到排序二叉树中
    //首先需要找到需要插入数据的父节点,
    //然后再判断是插在父节点左边还是右边
    void insert_TreeNode(int data)
    {
        TreeNode *p1,*p2,*temp;
        p1=new TreeNode(data);
        p2=root;
        while(p2)
        {
            temp=p2;
            if (data<p2->data)
                p2=p2->left;
            else if(data>p2->data)
                p2=p2->right;
            else
            {
                cout<<"要插入的数据"<<data<<"已经存在于二叉树中,不能重复插入"<<endl;
                return;
            }
        }
        p1->parent=temp;
        if (data<temp->data)
        {
            temp->left=p1;
        }
        else
            temp->right=p1;
    }
    
    //创建二叉树
    //采取数组元素填充的方式创建
    //函数调用insert_TreeNode()函数不断插入节点创建二叉树
    void creat(int *num,int len)
    {
        root=new TreeNode(num[0]);
        for (int i=1;i<len;i++)
        {
            insert_TreeNode(num[i]);
        }
    }
    
    //递归遍历二叉树
    void recursive_pre_show_TreeNode(TreeNode *current)
    {
        if (current)
        {
            cout<<current->data<<"  ";
            recursive_pre_show_TreeNode(current->left);
            recursive_pre_show_TreeNode(current->right);
        }
    }
    
    void recursive_mid_show_TreeNode(TreeNode *current)
    {
        if (current)
        {
            recursive_mid_show_TreeNode(current->left);
            cout<<current->data<<"  ";
            recursive_mid_show_TreeNode(current->right);
        }
    }
    
    void recursive_end_show_TreeNode(TreeNode *current)
    {
        if (current)
        {
            recursive_end_show_TreeNode(current->left);
            recursive_end_show_TreeNode(current->right);
            cout<<current->data<<"  ";
        }
    }
    
    void recursive_show_TreeNode()
    {
        if (root==NULL)
        {
            cout<<"二叉树为空"<<endl;
            return ;
        }
        TreeNode *current=root;
    
        cout<<"递归前序遍历。。。"<<endl;
        recursive_pre_show_TreeNode(current);
        cout<<endl<<"递归中序遍历。。。"<<endl;
        recursive_mid_show_TreeNode(current);
        cout<<endl<<"递归后序遍历。。。"<<endl;
        recursive_end_show_TreeNode(current);
        cout<<endl;
    }
    
    
    
    
    //非递归遍历二叉树
    //前序
    //借助栈  
    void pre_show_TreeNode(TreeNode *root)
    {
        if (root==NULL)
        {
            cout<<"二叉树为空"<<endl;
            return;
        }
        TreeNode *p=root;
        stack<TreeNode *> stack;
        cout<<"非递归前序遍历。。。"<<endl;
        while(p!=NULL||!stack.empty())
        {
            while(p)
            {
                cout<<p->data<<"  ";
                stack.push(p);
                p=p->left;
            }
            if (!stack.empty())
            {
                p=stack.top();
                stack.pop();
                p=p->right;
            }
        }
        cout<<endl;
    }
    
    //中序
    void mid_show_TreeNode(TreeNode *root)
    {
        if (root==NULL)
        {
            cout<<"二叉树为空"<<endl;
            return;
        }
        stack<TreeNode *> stack;
        TreeNode *p=root;
        cout<<"非递归中序遍历。。。"<<endl;
        while(p!=NULL||!stack.empty())
        {
            while(p)
            {
                stack.push(p);
                p=p->left;
            }
            if (!stack.empty())
            {
                p=stack.top();
                stack.pop();
                cout<<p->data<<"  ";
                p=p->right;
            }
        }
        cout<<endl;
    }
    
    //删除二叉树节点
    //删除节点p的时候需要删除其左右子树
    //从最底层的左右节点开始一个一个删除
    void delete_TreeNode(TreeNode *p)
    {
        if (p->left)
        {
            delete_TreeNode(p->left);
        }
        if (p->right)
        {
            delete_TreeNode(p->right);
        }
    
        if (p->parent==NULL)    //根节点
        {
            p->left=NULL;
            p->right=NULL;
            ::root=NULL;
            delete p;
            p=NULL;
            return;
        }
    
        //判断是要删除左边的还是右边的子节点
        if (p->parent->data>p->data)
        {
            p->parent->left=NULL;
        }
        else
            p->parent->right=NULL;
        delete p;
        p=NULL;
    }
    
    //删除二叉树节点
    //首先需要找到该节点
    //然后调用delete_TreeNode(TreeNode *p)函数删除找到的节点及其子节点
    void delete_TreeNode(TreeNode *root,int data)
    {
        if (root==NULL)
        {
            cout<<"二叉树为空"<<endl;
            return;
        }
        TreeNode *p=root;
        while(p)
        {
            if (data<p->data)
            {
                p=p->left;
            }
            else if (data>p->data)
            {
                p=p->right;
            }
            else
            {
                delete_TreeNode(p);
                return;
            }
        }
    }
    
    //层次遍历二叉树
    //首先根节点入队
    //然后根节点出队  根节点的左右节点若不为空也入队
    //父节点出队  子节点入队
    void level_show_TreeNode(TreeNode *root)
    {
        if (root==NULL)
        {
            cout<<"二叉树为空"<<endl;
            return;
        }
        TreeNode *p=root;
        queue<TreeNode *> queue;
        queue.push(root);
        while(!queue.empty())
        {
            p=queue.front();
            queue.pop();
            cout<<p->data<<"  ";
            if (p->left!=NULL)
            {
                queue.push(p->left);
            }
            if (p->right!=NULL)
            {
                queue.push(p->right);
            }
        }
        cout<<endl;
    }
    
    //判断给定的二叉树是否为排序二叉树
    //使用中序遍历二叉树
    //使用lastvalue记录上一次遍历时节点的值
    //lastvalue初始值为0 
    bool Is_SortTree(TreeNode *root)
    {
        if (root==NULL)
        {
            cout<<"二叉树为空"<<endl;
            return false;
        }
        stack<TreeNode *> stack;
        TreeNode *p=root;
        int lastvalue=0;
        while(p!=NULL||!stack.empty())
        {
            while(p)
            {
                stack.push(p);
                p=p->left;
            }
            if (!stack.empty())
            {
                p=stack.top();
                stack.pop();
                if (lastvalue==0||lastvalue<p->data)  //lastvalue==0表示第一次出栈
                {
                    lastvalue=p->data;
                }
                else if (lastvalue>p->data)
                {
                    return false;
                }
                p=p->right;
            }
        }
        return true;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int num[8]={5,3,7,2,4,6,8,1};
        creat(num,8);
        recursive_show_TreeNode();
        pre_show_TreeNode(root);
        mid_show_TreeNode(root);
    
        cout<<endl<<"删除节点后。。。"<<endl;
        delete_TreeNode(root,1);
        mid_show_TreeNode(root);
    
        cout<<"层次遍历。。。"<<endl;
        level_show_TreeNode(root);
    
        bool a=Is_SortTree(root);
        if (a)
        {
            cout<<"是排序树"<<endl;
        }
        else
            cout<<"不是排序树"<<endl;
        return 0;
    }

     

  • 相关阅读:
    集合合并
    非递减有序集合合并
    有序的双链表的实现
    单链表的实现
    构造有序的单链表
    约瑟夫环问题
    javaweb学习笔记
    Intellij IDEA快捷键
    JAVA:创建类和对象
    JAVA:成员变量和局部变量的区别
  • 原文地址:https://www.cnblogs.com/audi-car/p/4572704.html
Copyright © 2011-2022 走看看