[c]一个简单的二叉树
来源:互联网 发布:三国无双7帝国初音数据 编辑:程序博客网 时间:2024/04/28 01:39
使用C语言练练手避免手生。
文件主要有三个: bst.h, bst.c, test_bst.c
文件bst.h
typedef enum {FALSE=0, TRUE=1} bool;/* * Define a node structure. */typedef struct node { int val; struct node* left; struct node* right;} node_t;/* * Creates a new node from a given value, allocating heap memory for it. */node_t* make_tree(int val);/* * Inserts a new value into a given binary search tree, allocating heap memory * for it. */node_t* insert(int val, node_t* cur_root);bool find_val(int val, node_t* root);/* * Given a pointer to the root, frees the memory associated with an entire list. */void delete_bst(node_t* root);/* Given a pointer to the root, prints all of the values in a tree. */// pre order visitingvoid print_bst(node_t* root);
文件 bst.c
#include <stdio.h>#include <stdlib.h>#include "bst.h"/* * Creates a new node from a given value, allocating heap memory for it. */node_t* make_tree(int val) { node_t* new_tree = malloc(sizeof(node_t)); new_tree->val = val; new_tree->left = NULL; new_tree->right = NULL; return new_tree;}/* * Inserts a new value into a given binary search tree, allocating heap memory * for it. */node_t* insert(int val, node_t* cur_root) { /* always return root node */ node_t* left_node; node_t* right_node; node_t* new_node; node_t* root; root = cur_root; while(cur_root != NULL){ left_node = cur_root->left; right_node = cur_root->right; if(val < cur_root->val){ if(left_node == NULL){ new_node = make_tree(val); cur_root->left = new_node; return root; } else{ cur_root = cur_root->left; } } else if(val > cur_root->val){ if(right_node == NULL){ new_node = make_tree(val); cur_root->right = new_node; return root; } else{ cur_root = cur_root->right; } } else{ // the val has been inserted return NULL; } } //empty tree cur_root = make_tree(val); return cur_root;}bool find_val(int val, node_t* root) { /* recursive find */ if(root != NULL){ if(val == root->val){ return TRUE; } else if (val < root->val){ return find_val(val, root->left); } else { return find_val(val, root->right); } } return FALSE;}/* * Given a pointer to the root, frees the memory associated with an entire tree. */void delete_bst(node_t* root) { /* recursive delete */ if(root != NULL){ delete_bst(root->left); delete_bst(root->right); free(root); root = NULL; } return;}/* Given a pointer to the root, prints all of the values in a tree. */void print_bst(node_t* root) { if (root != NULL) { printf("%d ", root->val); print_bst(root->left); print_bst(root->right); } return;}
测试文件:
test_bst.c
#include <assert.h>#include <stdio.h>#include "bst.h"int main() { /* Insert 0. */ node_t* root = make_tree(1); node_t* cur; print_bst(root); printf("\n"); assert(find_val(1, root) == TRUE); /* Insert 1. */ cur = insert(0, root); assert(find_val(0, root) == TRUE); print_bst(root); printf("\n"); /* Insert 2. */ cur = insert(2, root); assert(find_val(2, root) == TRUE); /* Print the tree. */ print_bst(root); printf("\n"); /* Insert 4. */ cur = insert(4, root); assert(find_val(4, root) == TRUE); /* Insert 3 */ cur = insert(3, root); assert(find_val(3, root) == TRUE); print_bst(root); printf("\n"); /* Delete the list. */ delete_bst(root); return 0;}
测试结果:
输入命令:
$ gcc test_bst.c bst.c -o test
$ ./test
0 0
- [c]一个简单的二叉树
- 简单的一个二叉树的模版
- 一个简单的二叉树排序算法
- 一个简单的二叉查找树实现
- 一个简单的二叉搜索树实现
- 一个简单的二叉树实现
- c语言实现的简单二叉树
- 二叉树的简单C实现
- 哈弗曼二叉树的C简单实现
- C语言一个二叉树的实现
- C语言的一个二叉树
- C语言一个二叉树的实现
- Mark 一个类...一个很简单的二叉查找树...
- 【c++】简单实现二叉树
- 一个简单的排序二叉树的实现
- 求一个简单的二叉树的深度
- 一个简单的学生成绩管理程序(二叉树实现)
- 小猴子下落-一个简单的二叉树问题
- TensorFlow在图像识别中的应用
- PLSQL DDL Trigger
- JDK 1.7 java.io 源码学习之AutoCloseable接口和try-with-resources语法
- 自定义VIEW③Canvas
- Codeforces 165C Another Problem On Strings 尺取 Or 前缀和
- [c]一个简单的二叉树
- Python中的多线程MultiThread
- [C#]Log4net创建日志及简单扩展
- JSP九大内置对象及四个作用域
- 类的 静态成员 --- 静态成员变量 、 静态成员函数
- tensorflow学习笔记(三十三):ExponentialMovingAverage
- Python模块
- 抽象类
- JS笔记(一)