2017年5月23日 星期二

二元搜尋樹(Binary Search Tree)

/*the filename: main
 *the filename extension: .c
 */
#include <stdio.h>
#include <stdlib.h>
#include "bst.h"

int main()
{
    BST_TREE *treeHead;
    treeHead = (BST_TREE *) malloc(sizeof(BST_TREE));
    buildBST_Tree (treeHead);
    insertStudentNode(treeHead, "john", 52);
    insertStudentNode(treeHead, "john", 23);
    insertStudentNode(treeHead, "john", 18);
    insertStudentNode(treeHead, "john", 44);
    insertStudentNode(treeHead, "john", 12);
    insertStudentNode(treeHead, "john", 20);
    insertStudentNode(treeHead, "john", 35);


    //  insertStudentNode(treeHead, "john", 70);
    //  insertStudentNode(treeHead, "Bo", 80);
    //  insertStudentNode(treeHead, "merry", 20);
    //    insertStudentNode(treeHead, "alex", 60);
    dumpInOrder(treeHead);
    printf("Hello world!\n");
    return 0;
}

/*the filename: bst_imp
 *the filename extension: .c
 */
#include "bst.h"
#include <string.h>

void buildBST_Tree(BST_TREE *treeHead){
    treeHead->count = 0;
    treeHead->root = NULL;
}


void printInOrder(NODE *curr){
    if (curr != NULL){
        if (curr->left != NULL){
            printInOrder(curr->left);
        }
        printf("%s \t %d\n", curr->name, curr->score);
        if (curr->right != NULL){
            printInOrder(curr->right);
        }
    }
}


void dumpInOrder(BST_TREE *treeHead){
    if (treeHead->count == 0){
        printf("empty treen");
    }
    else {
        printf("There are %d nodes \n",treeHead->count);
        printInOrder(treeHead->root);
    }
}

void insert(NODE *parent, NODE *student){
    if (student->score < parent->score){
        if (parent->left == NULL) {
            parent->left = student;
        }
        else {
            insert (parent->left, student);
        }
    }
    else {
        if (parent->right == NULL) {
            parent->right = student;
        }
        else {
            insert (parent->right, student);
        }
    }
}

void insertStudentNode(BST_TREE *treeHead, char *name, int score)
{
    NODE *currNodePtr;
    currNodePtr = (NODE *) malloc(sizeof(NODE));
    strcpy(currNodePtr->name, name);
    currNodePtr->score = score;
    currNodePtr->left = NULL;
    currNodePtr->right = NULL;
    if (treeHead->root == NULL){
        treeHead->root = currNodePtr;
    }
    else {
        insert(treeHead->root, currNodePtr);
    }
    treeHead->count++;
}


/*the filename: bst
 *the filename extension: .h
 */


typedef struct node {
    char name[10];
    int score;
    struct node *left;
    struct node *right;
} NODE;

typedef struct {
    int count;
    NODE *root;
} BST_TREE;

void insertStudentNode(BST_TREE *treeHead, char *name, int score);

void dumpInOrder(BST_TREE *treeHead);

void buildBST_Tree(BST_TREE *treeHead);

=================================================================
/*the filename: main
 *the filename extension: .c
 */
#include <stdio.h>
#include <stdlib.h>
#include "bst.h"

int main ()
{
    t *tree = (t *)malloc(sizeof(t));
    build_tree(tree);
    insert_data(tree,"t",53);
    insert_data(tree,"t",87);
    insert_data(tree,"t",78);
    insert_data(tree,"t",64);
    insert_data(tree,"t",56);
    insert_data(tree,"t",89);
    insert_data(tree,"t",3);
    insert_data(tree,"t",5);
    insert_data(tree,"t",2);
    insert_data(tree,"t",1);
    print_in_order(tree);

    return 0;
}

/*the filename: bst
 *the filename extension: .h
 */
typedef struct node
{
    char data[10];
    int num;
    struct node *left;
    struct node *right;
} n;

typedef struct tree
{
    int count;
    n *root;
} t;
void build_tree(t *tree);
void insert_data(t *tree, char *data,int num);
void print_in_order(t *tree);







/*the filename: bst_imp
 *the filename extension: .c
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bst.h"

void build_tree(t *tree)
{
    tree->count = 0;
    tree->root = NULL;
}

void insert(n *parent,n *data)
{
    if(parent->num > data->num)
    {
        if(parent->left == NULL )
            parent->left=data;
        else
            insert(parent->left,data);
    }
    else
    {
        if(parent->right == NULL )
            parent->right=data;
        else
            insert(parent->right,data);
    }

}

void insert_data(t *tree,char *data, int num)
{
    n* a = (n*)malloc(sizeof(n));
    strcpy(a->data,data);
    a->num=num;
    a->left = NULL;
    a->right = NULL;
    if(tree->count==0)
        tree->root = a;
    else
        insert(tree->root,a);
    tree->count++;
}
void print_data(n *a)
{
    if(a!=NULL)
    {
//        printf("Preorder : %10s %10d\n",a->data,a->num);  //Preorder
        if(a->left != NULL)
            print_data(a->left);

        printf("Inorder : %10s %10d\n",a->data,a->num);  //Inorder
        if(a->right != NULL)
            print_data(a->right);
//        printf("Postorder : %10s %10d\n",a->data,a->num);  //Postorder
    }
}
void print_in_order(t *tree)
{
    if(tree->count == 0)
        printf("shit no data\n");
    else
    {
        printf("there are %d nodes\n",tree->count);
        print_data(tree->root);
    }
}




沒有留言:

張貼留言