mignatian的博客

靡不有初鲜克有终

数据结构之二叉树常见面试题

按先序遍历创建一棵树

treenode *_treecreat(treenodetype data[],size_t size,size_t *index,treenodetype null_node)
{
    if(index == NULL)
    {
        return NULL;//非法输入
    }
    if((*index) >= size)
    {
        return NULL;//遍历完了
    }
    if(data[*index] == null_node)
    {
        return NULL;//返回空树
    }
    treenode *newnode = treenode_creatnode(data[*index]);
    ++(*index);
    newnode->lchild = _treecreat(data,size,index,null_node);
    ++(*index);
    newnode->rchild = _treecreat(data,size,index,null_node);
    return newnode; 

}
treenode *treecreat(treenodetype data[],size_t size,treenodetype null_node)
{
    size_t index = 0;
    treenode *newnode = _treecreat(data,size,&index,null_node);
    return newnode;
}

克隆一棵树

treenode *treenode_clone(treenode *root)
{
    if(root == NULL)
    {
        return NULL;
    }
    treenode *newnode = treenode_creatnode(root->data);
    newnode->lchild = treenode_clone(root->lchild);
    newnode->rchild = treenode_clone(root->rchild);
    return newnode;

}

求一棵树的叶子节点

size_t treeleafsize(treenode *root) 
{
    if(root == NULL)
    {
        return 0;
    }
    if(root->lchild == NULL && root->rchild == NULL)
    {
        return 1;
    }
    return treeleafsize(root->lchild) + treeleafsize(root->rchild);
}

求第k层节点个数

size_t treeKlevelsize(treenode *root,int k)
{
    if(root == NULL)
    {
        return 0;
    }
    if(k == 1)
    {
        return 1;
    }
    return treeKlevelsize(root->lchild,k-1) + treeKlevelsize(root->rchild,k-1);
}

求树的深度

size_t treehight(treenode *root)
{
    if(root == NULL)
    {
        return 0;
    }
    size_t rhight = treehight(root->rchild);
    size_t lhight = treehight(root->lchild);
    return 1+(rhight > lhight ? rhight : lhight);
}

查找一个节点的位置

treenode *treefind(treenode *root,treenodetype value)
{
    if(root == NULL)
    {
        return;
    }
    seqqueue queue;
    seqqueuetype front;
    seqqueue_init(&queue);
    seqqueue_push(&queue,front);
    while(1)
    {
        seqqueue_gettop(&queue,&front);
        if(front->data = value)
        {
            return front;
        }
        if(front->lchild != NULL)
        {
            seqqueue_push(&queue,front->lchild);
        }
        if(front->rchild != NULL)
        {
            seqqueue_push(&queue,front->rchild);
        }
        seqqueue_pop(&queue);
    }
}

先序遍历(非递归)

void PreOrderByLoop(treenode *root)
{
    if(root == NULL)
    {
        return;
    }
    seqstack s;
    treenode *top; 
    treenode *cur = root;
    seqstack_init(&s);
    while(s.size != 0 || cur != NULL)
    {
        while(cur != NULL)
        {
            printf("%c ",cur->data);
            seqstack_pushback(&s,cur);
            cur = cur->lchild;
        }
        seqstack_gettop(&s,&top);
        seqstack_popback(&s);
        cur = top->rchild;
    }
    printf("\n");
}

中序遍历(非递归)

void InOrderByLoop(treenode *root)
{
    if(root == NULL)
    {
        return;
    }
    seqstack s;
    treenode *cur = root;
    treenode *top;
    seqstack_init(&s);    
    while(s.size != 0 || cur != NULL)
    {
        while(cur != NULL)
        {
            seqstack_pushback(&s,cur);
            cur = cur->lchild;
        }
        seqstack_gettop(&s,&top);
        printf("%c ",top->data);
        seqstack_popback(&s);
        cur = top->rchild;
    }
}

后序遍历(非递归)

void PostOrderByLoop(treenode *root)
{
    if(root == NULL)
    {
        return;
    }
    seqstack s;
    treenode *cur = root;
    treenode *top;
    treenode *prev = NULL;
    seqstack_init(&s);
    while(s.size != 0 || cur != NULL)
    {
        while(cur != NULL)
        {
            seqstack_pushback(&s,cur);
            cur = cur->lchild;
        }
        seqstack_gettop(&s,&top);
        if(top->rchild == NULL || top->rchild == prev)
        {
            printf("%c ",top->data);
            prev = top;
            seqstack_popback(&s);
        }
        else
        {
            cur = top->rchild;
        }
    }
}

二叉树的镜像

void swap(treenode **a,treenode **b)
{
    treenode *t = *a;
    *a = *b;
    *b = t;
}
void TreeMirror(treenode *root)
{
    if(root == NULL)
    {
        return;
    }
    swap(&root->lchild,&root->rchild);
    if(root->lchild != NULL)
    {
        TreeMirror(root->lchild);
    }
    if(root->rchild != NULL)
    {
        TreeMirror(root->rchild);
    }
}
阅读更多
个人分类: 数据结构
想对作者说点什么? 我来说一句

数据结构-二叉树及存储结构

2017年04月09日 470KB 下载

没有更多推荐了,返回首页

不良信息举报

数据结构之二叉树常见面试题

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