[料] BST 的 construct & traversal@Morris' Blog|PChome Online 人新台
2012-11-29 21:11:28| 人2,270| 回0 | 上一篇 | 下一篇

[料] BST 的 construct & traversal

0 收藏 0 0 站台

Problem :

BST二元的一,符合下面件 :

1. 所有在左子之 Node 之值一定不大於 Root 之值

2. 所有在右子之 Node 之值一定不小於 Root 之值

入值一串整, 字之以空白隔

入的 DATA 依序 Insert 以建立一 BST

依序出其 Inorder,Preorder,Postorder以及node

(考下面所附的及sample)

Sample input:

50 30 98 24 45 52 5 28 60

Sample output:

5 24 28 30 45 50 52 60 98

50 30 24 5 28 45 98 52 60

5 28 24 45 30 60 52 98 50

交期限:

12/11/2012 23:59

超期限者每多一天扣作分10%

超12/15/2012 23:59後交一律不分。

交格式:

以C/C++完成

一律程式.c/.cpp案.rar/.zip/.7z上至FTP

交案格式

_HW3_Ver1.rar

ex:101522054_HW3_Ver1.rar

(在期限如果想再重新上案命名ver2 ver3以此推

助教改作以最後一版)



次程式有很,但是我不定我有有。



demo 果如上

#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <queue>
using namespace std;
struct BTnode{
    BTnode *l, *r;
    int num;
};
void insBT(BTnode **root, int num) {
    if((*root) == NULL) {
        (*root) = new BTnode;
        (*root)->num = num;
        (*root)->l = (*root)-> r = NULL;
        return;
    }
    BTnode *idx, *p;
    idx = (*root);
    while(idx) {
  nbsp;     if(idx->num > num) {
            if(idx->l == NULL) {
                p = new BTnode;
                idx->l = p;
                p->num = num;
                p->l = p->r = NULL;
                return;
            }
            idx = idx->l;
        } else {
            if(idx->r == NULL) {
                p = new BTnode;
                idx->r = p;
                p->num = num;
                p->l = p->r = NULL;
                return;
            }
            idx = idx->r;
        }
    }
}
void preorder(BTnode *p) {
    if(p == NULL)   return;
    printf("%-5d", p->num);
    preorder(p->l);
    preorder(p->r);
}
void inorder(BTnode *p) {
    if(p == NULL)   return;
    inorder(p->l);
    printf("%-5d", p->num);
    inorder(p->r);
}
void postorder(BTnode *p) {
    if(p == NULL)   return;
    postorder(p->l);
    postorder(p->r);
    printf("%-5d", p->num);
}
void levelorder(BTnode *p) {
    queue<BTnode*> Q;
    Q.push(p);
    while(!Q.empty()) {
        p = Q.front();
        Q.pop();
        printf("%-5d", p->num);
        if(p->l)
            Q.push(p->l);
        if(p->r)
            Q.push(p->r);
    }
}
int cntNode(BTnode *p) {
    if(p == NULL)   return 0;
    return cntNode(p->l)+cntNode(p->r)+1;
}
void freeBT(BTnode *p) {
    if(p == NULL)   return;
    freeBT(p->l);
    freeBT(p->r);
    delete p;
}
int main() {
    char line[1024];
    BTnode *root = NULL;
    int num;
    printf("入列 用空白隔:");
    gets(line);
    stringstream sin(line);
    while(sin >> num) {
        insBT(&root, num);
    }
    printf("inorder:    ");
    inorder(root);
    puts("");
    printf("preorder:   ");
    preorder(root);
    puts("");
    printf("postorder:  ");
    postorder(root);
    puts("");
    printf("levelorder: ");
    levelorder(root);
    puts("");
    printf("Node: %d\n", cntNode(root));
    freeBT(root);
    system("pause");
    return 0;
}


台: Morris
人(2,270) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: 其他目 |
此分下一篇:[性代][作] 旋矩
此分上一篇:[演算法][程式作] LCS

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86