树三:创建二叉树

指路法定位结点:

  

  • 通过根结点与目标结点的相对位置进行定位
  • 指路法可以避开二叉树递归的性质“线性”定位
  • 在C语言中可以用 bit 位来进行指路:

    #define BT_LEFT 0

    #define BT_RIGHT 1

    typedef unsigned long long BTPos;

二叉树的存储结构:

/* 结点指针域定义 */
typedef struct _tag_BTreeNode BTreeNode;
struct _tag_BTreeNode {
    BTreeNode* left;
    BTreeNode* right;
};
/* 头结点定义 */
typedef struct _tag_BTree BTree;
struct _tag_BTree {
    int count;
    BTreeNode* root;
};
/* 数据元素定义示例 */
struct Node {
    BTreeNode header;
    char v;
};

定位操作:

/* mani.c */
#include <stdio.h>
#include <stdlib.h>
#include "BTree.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

struct Node
{
    BTreeNode header;
    char v;
};

void printf_data(BTreeNode* node)
{
    if( node != NULL )
    {
        printf("%c", ((struct Node*)node)->v);
    }
}

int main(int argc, char *argv[])
{
    BTree* tree = BTree_Create();

    struct Node n1 = {{NULL, NULL}, ‘A‘};
    struct Node n2 = {{NULL, NULL}, ‘B‘};
    struct Node n3 = {{NULL, NULL}, ‘C‘};
    struct Node n4 = {{NULL, NULL}, ‘D‘};
    struct Node n5 = {{NULL, NULL}, ‘E‘};
    struct Node n6 = {{NULL, NULL}, ‘F‘};

    BTree_Insert(tree, (BTreeNode*)&n1, 0, 0, 0);
    BTree_Insert(tree, (BTreeNode*)&n2, 0x00, 1, 0);
    BTree_Insert(tree, (BTreeNode*)&n3, 0x01, 1, 0);
    BTree_Insert(tree, (BTreeNode*)&n4, 0x00, 2, 0);
    BTree_Insert(tree, (BTreeNode*)&n5, 0x02, 2, 0);
    BTree_Insert(tree, (BTreeNode*)&n6, 0x02, 3, 0);

    printf("Height: %d\n", BTree_Height(tree));
    printf("Degree: %d\n", BTree_Degree(tree));
    printf("Count: %d\n", BTree_Count(tree));
    printf("Position At (0x02, 2): %c\n", ((struct Node*)BTree_Get(tree, 0x02, 2))->v);
    printf("Full Tree: \n");

    BTree_Display(tree, printf_data, 4, ‘-‘);

    BTree_Delete(tree, 0x00, 1);

    printf("After Delete B: \n");
    printf("Height: %d\n", BTree_Height(tree));
    printf("Degree: %d\n", BTree_Degree(tree));
    printf("Count: %d\n", BTree_Count(tree));
    printf("Full Tree: \n");

    BTree_Display(tree, printf_data, 4, ‘-‘);

    BTree_Clear(tree);

    printf("After Clear: \n");
    printf("Height: %d\n", BTree_Height(tree));
    printf("Degree: %d\n", BTree_Degree(tree));
    printf("Count: %d\n", BTree_Count(tree));

    BTree_Display(tree, printf_data, 4, ‘-‘);

    BTree_Destroy(tree);

    return 0;
}
/* BTree.h */
#ifndef _BTREE_H_
#define _BTREE_H_

#define BT_LEFT 0
#define BT_RIGHT 1

typedef void BTree;
typedef unsigned long long BTPos;

typedef struct _tag_BTreeNode BTreeNode;
struct _tag_BTreeNode
{
    BTreeNode* left;
    BTreeNode* right;
};

typedef void (BTree_Printf)(BTreeNode*);

BTree* BTree_Create();

void BTree_Destroy(BTree* tree);

void BTree_Clear(BTree* tree);

