二叉树基本操作

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

#define MAX(x,y) ((x)>(y)?(x):(y))

typedef int DataType;

typedef struct Node{
    DataType _data;
    struct Node* _left;
    struct Node* _right;
}Node;

typedef struct Stack{
    Node* node;
    int size;
    int capacity;
}Stack;

Node* BuyNode(DataType data)
{
    Node* node = (Node*)malloc(sizeof(Node));
    node->_data = data;
    node->_right = NULL;
    node->_left = NULL;
    return node;
}

void BinarayTreeCreate(Node** _pRoot, DataType* array, int size, int* pos, DataType invaled)
{
    if(array == NULL)
        return;
    if(*pos >= size || array[*pos] == invaled)
        return;
    *_pRoot = BuyNode(array[*pos]);
    (*pos)++;
    BinarayTreeCreate((&((*_pRoot))->_left), array, size, pos, invaled);
    (*pos)++;
    BinarayTreeCreate((&(*_pRoot)->_right), array, size, pos, invaled);
}


void display_in_order(Node* _pRoot)
{
    if(_pRoot == NULL)
     {
        printf("NULL ");
         return;
     }
    printf("%d ",_pRoot->_data);
    display_in_order(_pRoot->_left);
    display_in_order(_pRoot->_right);
}

void display_in_back(Node* _pRoot)
{
    if(_pRoot == NULL)
        {
            printf("NULL ");
            return;
        }
    display_in_back(_pRoot->_left);
    display_in_back(_pRoot->_right);
    printf("%d ", _pRoot->_data);
}

void display_in_middle(Node* _pRoot)
{
    if(_pRoot == NULL)
     {
        printf("NULL ");
         return;
     }
    display_in_order(_pRoot->_left);
    printf("%d ",_pRoot->_data);
    display_in_order(_pRoot->_right);
}

int number_of_node(Node* _pRoot)
{
    if(_pRoot == NULL)
        return 0;
    return 1 + number_of_node(_pRoot->_left) + number_of_node(_pRoot->_right);
}

int number_of_levef(Node* _pRoot)
{
    if(_pRoot == NULL)
        return 0;
    if(_pRoot->_left != NULL || _pRoot->_right != NULL)
        return number_of_levef(_pRoot->_left) + number_of_levef(_pRoot->_right);
    return 1;
}

int hight_of_tree(Node* _pRoot)
{
    if(_pRoot == NULL)
        return 0;
    if(_pRoot->_left == NULL && _pRoot->_right == NULL)
        return 1;
    return MAX(hight_of_tree(_pRoot->_left),hight_of_tree(_pRoot->_right)) + 1;
}

int _number_of_node_in_k(Node* _pRoot, int k, int* layer)
{
    if(_pRoot == NULL)
        return 0;
    if((*layer) != k)
    {
        printf("now layer is %d\n", *layer);
        (*layer)++ ;
        return _number_of_node_in_k(_pRoot->_left, k, layer) + _number_of_node_in_k(_pRoot->_right, k, layer);
    }
    return 1;
}

int number_of_node_in_k(Node* _pRoot, int k, int hight)
{
    if(_pRoot == NULL || k > hight)
        return 0;
    if(k < 0)
        return 0;
    if(k == 1)
        return 1;
    int layer = 1;
    return _number_of_node_in_k(_pRoot, k, &layer);
}

int GetKlevef(Node* _pRoot, int k)
{
    if(_pRoot == NULL)
        return 0;
    if(k == 1)
        return 1;
    return GetKlevef(_pRoot->_left, k - 1) + GetKlevef(_pRoot->_right, k -1);
}

Node* FindNode(Node* _pRoot, DataType data)
{
    if(_pRoot == NULL)
        return NULL;
    if(_pRoot->_data == data)
        return _pRoot;
    Node* node = FindNode(_pRoot->_left, data);
    if(node != NULL)
        return node;
    else
        node = FindNode(_pRoot->_right, data);
    return node;
}

bool IsCompleteTree(Node* _pRoot)
{
    if(_pRoot == NULL)
        return true;
    if(_pRoot->_right != NULL && _pRoot->_left == NULL)
        return false;
    return IsCompleteTree(_pRoot->_left) && IsCompleteTree(_pRoot->_right);
}

int main()
{
    int array[] = {1, 2, 4, -1, -1, -1, 3, 5, 6};
    Node* root;
    int pos = 0;
    BinarayTreeCreate(&root, array, sizeof(array)/sizeof(array[0]), &pos, -1);
    printf("display_in_order : ");
    display_in_order(root);
    printf("\n");
    printf("display_in_middle : ");
    display_in_middle(root);
    printf("\n");
    printf("display_in_back : ");
    display_in_back(root);
    printf("\n");
    printf("number of node is %d\n", number_of_node(root));
    printf("number of levef is %d\n", number_of_levef(root));
    printf("hight of tree is %d\n", hight_of_tree(root));
    printf("k layer is %d\n", GetKlevef(root, 3));
    printf("%d\n", FindNode(root, 2)->_data);
    if(IsCompleteTree(root) == true)
        printf("this is complete tree!\n");
    else
        printf("this is not complete tree!\n");
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值