C 查找二叉树的基本操作

//
// main.c
// 二叉排序树
//
// Created by 赫凯 on 2018/10/31.
// Copyright © 2018 赫凯. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int ElementType;

typedef struct BinaryTreeNode{
    ElementType data;
    struct BinaryTreeNode *LeftTreeNode;
    struct BinaryTreeNode *RightTreeNode;
}BiThrNode, *BiThrTree;

//创建排序二叉树,非递归
void CreatSortTree1(BiThrTree *T, int temp[10]){
    int i;
    BiThrTree p, p1;
    (*T) = (BiThrTree)malloc(sizeof(BiThrNode));
    (*T)->data = temp[0];
    (*T)->LeftTreeNode = (*T)->RightTreeNode = NULL;
    
    for (i = 1; i < 10; i++) {
        p = (BiThrTree)malloc(sizeof(BiThrNode));
        p->data = temp[i];
        p->LeftTreeNode = p->RightTreeNode = NULL;
        p1 = (*T);
        while (p->data != p1->data) {
            if(p->data <= p1->data){
                if(p1->LeftTreeNode)
                {
                    p1 = p1->LeftTreeNode;
                }else
                {
                    p1->LeftTreeNode = p;
                    p1 = p;
                }
            }
            
            if(p->data > p1->data){
                if(p1->RightTreeNode){
                    p1 = p1->RightTreeNode;
                }else
                {
                    p1->RightTreeNode = p;
                    p1 = p;
                }
            }
        }
    }
}

//创建排序二叉树递归
BiThrTree CreatSortTree2(int temp, BiThrTree *T){
    if((*T) == NULL){
        (*T) = (BiThrTree)malloc(sizeof(BiThrNode));
        (*T)->data = temp;
        (*T)->LeftTreeNode = (*T)->RightTreeNode = NULL;
        return (*T);
    }else{
        if(temp > (*T)->data){
            (*T)->RightTreeNode = CreatSortTree2(temp, &(*T)->RightTreeNode);
        }else
        {
            (*T)->LeftTreeNode = CreatSortTree2(temp, &(*T)->LeftTreeNode);
        }
    }
    return (*T);
}

//查找
//T查找的节点,key被查找的数,pa找到的爸爸节点,q返回查找的结点
int Sreachtree(BiThrTree T, int key, BiThrTree pa, BiThrTree *q){
    if (!T) {
        *q = pa;
        return 0;
    }
    
    if(T->data == key)
    {
        *q = T;
        return 1;
    }else{
        if(key < T->data){
            return Sreachtree(T->LeftTreeNode, key, T, q);
        }else
        {
            return Sreachtree(T->RightTreeNode, key, T, q);
        }
    }
}

//中序遍历
void putout2(BiThrTree T){
    if(T)
    {
        putout2(T->LeftTreeNode);
        printf("%d ", T->data);
        putout2(T->RightTreeNode);
    }
}
//找一个最大
void FindMax(BiThrTree T, BiThrTree *e){
    while (T->RightTreeNode) {
        T = T->RightTreeNode;
    }
    *e = T;
}
//找一个最小
void FindMin(BiThrTree T, BiThrTree *e){
    while (T->LeftTreeNode) {
        T = T->LeftTreeNode;
    }
    *e = T;
}

//删除结点,总要点为遵从,删了节点a,顶上来的节点b要比a的左子树大且比a的右子树小
int Delete(BiThrTree *p)
{
    BiThrTree q, s;
    if((*p)->RightTreeNode == NULL)
    {
        q = *p;
        (*p) = (*p)->LeftTreeNode;
        free(q);
    }else if((*p)->LeftTreeNode == NULL){
        q = *p;
        (*p) = (*p)->RightTreeNode;
        free(q);
    }else
    {
        q = *p;
        s = (*p)->LeftTreeNode;
        
        while ( s->RightTreeNode ) {
            q = s;
            s = s->RightTreeNode;
        }
        
        (*p)->data = s->data;
        if(q != *p)
        {
            q->RightTreeNode = s->LeftTreeNode;
        }else
        {
            q->LeftTreeNode = s->LeftTreeNode;
        }
        free(s);
    }
    return 1;
    
}

int DeleteBST(BiThrTree *T, int key){
    if(!(*T)){
        return 0;
    }else
    {
        if( key == (*T)->data ){
            return Delete(T);
        }else if( key < (*T)->data){
            return DeleteBST(&(*T)->LeftTreeNode, key);
        }else
        {
            return DeleteBST(&(*T)->RightTreeNode, key);
        }
    }
}



int main(int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    
    BiThrTree tree = NULL, e = NULL, pa = NULL;
    int temp[10] = {4,5,3,8,6,9,5,2,1,10}, i;
// CreatSortTree1(&tree, temp);
    for (i = 0; i < 10; i++) {
        CreatSortTree2(temp[i], &tree);
    }
    
    putout2(tree);
    if (Sreachtree(tree, 12, pa, &e)) {
        printf("\n找到了!节点为%d", e->data);
    }else
    {
        printf("\n没有找到!双亲节点为:%d", e->data);
    }
    FindMax(tree, &e);
    printf("\n最大:%d", e->data);
    FindMin(tree, &e);
    printf("\n最小:%d", e->data);
    
    return 0;
}

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/u010095372/article/details/83587642
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