zoukankan      html  css  js  c++  java
  • 二叉树的8种操作

    1.为什么会有树?因为当有大量的输入数据时,链表的线性访问时间就显得略长了。而树结构,其大部分操作的运行时间平均为O(logN)。

    2.树的实现并不难,几行代码就搞定了。

    struct TreeNode
    {
        Object element;
        TreeNode *firstChild;
        TreeNode *nextSibling;
    }

    3.遍历形式:

    // 中序遍历二叉树
    void inorder(tree_pointer ptr)
    {
        if(ptr)
        {
            inorder(ptr->left_child);
            printf("%d",ptr->data);
            inorder(ptr->right_child);
        }
    }
    
    // 前序遍历二叉树
    void preorder(tree_pointer ptr)
    {
        if(ptr)
        {
            printf("%d",ptr->data);
            preorder(ptr->left_child);
            preorder(ptr->right_child);
        }
    }
    
    // 后序遍历二叉树
    void postorder(tree_pointer ptr)
    {
        if(ptr)
        {
            postorder(ptr->left_child);
            postorder(ptr->right_child);
            printf("%d",ptr->data);
        }
    }

    4.迭代的中序遍历

    void iter_inorder(tree_pointer node)
    {
        int top=-1;
        tree_pointer stack[MAX_STACK_SIZE];
        for(;;)
        {
            for(;node;node=node->left_child)
                add(&top,node);
            node=delete(&top);
            if(!node)
                break;
            printf("%d",node->data);
            node=node->right_child;
        }
    }

    5.二叉树的层序遍历

    void level_order(tree_pointer ptr)
    {
        int front=rear=0;
        tree_pointer queue[MAX_QUEUE_SIZE];
        if(!ptr)
            return;
        addq(front,&rear,ptr);
        for(;;)
        {
            ptr=deleteq(&front,rear);
            if(ptr)
            {
                printf("%d",ptr->data);
                if(ptr->left_child)
                    addq(front,&rear,ptr->left_child);
                if(ptr->right_child)
                    addq(front,&rear,ptr->right_child);
            }
            else
                break;
        }
    }

    6.二叉树的复制

    tree_pointer copy(tree_pointer original)
    {
        tree_pointer temp;
        if(original)
        {
            temp=(tree_pointer) malloc(sizeof(node));
            if(IS_FULL(temp))
            {
                fprintf(stderr,"The memory is full
    ");
                exit(1);
            }
            temp->left_child=copy(original->left_child);
            tmep->right_child=copy(original->right_child);
            temp->data=original->data;
            return temp;
        }
        return NULL;
    }

    7.判断二叉树的等价性

    int equal(tree_pointer first,tree_pointer second)
    {
        return ((!first&&!second)||(first && second && (first->data == second->data) && 
                equal(first->left_child,second->left_child) &&
                equal(first->right_child,second->right_child));
    }

    8.寻找结点的中序后继

    threaded_pointer insucc(threaded_pointer tree)
    {
        threaded_pointer temp;
        temp=tree->right_child;
        if(!tree->right_thread)
            while(!temp->left_thread)
                temp=temp->left_child;
        return temp;
    }

    9.线索二叉树的中序遍历

    void tinorder(threaded_pointer tree)
    {
        threaded_pointer temp=tree;
        for(;;)
        {
            temp=insucc(temp);
            if(temp==tree)
                break;
            printf("%3c",temp->data);
        }
    }

    10.线索二叉树的右插入操作

    void insert_right(threaded_pointer parent,threaded_pointer child)
    {
         threaded_pointer temp;
         child->right_child=parent->right_child;
         child->right_thread=parent->right_thread;
         child->left_child=parent;
         child->left_thread=TRUE;
         parent->right_child=child;
         parent->right_thread=FALSE;
         if(!child->right_thread)
         {
             temp=insucc(child);
             temp->left_child=child;
         }
    }


    欢迎大家点击左上角的“关注”或右上角的“收藏”方便以后阅读。


    为使本文得到斧正和提问,转载请注明出处:
    http://blog.csdn.net/nomasp

  • 相关阅读:
    质数检测器
    《视觉SLAM十四讲》学习日志(一)——预备知识
    C++类的介绍
    Python数据类型解析(基础篇)
    括号匹配详解
    哈夫曼树编码
    分治之归并,快速排序
    洛谷p2216 多次单调队列,扫描矩阵中的最大值减去最小值最的固定大小子矩阵
    洛谷p1886滑动窗口最大最小值 双单调队列
    洛谷p1725 露琪诺 单调队列优化的DP
  • 原文地址:https://www.cnblogs.com/NoMasp/p/4495382.html
Copyright © 2011-2022 走看看