一对多

节点:集合 节点的数量=边数+1

边:关系,集合与集合的关系。可以通过指针域完成。

根:全集,子集取并集

子节点:子集

树的深度、高度:最长路径节点的个数。

节点的深度:从上往下数节点。向下看

节点的高度:从下往上数节点。向上看

节点的度:边向外指向的数量。

二叉树

任何N叉树都可转成二叉树(左孩子,右兄弟,十字链表)。从非确定性问题道确定性问题的转变。

  • 每一节点度最多为2。
  • 度为0的节点比度为2的节点多一个。节点的数量=边数+1

二叉树的遍历(递归)

  • 前序遍历:根 左 右
  • 中序遍历:左 根 右(二叉排序树)
  • 后序遍历:左 右 根

完全二叉树

  1. 编号为i的子节点:

左边号:2 * i

右编号:2 * i + 1

  1. 可以用连续空间储存(数组)

结构定义

typedef struct Node {
    int data;
    struct Node *lchild, *rchild;
} Node;

typedef struct Tree {
    Node *root;
    int length;
} Tree;

初始化

Node *getNewNode(int val) {
    Node *p = (Node *)malloc(sizeof(Node));
    p->data = val;
    p->lchild = p->rchild = NULL;
    return p;
}

Tree *getNewTree() {
    Tree *tree = (Tree *)malloc(sizeof(Tree));
    tree->root = NULL;
    tree->length = 0;
    return tree;
}

插入

Node *insert_node(Node *root, int val, int *flag) {
    if (root == NULL) {
        *flag = 1;
        return getNewNode(val);
    }
    if (root->data == val) return root;
    if (root->data > val) root->lchild = insert_node(root->lchild, val, flag);
    else root->rchild = insert_node(root->rchild, val, flag);
    return root;
}

void insert(Tree *tree, int val) {
    if (tree == NULL) return ;
    int flag = 0;
    tree->root = insert_node(tree->root, val, &flag);
    tree->length += flag;
    return ;
}

清除

void clear_node(Node *root){
    if (root = NULL) return;
    clear_node(root->lchild);
    clear_node(root->rchild);
    free(root);
    return ;
}

void clear(Tree *tree) {
    if (tree == NULL) return ;
    clear_node(tree->root);
    free(tree);
    return ;
}

插入

Node *insert_node(Node *root, int val, int *flag) {
    if (root == NULL) {
        *flag = 1;
        return getNewNode(val);
    }
    if (root->data == val) return root;
    if (root->data > val) root->lchild = insert_node(root->lchild, val, flag);
    else root->rchild = insert_node(root->rchild, val, flag);
    return root;
}

void insert(Tree *tree, int val) {
    if (tree == NULL) return ;
    int flag = 0;
    tree->root = insert_node(tree->root, val, &flag);
    tree->length += flag;
    return ;
}

遍历

void pre_order_node(Node *root) {
    if (root == NULL ) return ;
    printf(" %d", root->data);
    pre_order_node(root->lchild);
    pre_order_node(root->rchild);
    return ;
}

void pre_order(Tree *tree) {
    if (tree == NULL) return ;
    printf("pre_order : ");
    pre_order_node(tree->root);
    printf("\n");
    return ;
}

void in_order_node(Node *root) {
    if (root == NULL ) return ;
    in_order_node(root->lchild);
    printf(" %d", root->data);
    in_order_node(root->rchild);
    return ;
}
void in_order(Tree *tree) {
    if (tree == NULL) return ;
    printf("in_order : ");
    in_order_node(tree->root);
    printf("\n");
    return ;
}

void post_order_node(Node *root) {
    if (root == NULL ) return ;
    post_order_node(root->lchild);
    post_order_node(root->rchild);
    printf(" %d", root->data);
    return ;
}

void post_order(Tree *tree) {
    if (tree == NULL) return ;
    printf("post_order : ");
    post_order_node(tree->root);
    printf("\n");
    return ;
}

输出

void output_node(Node *root) {
    if (root == NULL) return ;
    printf(" %d", root->data);
    if (root->lchild == NULL && root->rchild == NULL) return ;
    printf("(");
    output_node(root->lchild);
    printf(",");
    output_node(root->rchild);
    printf(")");
    return ;
}

void output(Tree *tree) {
    if (tree == NULL) return ;
    printf("tree (%d): ", tree->length);
    output_node(tree->root);
    printf("\n");
    return ;
}

完整代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

typedef struct Node {
    int data;
    struct Node *lchild, *rchild;
} Node;

typedef struct Tree {
    Node *root;
    int length;
} Tree;

Node *getNewNode(int val) {
    Node *p = (Node *)malloc(sizeof(Node));
    p->data = val;
    p->lchild = p->rchild = NULL;
    return p;
}

Tree *getNewTree() {
    Tree *tree = (Tree *)malloc(sizeof(Tree));
    tree->root = NULL;
    tree->length = 0;
    return tree;
}

Node *insert_node(Node *root, int val, int *flag) {
    if (root == NULL) {
        *flag = 1;
        return getNewNode(val);
    }
    if (root->data == val) return root;
    if (root->data > val) root->lchild = insert_node(root->lchild, val, flag);
    else root->rchild = insert_node(root->rchild, val, flag);
    return root;
}

void insert(Tree *tree, int val) {
    if (tree == NULL) return ;
    int flag = 0;
    tree->root = insert_node(tree->root, val, &flag);
    tree->length += flag;
    return ;
}

void clear_node(Node *root){
    if (root = NULL) return;
    clear_node(root->lchild);
    clear_node(root->rchild);
    free(root);
    return ;
}

void clear(Tree *tree) {
    if (tree == NULL) return ;
    clear_node(tree->root);
    free(tree);
    return ;
}

void pre_order_node(Node *root) {
    if (root == NULL ) return ;
    printf(" %d", root->data);
    pre_order_node(root->lchild);
    pre_order_node(root->rchild);
    return ;
}

void pre_order(Tree *tree) {
    if (tree == NULL) return ;
    printf("pre_order : ");
    pre_order_node(tree->root);
    printf("\n");
    return ;
}

void in_order_node(Node *root) {
    if (root == NULL ) return ;
    in_order_node(root->lchild);
    printf(" %d", root->data);
    in_order_node(root->rchild);
    return ;
}
void in_order(Tree *tree) {
    if (tree == NULL) return ;
    printf("in_order : ");
    in_order_node(tree->root);
    printf("\n");
    return ;
}

void post_order_node(Node *root) {
    if (root == NULL ) return ;
    post_order_node(root->lchild);
    post_order_node(root->rchild);
    printf(" %d", root->data);
    return ;
}
void post_order(Tree *tree) {
    if (tree == NULL) return ;
    printf("post_order : ");
    post_order_node(tree->root);
    printf("\n");
    return ;
}


void output_node(Node *root) {
    if (root == NULL) return ;
    printf(" %d", root->data);
    if (root->lchild == NULL && root->rchild == NULL) return ;
    printf("(");
    output_node(root->lchild);
    printf(",");
    output_node(root->rchild);
    printf(")");
    return ;
}

void output(Tree *tree) {
    if (tree == NULL) return ;
    printf("tree (%d): ", tree->length);
    output_node(tree->root);
    printf("\n");
    return ;
}




int main() {
    srand(time(0));
    Tree *tree = getNewTree();
    #define MAX_OP 20
    for (int i = 0; i < MAX_OP; i++) {
        int val = rand() % 100;
        insert(tree, val);
        output(tree);
    }
    pre_order(tree);
    in_order(tree);
    post_order(tree);
    clear(tree);
    #undef MAX_OP
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值