数据结构——(计算二叉树的结点个数、计算二叉树的高度(深度)、查找值为k的结点、查找值为k的父结点)

1.计算二叉树的结点个数

思想:以root为根的树的结点个数=左子树的结点个数+右子树的结点个数+1。

//计算结点个数
int Size(Node* root)
{
    if (root == NULL)
        return 0;
    else
        return Size(root->left) + Size(root->right) + 1;
}

2.计算二叉树的高度(深度)

思想:
①计算出左子树的高度HL;
②计算出右子树的高度HR;
③取出HL和HR中较大的,然后再加上1,即是当前树的高度。

以root为根的二叉树的高度=左子树的高度HL与右子树的高度HR(比较,选择较大的高度+1)

//计算二叉树的高度(深度)
int Height(Node* root)
{
    if (root == NULL)
        return 0;
    else
    {
        int HL = Height(root->left);
        int HR = Height(root->right);
        return HL > HR ? (HL + 1) : (HR + 1);
    }
}

3.查找值为k的结点

思想:查找以root为根的二叉树中值为k的结点,如果找到了,则返回指向该结点的指针,没有找到,则返回NULL。

①如果根为NULL,则返回NULL;
②找到了,返回此指针;
③在左子树查找;
④左子树找到,则返回;
⑤在右子树查找。

//查找值为k的结点
Node* Find(Node* root, char k)
{
    Node* p=NULL;
    if (root == NULL)
        return NULL;
    if (root->value == k)//说明找到了
        return root;
    p = Find(root->left, k);
    if (p != NULL)
        return p;
    return Find(root->right, k);//在右边查找
}

4.查找值为k的父结点

//查找以root为根的二叉树中值为k的父节点
Node* FindParent(Node* root, char k)
{
    Node* p = NULL;
    if (root == NULL)
        return NULL;
    if (root->left != NULL && root->left->value == k||
        root->right!=NULL&&root->right->value==k);//孩子值为k
        return root;

    p = FindParent(root->left, k);//如果左边找到了,返回p
    if (p)
        return p;
    return FindParent(root->right, k);
}

5.调试代码(完整)

#include<stdio.h>
#include<iostream>
using namespace std;

//结点的结构
typedef struct node
{
    char value;//当前结点本身的值
    struct node* left;//当前节点指向左子树的指针
    struct node* right;//当前节点指向右子树的指针
}Node;

//树的结构
typedef struct tree
{
    Node* root;//指向树的根结点的指针
}Tree;

void InitTree(Tree* t);//初始化树,其实就是将树的root初始化为NULL
Node* Create(char*& str);//创建二叉树,创建成功后,将根节点返回
int Size(Node* root);//计算以root为根的二叉树的结点个数
int Height(Node* root);//计算以root为根的二叉树的高度(深度)
Node* Find(Node* root, char k);//在以root为根的二叉树中查找值为k的结点
Node* FindParent(Node* root, char k);//在以root为根的二叉树查找值为k的父结点

void InitTree(Tree* t)
{
    t->root = NULL;
}

//根据先序遍历创建二叉树,先创建根,在创建根的左子树,最后 创建根的右子树
Node* Create(char*& str)
{
    if (*str == '*')
        return NULL;
    else
    {
        Node* newnode = (Node*)malloc(sizeof(Node));//先开辟一个结点空间
        newnode->value = *str;//将当前不是*的字符赋给当前节点,创建根
        newnode->left = Create(++str);//创建当前结点的左子树,调用创建函数
        newnode->right = Create(++str);//创建当前结点的右子树
        return newnode;

    }
}

//计算root为根的树的结点个数
int Size(Node* root)
{
    if (root == NULL)
        return 0;
    else
        return Size(root->left) + Size(root->right) + 1;
}

//计算以root为根的二叉树的高度
int Height(Node* root)
{
    if (root == NULL)
        return 0;
    else
    {
        int HL = Height(root->left);
        int HR = Height(root->right);
        return HL > HR ? (HL + 1) : (HR + 1);
    }
}

//查找以root为根的二叉树中值为k的结点
Node* Find(Node* root, char k)
{
    Node* p=NULL;
    if (root == NULL)
        return NULL;
    if (root->value == k)//说明找到了
        return root;
    p = Find(root->left, k);
    if (p != NULL)
        return p;
    return Find(root->right, k);//在右边查找
}

//查找以root为根的二叉树中值为k的父结点
Node* FindParent(Node* root, char k)
{
    Node* p = NULL;
    if (root == NULL)
        return NULL;
    if (root->left != NULL && root->left->value == k||
        root->right!=NULL&&root->right->value==k);//孩子值为k
        return root;

    p = FindParent(root->left, k);//如果左边找到了,返回p
    if (p)
        return p;
    return FindParent(root->right, k);
}

int main()
{
    Tree t;
    InitTree(&t);//空树
    char* str =(char*) "ABDG**HI****CE*J**F**";//变量强转
    t.root = Create(str);

    printf("size=%d\n",Size(t.root));

    printf("height=%d\n",Height(t.root ));

    //查找值为k的节点
    printf("--------find-------\n");
    Node* p = Find(t.root, 'G');
    if (p != NULL)
    {
        printf("找到了,p=%c\n", p->value);
    }
    else
    {
        printf("没找到\n");
    }

    //查找值为k的父节点
    printf("-------find parent-------\n");
    p = FindParent(t.root, 'B');
    if (p)
    {
        printf("找到了,父节点为%c\n", p->value);
    }
    else
    {
        printf("没找到\n");
    }
    return 0;
}


  • 8
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sweep-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值