加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

【数据结构】回顾二叉树

发布时间:2020-12-15 06:01:29 所属栏目:安全 来源:网络整理
导读:1.为什么会有树?因为当有大量的输入数据时,链表的线性访问时间就显得略长了。而树结构,其大部分操作的运行时间平均为O(logN)。 2.树的实现并不难,几行代码就搞定了。 struct TreeNode{ Object element; TreeNode *firstChild; TreeNode *nextSibling;} 3

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,ptr->left_child);
            if(ptr->right_child)
                addq(front,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 fulln");
            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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读