• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大漠落日

            while(!dead) study++;
            posts - 46, comments - 126, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
              1#include <stdio.h>
              2#include <stdarg.h>
              3
              4#ifndef bool
              5#define bool unsigned int
              6#define false    0
              7#define true    1
              8#endif
              9
             10#ifndef NULL
             11#define NULL    0
             12#endif
             13
             14#define traversal_output
             15
             16/* traversal order */
             17typedef enum {
             18    NLR    = 0,    /* preorder */
             19    LNR = 1,    /* inorder */
             20    LRN = 2        /* postorder */
             21}
            Order;
             22
             23typedef struct _BTNode{
             24    struct BTNode *l_child;
             25    struct BTNode *r_child;
             26    void *data;
             27}
            BTNode;
             28
             29BTNode *create_node(void)
             30{
             31    BTNode *node = (BTNode *)malloc(sizeof(BTNode));
             32    
             33    if (node != NULL) {
             34        node->data = NULL;
             35        node->l_child = NULL;
             36        node->r_child = NULL;
             37    }

             38    
             39    return node;
             40}

             41
             42void destroy_node(BTNode *node)
             43{
             44    free(node);
             45    node = NULL;
             46}

             47
             48void bt_printf(const char *fmt, )
             49{
             50#ifdef traversal_output
             51    va_list list;
             52    char buf[256];
             53    va_start(list, fmt);
             54    vsnprintf(buf, sizeof(buf), fmt, list);
             55    printf("%s", buf);
             56    va_end(list);
             57#endif
             58}

             59
             60BTNode *insert_child(BTNode *parent, bool left, void *data)
             61{
             62    BTNode *node;
             63    if (parent == NULL)    {
             64        return NULL;
             65    }

             66
             67    if ((node = create_node()) == NULL) {
             68        return NULL;
             69    }

             70
             71    node->data = data;
             72
             73    if (left) {
             74        parent->l_child = node;
             75    }

             76    else {
             77        parent->r_child = node;
             78    }

             79
             80    return node;
             81}

             82
             83void remove_child(BTNode *parent, BTNode *node, bool destroy)
             84{
             85    if (parent == NULL || node == NULL) {
             86        return;
             87    }

             88
             89    if (parent->l_child == node) {
             90        parent->l_child = NULL;
             91    }

             92    else if (parent->r_child == node) {
             93        parent->r_child = NULL;
             94    }

             95    else {
             96        return;
             97    }

             98
             99    if (destroy) {
            100        destroy_node(node);
            101    }

            102}

            103
            104BTNode *NLR_search(BTNode *parent, void *data)
            105{
            106    BTNode *node = NULL;
            107
            108    if (parent == NULL) {
            109        return NULL;
            110    }

            111    
            112    bt_printf("->%d\n"*(int *)(parent->data));
            113
            114    //首先訪問中間
            115    if (parent->data == data){
            116        return parent;
            117    }

            118    
            119    if ((node = NLR_search(parent->l_child, data)) == NULL) {
            120        return NLR_search(parent->r_child, data);
            121    }

            122    return node;
            123}

            124
            125BTNode *LNR_search(BTNode *parent, void *data)
            126{
            127    BTNode *node = NULL;
            128
            129    if (parent == NULL) {
            130        return NULL;
            131    }

            132
            133    //首先訪問左邊
            134    node = LNR_search(parent->l_child, data);
            135
            136    bt_printf("->%d\n"*(int *)(parent->data));
            137
            138    if (node == NULL) {
            139        if (parent->data == data) {
            140            return parent;
            141        }

            142        else {
            143            return LNR_search(parent->r_child, data);
            144        }

            145    }

            146    return node;
            147}

            148
            149BTNode *LRN_search(BTNode *parent, void *data)
            150{
            151    BTNode *node = NULL;
            152
            153    if (parent == NULL) {
            154        return NULL;
            155    }

            156
            157    //首先訪問右邊
            158    node = LRN_search(parent->r_child, data);
            159
            160    if (node == NULL) {
            161        node = LRN_search(parent->l_child, data);
            162        if (node == NULL && parent->data == data) {
            163            node = parent;
            164        }

            165    }

            166
            167    bt_printf("->%d\n"*(int *)(parent->data));
            168
            169    return node;
            170}

            171
            172BTNode *find_node(BTNode *parent, void *data, Order order)
            173{
            174    if (order == NLR) {
            175        return NLR_search(parent, data);
            176    }

            177    else if (order == LNR) {
            178        return LNR_search(parent, data);
            179    }

            180    else if (order == LRN) {
            181        return LRN_search(parent, data);
            182    }

            183    return NULL;
            184}

            185
            186/*****************************************
            187 *    test code
            188 *****************************************/

            189
            190//我們來找它
            191void *target;
            192
            193BTNode *insert_binary_node(BTNode *parent, bool left, int num)
            194{
            195    int *data;
            196    data = (int *)malloc(sizeof(int));
            197    *data = num;
            198    return insert_child(parent, left, (void *)data);
            199}

            200
            201/*                    root(100)
            202 *                    /  \ 
            203 *                   1    2
            204 *                  /    / \
            205 *                 3      4   5
            206 *                / \         / \
            207 *             NULL  0   NULL NULL
            208 */

            209BTNode *create_binary_tree()
            210{
            211    BTNode *root, *node;
            212    
            213    root = create_node();
            214    root->data = (int *)malloc(sizeof(int));
            215    *(int *)(root->data) = 100;
            216
            217    //構(gòu)建左邊樹
            218    node = insert_binary_node(root, true1);
            219    node = insert_binary_node(node, true3);
            220    node = insert_binary_node(node, false0);
            221
            222    //這就是我們要找的目標
            223    target = node->data;
            224    
            225    //構(gòu)建右邊樹
            226    node = insert_binary_node(root, false2);
            227    insert_binary_node(node, true4);
            228    insert_binary_node(node, false5);
            229    
            230    return root;
            231}

            232
            233void destroy_binary_tree(BTNode *root)
            234{
            235    //用LRN遍歷并銷毀每個節(jié)點
            236}

            237
            238void main(int argc, char *argv[])
            239{
            240    BTNode *root, *node;
            241
            242    root = create_binary_tree();
            243
            244    node = find_node(root, target, NLR);
            245    printf("NLR\ttarget=%d\n\n"*(int *)(node->data));
            246
            247    node = find_node(root, target, LNR);
            248    printf("LNR\ttarget=%d\n\n"*(int *)(node->data));
            249
            250    node = find_node(root, target, LRN);
            251    printf("LRN\ttarget=%d\n\n"*(int *)(node->data));
            252
            253    destroy_binary_tree(root);
            254
            255    getchar();
            256}
            久久久精品国产亚洲成人满18免费网站 | 蜜臀久久99精品久久久久久小说| 久久99国产精品久久99果冻传媒| 伊人久久大香线蕉综合Av| 久久久久国产一区二区| 天天影视色香欲综合久久| 亚洲国产一成久久精品国产成人综合 | 久久久久久综合一区中文字幕| 日产精品久久久一区二区| 国产三级久久久精品麻豆三级 | 国产精品久久99| 久久久精品波多野结衣| 亚洲午夜无码久久久久| 久久免费精品视频| 久久精品青青草原伊人| 久久久久婷婷| 丰满少妇人妻久久久久久4| 伊人久久大香线蕉AV色婷婷色 | 久久精品中文无码资源站| 欧美激情精品久久久久久久九九九| 7777精品伊人久久久大香线蕉| 久久精品国产影库免费看| 亚洲色欲久久久综合网东京热| 久久一区二区三区免费| 欧美麻豆久久久久久中文| 狠狠综合久久综合中文88| 一本色道久久88加勒比—综合| 久久精品人人做人人爽97 | 久久国产精品久久| 精品久久久久中文字幕一区| 久久A级毛片免费观看| 久久久久se色偷偷亚洲精品av| 久久精品夜色噜噜亚洲A∨| 国产激情久久久久影院老熟女| 国产精品久久久久9999高清| 色综合久久中文综合网| 国产成人无码精品久久久免费 | 伊人情人综合成人久久网小说| 久久无码国产| 久久人妻AV中文字幕| 国产一久久香蕉国产线看观看|