1. 程式人生 > >數據結構之二叉搜索樹

數據結構之二叉搜索樹

二叉搜索樹

C語言實現二叉搜索樹
很簡單,權當復習下指針知識
//
// Created by SuperHakce on 2018/3/29.
//

#ifndef BINARYSEARCHTREE_BINARYTREE_H
#define BINARYSEARCHTREE_BINARYTREE_H

typedef struct BinaryTreeNode{
    int key;
    struct BinaryTreeNode *leftChild;
    struct BinaryTreeNode *rightChild;
}SearchTree;

void insert(SearchTree * node,int value);

void printTree(SearchTree *node);

void beforeSearch(SearchTree *node);

void midSearch(SearchTree *node);

void afterSearch(SearchTree *node);

int findByValue(SearchTree *node,int value);

#endif //BINARYSEARCHTREE_BINARYTREE_H
//
// Created by SuperHakce on 2018/3/29.
//
#include <malloc.h>
#include <stdio.h>
#include "BinaryTree.h"

void insert(SearchTree * node,int value){
    if(node == NULL){
        node = (SearchTree *)malloc(sizeof(struct BinaryTreeNode));
        node->key = value;
        node->leftChild = node->rightChild = NULL;
    } else if(value > node->key){
        if(node->rightChild == NULL){
            SearchTree* a = (SearchTree *)malloc(sizeof(struct BinaryTreeNode));
            a->key = value;
            a->leftChild = a->rightChild = NULL;
            node->rightChild = a;
        }
        insert(node->rightChild,value);
    } else if(value < node->key){
        if(node->leftChild == NULL){
            SearchTree* a = (SearchTree *)malloc(sizeof(struct BinaryTreeNode));
            a->key = value;
            a->leftChild = a->rightChild = NULL;
            node->leftChild = a;
        }
        insert(node->leftChild,value);
    }
    return;
}

void printTree(SearchTree *node){
    if(node == NULL){
        return;
    }
    printTree(node->leftChild);
    printf("%d\n",node->key);
    printTree(node->rightChild);
}

void beforeSearch(SearchTree *node){
    if(node == NULL){
        return;
    }
    printf("%d ",node->key);
    beforeSearch(node->leftChild);
    beforeSearch(node->rightChild);
}

void midSearch(SearchTree *node){
    if(node == NULL){
        return;
    }
    beforeSearch(node->leftChild);
    printf("%d ",node->key);
    beforeSearch(node->rightChild);
}

void afterSearch(SearchTree *node){
    if(node == NULL){
        return;
    }
    beforeSearch(node->leftChild);
    beforeSearch(node->rightChild);
    printf("%d ",node->key);
}

int findByValue(SearchTree *node,int value){
    if(node == NULL){
        return -1;
    }
    if(node->key == value){
        return node->key;
    }
    if(value > node->key){
        findByValue(node->rightChild,value);
    }else if(value < node->leftChild){
        findByValue(node->leftChild,value);
    }
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "BinaryTree/BinaryTree.h"

int main() {
    SearchTree* root = (SearchTree *)malloc(sizeof(struct BinaryTreeNode));
    srand(time(NULL));
    root->key = rand();
    printf("root = %d\n",root->key);
    root->leftChild = root->rightChild = NULL;
    int i;
    for(i = 0;i < 6;i ++){
        insert(root,rand());
    }
    beforeSearch(root);
    printf("\n");
    midSearch(root);
    printf("\n");
    afterSearch(root);
    printf("\n");
    insert(root,1456);
    int a = findByValue(root,1456);
    printf("%d\n",a);
} 

數據結構之二叉搜索樹