Binary Search Tree (二叉搜索树)

一直在看Data Structure and Algorithm Analysis 的原版,英文水平有限看的比较慢。代码功力就更不用说了,所以代码打的还没有看书快……已经在看优先队列了,AVL树还没有打完也是棒棒哒。这会儿就先从二叉树更新开始吧。

二叉树的结构什么的基本都知道,二叉搜索树就是比就简单的二叉树多了一个特性(property)——每个节点的左子叶内的key比节点的key小,而其右子叶的key比节点的key大。这个特性不是唯一的(比如左右子叶相对于其父节点的key值大小顺序可以颠倒),在学习时学会一种方法另一种也就没什么问题。从这一特性还可以知道,二叉树中最小的值一定在左子树中而最大的值一定在右子树中。在构建构造二叉搜索树时必须要设计节点元素大小的比较,为了学习的方便,这里使用int类型作为二叉搜索树节点中key值的类型。

我实现的代码github为:https://github.com/smallbal/DataStructure_in_C/tree/master/binary_search_tree

我这里将二叉树节点的声明和其操作(operation)声明放在一个头文件中(binary_search_tree.h):

 1 #ifndef _BINARY_SEARCH_TREE_H_
 2 #define _BINARY_SEARCH_TREE_H_
 3
 4 typedef enum FUNCTIONSTATE{
 5   FUNC_TRUE,
 6   FUNC_FAILURE,
 7   FUNC_BIGER,
 8   FUNC_SMALLER,
 9   FUNC_EQUAL
10 }FUNCTIONSTATE;
11
12 typedef int ElementType;
13 struct _node;
14 typedef struct _node *BinarySearchTree;
15 typedef BinarySearchTree Position;
16
17
18 typedef struct _node
19 {
20   ElementType element;
21   Position leftnode;
22   Position rightnode;
23 }Node;
24
25 BinarySearchTree MakeBinarySearchTree(const ElementType X);
26 FUNCTIONSTATE MakeBinarySearchTreeEmpty(BinarySearchTree T);
27 FUNCTIONSTATE IsEmpty(const BinarySearchTree T);
28 void InsertNode(ElementType X, BinarySearchTree *T);
29 //Position FindNode(const ElementType X, const BinarySearchTree T);
30 Position FindNode(const ElementType X, BinarySearchTree T);
31 FUNCTIONSTATE DeleteNode(ElementType X, BinarySearchTree T);
32 //ElementType FindMaxElement(const BinarySearchTree T);
33 //ElementType FindMinElement(const BinarySearchTree T);
34 ElementType FindMaxElement(BinarySearchTree T);
35 ElementType FindMinElement(BinarySearchTree T);
36 FUNCTIONSTATE Traversal(const BinarySearchTree T);//There are lots of problems
37 #endif

binary_search_tree.h

在最初设计时我想把所有未对树本身做改变的操作的参数都设为const型常量,但目前来说实现方面我还没有完全解决,这就是为什么在函数声明中有几个参数为const常量的注释。
最后一个Traversal()函数是对二叉搜索树的遍历,我对于按层遍历的编写仍然没有成功(并且我只想按层遍历,prefix, infix 和 postfix的遍历思想差不多),所以目前还没写出来,在参考过别的网站和书籍后再补充上去。

时间: 2024-10-10 19:25:22

Binary Search Tree (二叉搜索树)的相关文章

235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的LCA

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

[LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

[LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数

这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回上层,参数也需要最新的),就要用全局变量,而不是传参,其实这就是全局变量的定义. 不过如果是保存每层递归内的信息,就需要传参数. 本题显然是需要全局参数 /** * Definition for a binary tree node. * public class TreeNode { * int

1099 Build A Binary Search Tree [二叉搜索树/中序、层次遍历]

先用中序确定节点的值,再用层次遍历输出即可. 写的时候思维江化,一开始用指针建树... #include <bits/stdc++.h> using namespace std; #define maxn 105 struct Node { int index,left,right; }node[maxn]; int n,a[maxn],tree[maxn]; vector<int> ve[maxn],in,post; void inorder(int p) { if(node[p

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

132.Find Mode in Binary Search Tree(二分搜索树的众数)

题目: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. 给定具有重复项的二叉搜索树(BST),找到给定BST中的所有众数(最频繁出现的元素). Assume a BST is defined as follows: 假设BST定义如下: The left subtree of a node

hdu 3999 The order of a Tree (二叉搜索树)

1 /****************************************************************** 2 题目: The order of a Tree(hdu 3999) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=3999 4 题意: 给你一个序列建立一棵二叉搜索树 要你找出另外一个序 5 列,可以建立和原序列建立的二叉搜索树一样且这个序列 6 是字典序最小 7 算法: 二叉搜索树 8 思想: 对于一个

PAT Advanced 1099 Build A Binary Search Tree (30) [?叉查找树BST]

题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys gre

LeetCode Recover Binary Search Tree——二查搜索树中两个节点错误

Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树.最简单

编程算法 - 二叉搜索树(binary search tree) 代码(C)

二叉搜索树(binary search tree) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉搜索树(binary search tree)能够高效的进行插入, 查询, 删除某个元素, 时间复杂度O(logn). 简单的实现方法例如以下. 代码: /* * main.cpp * * Created on: 2014.7.20 * Author: spike */ /*eclipse cdt, gcc 4.8.1*/ #include <s