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

  • 相关阅读:
    poj 3528 (三维几何求凸包+凸包表面积)
    dijkstra模板(好像是斐波那契额堆优化,但我为什么看起来像优先队列优化,和spfa一样)
    最大空凸包模板
    ICPC 2017–2018, NEERC, Northern Subregional Contest St Petersburg, November 4, 2017 I题
    hdu 5248 序列变换
    hdu 2063(二分图模板测试)
    组合数
    85. Maximal Rectangle 由1拼出的最大矩形
    750. Number Of Corner Rectangles四周是点的矩形个数
    801. Minimum Swaps To Make Sequences Increasing 为使两个数组严格递增,所需要的最小交换次数
  • 原文地址:https://www.cnblogs.com/NoMasp/p/4495382.html
Copyright © 2011-2022 走看看