数据结构之二叉搜索树

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构之二叉搜索树相关的知识,希望对你有一定的参考价值。

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);
} 

以上是关于数据结构之二叉搜索树的主要内容,如果未能解决你的问题,请参考以下文章

数据结构之二叉搜索树

Java数据结构之二叉搜索树

Java数据结构之二叉搜索树

Java数据结构之二叉搜索树

javascript 数据结构之二叉搜索树

数据结构之二叉搜索树