數據結構之二叉搜索樹

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

根據前序,中序求後序

/**
 * @param before          前序數組
 * @param mid             中序數組
 * @param beforeLocation  子樹開始位置 中序數組
 * @param afterLocation   子樹開始位置 中序數組
 * @param parentLocation  父節點位置 中序數組
 * @param parent          父節點
 */
void createTreeByBeforeAndMid(int before[],int mid[],int beforeLocation,int afterLocation,int parentLocation, SearchTree *parent){
    if(beforeLocation != parentLocation)
        createLeftTreeByBeforeAndMid(before,mid,beforeLocation,parentLocation - 1,parent);
    if(afterLocation != parentLocation)
        createRightTreeByBeforeAndMid(before,mid,parentLocation + 1,afterLocation,parent);
}

/**
 * @param before          前序數組
 * @param mid             中序數組
 * @param beforeLocation  左子樹開始位置 中序數組
 * @param afterLocation   左子樹開始位置 中序數組
 * @param parent          父節點
 */
void createLeftTreeByBeforeAndMid(int before[],int mid[],int beforeLocation,int afterLocation, SearchTree *parent){
    if(beforeLocation == afterLocation){
        SearchTree* searchTree = (SearchTree *)malloc(sizeof(struct BinaryTreeNode));
        searchTree->key = mid[beforeLocation];
        searchTree->leftChild = searchTree->rightChild = NULL;
        parent->leftChild = searchTree;
        return;
    }
    int rootLocation = -1;
    int i,j;
    for(j = 0;j < 6;j ++){
        for(i = beforeLocation;i <= afterLocation;i ++){
            if(before[j] == mid[i]){
                rootLocation = i;
                break;
            }
            if(rootLocation > 0){
                break;
            }
        }
    }
    SearchTree* searchTree = (SearchTree *)malloc(sizeof(struct BinaryTreeNode));
    searchTree->key = mid[rootLocation];
    searchTree->leftChild = searchTree->rightChild = NULL;
    parent->leftChild = searchTree;
    createTreeByBeforeAndMid(before,mid,beforeLocation,afterLocation,rootLocation,searchTree);
}

/**
 * @param before          前序數組
 * @param mid             中序數組
 * @param beforeLocation  右子樹開始位置 中序數組
 * @param afterLocation   右子樹開始位置 中序數組
 * @param parent          父節點
 */
void createRightTreeByBeforeAndMid(int before[],int mid[],int beforeLocation,int afterLocation, SearchTree *parent){
    if(beforeLocation == afterLocation){
        SearchTree* searchTree = (SearchTree *)malloc(sizeof(struct BinaryTreeNode));
        searchTree->key = mid[beforeLocation];
        searchTree->leftChild = searchTree->rightChild = NULL;
        parent->rightChild = searchTree;
        return;
    }
    int rootLocation = -1;
    int i,j;
    for(j = 0;j < 6;j ++){
        for(i = beforeLocation;i <= afterLocation;i ++){
            if(before[j] == mid[i]){
                rootLocation = i;
                break;
            }
            if(rootLocation > 0){
                break;
            }
        }
    }
    SearchTree* searchTree = (SearchTree *)malloc(sizeof(struct BinaryTreeNode));
    searchTree->key = mid[rootLocation];
    searchTree->leftChild = searchTree->rightChild = NULL;
    parent->rightChild = searchTree;
    createTreeByBeforeAndMid(before,mid,beforeLocation,afterLocation,rootLocation,searchTree);
}

BFS 廣度優先搜索算法

/**
     * bfs
     * @return
     */
    public GraphNode<K,V>[] bfsGraph(GraphNode<K,V> root){
        queue = new ArrayDeque<GraphNode<K,V>>();
        if(table[(Integer) root.getKey()] == null){
            return null;
        }
        root.setSearched(true);
        queue.add(root);
        GraphNode<K,V>[] tableBfs = new GraphNode[tableSize + 1];
        return bfs(tableBfs,0);
    }

    /**
     * bfs
     * @param tableBfs
     * @param location
     * @return
     */
    private GraphNode<K,V>[] bfs(GraphNode<K,V>[] tableBfs,int location){
        if(queue.size() == 0){
            return tableBfs;
        }
        GraphNode<K,V> graphNode = queue.poll();
        graphNode.setSearched(true);
        tableBfs[location ++] = graphNode;
        GraphNode<K,V> a = graphNode;
        while(true){
            if(a.getNext() == null){
                break;
            }
            if(!table[(Integer)a.getNext().getKey()].isSearched())
                queue.add(table[(Integer)a.getNext().getKey()]);
            a = a.getNext();
        }
        return bfs(tableBfs,location);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章