int BTree_Insert(BTree* tree, BTreeNode* node, BTPos pos, int count, int flag);

BTreeNode* BTree_Delete(BTree* tree, BTPos pos, int count);

BTreeNode* BTree_Get(BTree* tree, BTPos pos, int count);

BTreeNode* BTree_Root(BTree* tree);

int BTree_Height(BTree* tree);

int BTree_Count(BTree* tree);

int BTree_Degree(BTree* tree);

void BTree_Display(BTree* tree, BTree_Printf* pFunc, int gap, char div);

#endif
/* BTree.c */
#include <stdio.h>
#include <malloc.h>
#include "BTree.h"

typedef struct _tag_BTree TBTree;
struct _tag_BTree
{
    int count;
    BTreeNode* root;
};

static void recursive_display(BTreeNode* node, BTree_Printf* pFunc, int format, int gap, char div) // O(n)
{
    int i = 0;

    if( (node != NULL) && (pFunc != NULL) )
    {
        for(i=0; i<format; i++)
        {
            printf("%c", div);
        }

        pFunc(node);

        printf("\n");

        if( (node->left != NULL) || (node->right != NULL) )
        {
            recursive_display(node->left, pFunc, format + gap, gap, div);
            recursive_display(node->right, pFunc, format + gap, gap, div);
        }
    }
    else
    {
        for(i=0; i<format; i++)
        {
            printf("%c", div);
        }
        printf("\n");
    }
}

static int recursive_count(BTreeNode* root) // O(n)
{
    int ret = 0;

    if( root != NULL )
    {
        ret = recursive_count(root->left) + 1 + recursive_count(root->right);
    }

    return ret;
}

static int recursive_height(BTreeNode* root) // O(n)
{
    int ret = 0;

    if( root != NULL )
    {
        int lh = recursive_height(root->left);
        int rh = recursive_height(root->right);

        ret = ((lh > rh) ? lh : rh) + 1;
    }

    return ret;
}

static int recursive_degree(BTreeNode* root) // O(n)
{
    int ret = 0;

    if( root != NULL )
    {
        if( root->left != NULL )
        {
            ret++;
        }

        if( root->right != NULL )
        {
            ret++;
        }

        if( ret == 1 )
        {
            int ld = recursive_degree(root->left);
            int rd = recursive_degree(root->right);

            if( ret < ld )
            {
                ret = ld;
            }

            if( ret < rd )
            {
                ret = rd;
            }
        }
    }

    return ret;
}

BTree* BTree_Create() // O(1)
{
    TBTree* ret = (TBTree*)malloc(sizeof(TBTree));

    if( ret != NULL )
    {
        ret->count = 0;
        ret->root = NULL;
    }

    return ret;
}

void BTree_Destroy(BTree* tree) // O(1)
{
    free(tree);
}

void BTree_Clear(BTree* tree) // O(1)
{
    TBTree* btree = (TBTree*)tree;

    if( btree != NULL )
    {
        btree->count = 0;
        btree->root = NULL;
    }
}

int BTree_Insert(BTree* tree, BTreeNode* node, BTPos pos, int count, int flag) // O(n)
{
    TBTree* btree = (TBTree*)tree;
    int ret = (btree != NULL) && (node != NULL) && ((flag == BT_LEFT) || (flag == BT_RIGHT));
    int bit = 0;

    if( ret )
    {
        BTreeNode* parent = NULL;
        BTreeNode* current = btree->root;

        node->left = NULL;
        node->right = NULL;

        while( (count > 0) && (current != NULL) )
        {
            bit = pos & 1;
            pos = pos >> 1;

            parent = current;

            if( bit == BT_LEFT )
            {
                current = current->left;
            }
            else if( bit == BT_RIGHT )
            {
                current = current->right;
            }

            count--;
        }

        if( flag == BT_LEFT )
        {
            node->left = current;
        }
        else if( flag == BT_RIGHT )
        {
            node->right = current;
        }

        if( parent != NULL )
        {
            if( bit == BT_LEFT )
            {
                parent->left = node;
            }
            else if( bit == BT_RIGHT )
            {
                parent->right = node;
            }
        }
        else
        {
            btree->root = node;
        }

        btree->count++;
    }

    return ret;
}

