mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-06-16 23:17:21 +08:00
更新树
This commit is contained in:
@@ -141,6 +141,7 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="head.h" />
|
||||
<ClCompile Include="main.c" />
|
||||
<ClCompile Include="sequence_tree.h" />
|
||||
<ClCompile Include="sequence_stack.h" />
|
||||
<ClCompile Include="sequence_tree.cpp" />
|
||||
<ClCompile Include="thread_tree.h" />
|
||||
|
||||
@@ -118,4 +118,73 @@ int LevelorderTraversalLinkTree(LinkTree tree, int(*visit)(LinkTree elem)) {
|
||||
EnterSequenceQueue(&queue, p->rchild);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
// 二叉排序树遍历查找
|
||||
LinkTreeNode* TraversalSearchBST(LinkTree tree, element_type elem) {
|
||||
while (tree != NULL && elem != tree->data) {
|
||||
if (elem < tree->data) {
|
||||
tree = tree->lchild;
|
||||
}
|
||||
else {
|
||||
tree = tree->rchild;
|
||||
}
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
|
||||
// 二叉排序树递归查找
|
||||
LinkTreeNode* RecursiveSearchBST(LinkTree tree, element_type elem) {
|
||||
if (tree == NULL) {
|
||||
// 为空树
|
||||
return NULL;
|
||||
}
|
||||
if (elem = tree->data) {
|
||||
return tree;
|
||||
}
|
||||
else if (elem < tree->data) {
|
||||
return RecursiveSearchBST(tree->lchild, elem);
|
||||
}
|
||||
else {
|
||||
return RecursiveSearchBST(tree->rchild, elem);
|
||||
}
|
||||
}
|
||||
|
||||
// 二叉排序树递归插入
|
||||
int InsertBST(LinkTree tree, element_type elem) {
|
||||
if (tree == NULL) {
|
||||
tree = (LinkTree)malloc(sizeof(LinkTree));
|
||||
if (tree) {
|
||||
tree->data = elem;
|
||||
tree->lchild = tree->rchild = NULL;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("BSTInsert:插入分配空间失败!");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
// 关键词重复
|
||||
else if (elem == tree->data) {
|
||||
printf("BSTInsert:关键字重复!");
|
||||
return 1;
|
||||
}
|
||||
else if (elem < tree->data) {
|
||||
return InsertBST(tree->lchild, elem);
|
||||
}
|
||||
else {
|
||||
return InsertBST(tree->rchild, elem);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据关键字建立二叉排序树
|
||||
int CreateBST(LinkTree tree, element_type elem[], int n) {
|
||||
tree = NULL;
|
||||
int i = 0;
|
||||
while (true)
|
||||
{
|
||||
InsertBST(tree, elem[i]);
|
||||
i++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 定义一个顺序树数组
|
||||
typedef int SequenceTree[MAXSIZE];
|
||||
Reference in New Issue
Block a user