数据结构篇 --- 树

由一个根节点及多个节点组成的有层次关系的集合,结构像一颗树。

特点:每个节点有0个或者多个子节点,没有父节点的节点为根节点,非根节点有且只有一个父节点。

二叉树:每个节点有最多两个子节点。

满二叉树:除叶子节点外其他的节点都含有两个子节点。

完全二叉树:有 个节点的满二叉树称为完全二叉树。

typedef struct tree
{
    int data;
    struct root *lchild;    
    struct root *rchild;  
}TREE;

TREE* Insert_tree(TREE * tree, int key)
{
    TREE *tmp_tmp_tree = NULL;

    if (tree == NULL) {
         tmp_tree = (TREE*)malloc(sizeof(TREE));
         tmp_tree->data = key;
         tmp_tree->lchild = NULL;
         tmp_tree->lchild = NULL;
        
         return tmp_tree;
    }

     if (key < tree->data) {
         tmp_tree = Insert_tree(tree->lchild, key);
     } else {
         tmp_tree = Insert_tree(tree->rchild, key);
     }

      return tmp_tree;
}

 

posted @ 2020-12-07 14:49  _Show  阅读(58)  评论(0)    收藏  举报