BTreeNode* BTree_Delete(BTree* tree, BTPos pos, int count) // O(n)
{
    TBTree* btree = (TBTree*)tree;
    BTreeNode* ret = NULL;
    int bit = 0;

    if( btree != NULL )
    {
        BTreeNode* parent = NULL;
        BTreeNode* current = btree->root;

        while( (count > 0) && (current != NULL) )
        {
            bit = pos & 1;
            pos = pos >> 1;

            parent = current;

            if( bit == BT_LEFT )
            {
                current = current->left;
            }
            else if( bit == BT_RIGHT )
            {
                current = current->right;
            }

            count--;
        }

        if( parent != NULL )
        {
            if( bit == BT_LEFT )
            {
                parent->left = NULL;
            }
            else if( bit == BT_RIGHT )
            {
                parent->right = NULL;
            }
        }
        else
        {
            btree->root = NULL;
        }

        ret = current;

        btree->count = btree->count - recursive_count(ret);
    }

    return ret;
}

BTreeNode* BTree_Get(BTree* tree, BTPos pos, int count) // O(n)
{
    TBTree* btree = (TBTree*)tree;
    BTreeNode* ret = NULL;
    int bit = 0;

    if( btree != NULL )
    {
        BTreeNode* current = btree->root;

        while( (count > 0) && (current != NULL) )
        {
            bit = pos & 1;
            pos = pos >> 1;

            if( bit == BT_LEFT )
            {
                current = current->left;
            }
            else if( bit == BT_RIGHT )
            {
                current = current->right;
            }

            count--;
        }

        ret = current;
    }

    return ret;
}

BTreeNode* BTree_Root(BTree* tree) // O(1)
{
    TBTree* btree = (TBTree*)tree;
    BTreeNode* ret = NULL;

    if( btree != NULL )
    {
        ret = btree->root;
    }

    return ret;
}

int BTree_Height(BTree* tree) // O(n)
{
    TBTree* btree = (TBTree*)tree;
    int ret = 0;

    if( btree != NULL )
    {
        ret = recursive_height(btree->root);
    }

    return ret;
}

int BTree_Count(BTree* tree) // O(1)
{
    TBTree* btree = (TBTree*)tree;
    int ret = 0;

    if( btree != NULL )
    {
        ret = btree->count;
    }

    return ret;
}

int BTree_Degree(BTree* tree) // O(n)
{
    TBTree* btree = (TBTree*)tree;
    int ret = 0;

    if( btree != NULL )
    {
        ret = recursive_degree(btree->root);
    }

    return ret;
}

void BTree_Display(BTree* tree, BTree_Printf* pFunc, int gap, char div) // O(n)
{
    TBTree* btree = (TBTree*)tree;

    if( btree != NULL )
    {
        recursive_display(btree->root, pFunc, 0, gap, div);
    }
}
时间: 2024-10-08 11:13:07

树三:创建二叉树的相关文章

创建二叉树的两种方法以及三种遍历方法

