我是靠谱客的博主 细心盼望,这篇文章主要介绍二叉排序树(二叉搜索树)的算法实现二叉排序树(二叉搜索树)的算法实现,现在分享给大家,希望可以做个参考。

二叉排序树(二叉搜索树)的算法实现

二叉排序树有以下五个特征:
1.左子树非空,左子树上的所有结点的值都小于其根上的值
2.右子树非空,右子树上的所有结点的值都大于其跟上的值
3.按照中序遍历的方法可以得到一个有序的序列(从小到大)
4.二叉排序树上的任何一棵子树都是这样有序的
5.二叉排序树的查询效率是最高的
以下是二叉排序树的构造,插入结点,遍历(中序)的代码部分。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream> #include <string.h> using namespace std; typedef struct data{ int first; char second[10]; }DATA,*LPDATA; typedef struct binaryTreeNode { DATA element; struct binaryTreeNode* Lchild; struct binaryTreeNode* Rchild; }NODE,*LPNODE; typedef struct binarySearchTree { struct binaryTreeNode* root; int treeSize; }BTREE,*LPBTREE; //创建一个结点 LPNODE creatNode(DATA element) { LPNODE newNode = (LPNODE)malloc(sizeof(NODE)); newNode->element = element; newNode->Lchild = NULL; newNode->Rchild = NULL; return newNode; } //创建一棵树 LPBTREE creatBinarySearchTree() { LPBTREE tree = (LPBTREE)malloc(sizeof(BTREE)); tree->root = NULL; tree->treeSize = 0; //树的容量 return tree; } int size(LPBTREE tree) { return tree->treeSize; } int empty(LPBTREE tree) { if (tree->treeSize == 0) return 1; else return 0; } //插入结点 void insertNode(LPBTREE tree, DATA element) { LPNODE moveNode = tree->root; LPNODE moveParent = NULL; //找到新结点的插入位置 while (moveNode != NULL) { moveParent = moveNode; if (element.first < moveNode->element.first) //插入的数据比当前结点小 { moveNode = moveNode->Lchild; } else if (element.first>moveNode->element.first) //插入的数据比当前结点大 { moveNode = moveNode->Rchild; } else // 插入的数据和当前结点的大小一致 (用最新的相同数据把原来的数据覆盖) { strcpy(moveNode->element.second, element.second); //比较的是关键字,内容是关键词和数据,就是上面char类型数组中的内容【关键字,内容】 return; } } //插入新的结点 LPNODE newNode = creatNode(element); //插入的位置是移动结点父结点的位置后(左或右孩子的位置) if (tree->root == NULL) { tree->root = newNode; } else { if (moveParent->element.first > element.first) { moveParent->Lchild = newNode; } else { moveParent->Rchild = newNode; } } } //中序遍历 void printTree(LPNODE root) { if (root != NULL) { printTree(root->Lchild); cout << root->element.first << "" << root->element.second << endl; printTree(root->Rchild); } } void printSearchTree(LPBTREE tree) { printTree(tree->root); } void main() { DATA Data[4] = { 23, "小红", 58, "小明", 67, "小亮", 88, "小张" }; LPBTREE tree = creatBinarySearchTree(); for (int i = 0; i < 4; i++) { insertNode(tree,Data[i]); } printSearchTree(tree); system("pause"); }

主要思路:
1.构造数据类型的结构体DATA,构造结点类型的结构体,构造二叉排序树类型的结构体。
他们之间的关系:二叉排序树的结构体确定数的根,结点类型的结构体是为了向书中添加结点,数据类型的结构体为了表示存储的数据

2 设计创建新结点的函数,目的是为了后面方便插入新的结点

3 设计创建二叉排序树的函数,目的是创建二叉排序树的根来确定一棵二叉排序树

4 设计结点插入函数,这时候首先得先找结点的插入位置,方法就是让新添加进来的结点的键值和二叉排序树中结点的键值进行比较,小的是左孩子,大的是右孩子。当找到位置了以后呢,就需要插入新的结点,插入新的结点是插入在所要插入位置的父亲结点的左孩子或右孩子上,这时候就需要设置一个标记量来记录之前移动指针的位置,这样能方便的找到目的父节点的位置。找到位置以后呢,还是判断大小来选择插入的是左孩子还是右孩。最后完成插入。

5 中序输出,利用递归中序输出的方法输出出来。

最后

以上就是细心盼望最近收集整理的关于二叉排序树(二叉搜索树)的算法实现二叉排序树(二叉搜索树)的算法实现的全部内容,更多相关二叉排序树(二叉搜索树)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部