二叉树的两种创建方法和三种遍历方法 这里的两种创建方法,一种值得是 数据结构上面的创建方法: 方法一 代码如下: 二叉树的结构定义如下: typedef struct BinaryTreeNode{ char value; struct BinaryTreeNode *left; struct BinaryTreeNode *right; }; - c语言版 void CreateBinaryTree(BinaryTreeNode **T) { char data; scanf("%d"

先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)

#include "stdio.h" #include "string.h" #include "malloc.h" #define NULL 0 #define MAXSIZE 30 typedef struct BiTNode      //定义二叉树数据结构 { char data; struct BiTNode *lchild,*rchild; } BiTNode; void preCreate(BiTNode *& T)   /

创建二叉树 树的深度搜索 广度搜索

树的深度搜索 与树的前序遍历同理 根节点->左孩子->右孩子  树的广度搜索 与树的层次遍历同理 一层一层遍历内容 深度搜索 采用stack的适配器 先进后出原则  而广度搜索采用的queue适配器 先进先出原则 二者正好满足 搜索需求 简要代码如下: #include <iostream> #include <stack> #include <queue> #include <malloc.h> using namespace std; typ

二叉树:树的创建和遍历

前面介绍的链表,栈,队列都是一种顺序容器,访问元素的时候都是通过位置来访问的.如果想要通过值的方式来获取数据,只能通过遍历的方式.这在时间上消耗比较大.而二叉树可以做到不用遍历就可以通过值的方式来获取数据.二叉树是按值来保存元素,也按值来访问元素. 二叉树的相关术语: 树的结点:包含一个数据元素及若干指向子树的分支: 孩子结点:结点的子树的根称为该结点的孩子: 双亲结点:B 结点是A 结点的孩子,则A结点是B 结点的双亲: 兄弟结点:同一双亲的孩子结点: 堂兄结点:同一层上结点: 祖先结点: 从

哈夫曼树(三)之 Java详解

前面分别通过C和C++实现了哈夫曼树,本章给出哈夫曼树的java版本. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载请注明出处:http://www.cnblogs.com/skywang12345/ 更多内容:数据结构与算法系列 目录 哈夫曼树的介绍 Huffman Tree,中文名是哈夫曼树或霍夫曼树,它是最优二叉树. 定义:给定n个权值作为n个叶子结点,构造一棵二叉树,若树的带权路径长度达到最小,则这棵树被称为哈夫曼树. 这

003--如何创建二叉树

一.基本概念                                                          二叉树是每个节点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(right subtree) 二.二叉树性质(特性) 1.在二叉树的第i层上至多有2^(i-1)个结点(i>0) 2.深度为k的二叉树至多有2^k - 1个结点(k>0) 3.对于任意一棵二叉树,如果其叶结点数为N0,而度数

创建二叉树、创建链表等

方法一: 1 #include <iostream>//创建二叉树,要用二级指针 2 3 using namespace std; 4 5 typedef struct TreeNode 6 { 7 char data; 8 struct TreeNode *left; 9 struct TreeNode *right; 10 }BiTree; 11 12 void creatBitree(BiTree **T) 13 { 14 char ch; 15 cin >> ch; 16

层次创建二叉树

第一种: 主要是利用 树结点类型的数组.二叉树结点序号之间的关系 来创建: 父结点序号为 i 则,左儿子结点序号为 2*i ,右儿子序号为 2*i+1. //用层次遍历的方法来创建二叉树 #include <iostream> #include <queue> using namespace std; //二叉链表的结构类型定义 const int maxsize=1024; typedef char datatype; typedef struct node { datatype

数据结构上机 【创建二叉树,并采用先中后序遍历,输出树高,度数为分别为0 1 2 的结点个数】

#include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<malloc.h> #define null 0 using namespace std; typedef struct node { int data;//节点 node *lchild,*rchild; }node,*Tree; typedef struct{ Tree to

树 森林与二叉树的转换

1.树.森林为什么向二叉树转换? 因为在实际的处理问题中,大多数情况都是一对多,就向树.森林这样的数据结构! 而对于二叉树我们已经很熟悉了,所以转向我们所熟悉的结构,好处理. 2.孩子兄弟树的方法 把握左孩子右兄弟的原则: (1).树与二叉树的转换:i>以树的根结点为二叉树的根节点: ii>左孩子指针指向该根节点的第一个子结点: iii>右孩子指针指向"兄弟结点" (2).二叉树表示森林:i>二叉树的根结点是森林中第一棵树的根结点 ii>根结点的右孩子为森