-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhaojiedi
committed
Apr 17, 2022
1 parent
9763946
commit e7c48ad
Showing
19 changed files
with
333 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
// bstree_link_base.c | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "bitree_link_base.h" | ||
|
||
|
||
// 判断树是否为空 | ||
int TreeIsEmpty(BsTree bt) { | ||
if (bt == NULL) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
// 创建树 | ||
BsTree CreateBsTree() { | ||
BsTree bt = (BsTree) malloc(sizeof(struct TreeNode)); | ||
return bt; | ||
} | ||
|
||
Position FindByRecursive(ElementType item, BsTree bst) { | ||
if (!bst) { | ||
return NULL; | ||
} | ||
if (item == bst->Data) { | ||
return bst; | ||
} | ||
if (item > bst->Data) { | ||
return FindByRecursive(item, bst->Right); | ||
} | ||
if (item < bst->Data) { | ||
return FindByRecursive(item, bst->Left); | ||
} | ||
} | ||
|
||
Position FindByIterate(ElementType item, BsTree bst) { | ||
|
||
BsTree pst = bst; | ||
while (pst) { | ||
if (pst->Data == item) { | ||
return pst; | ||
} | ||
if (item > pst->Data) { | ||
pst = pst->Right; | ||
} else { | ||
pst = pst->Left; | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
Position FindMax(BsTree bst) { | ||
BsTree pst = bst; | ||
while (pst && pst->Right) { | ||
pst = pst->Right; | ||
} | ||
return pst; | ||
|
||
} | ||
|
||
Position FindMin(BsTree bst) { | ||
BsTree pst = bst; | ||
while (pst && pst->Left) { | ||
pst = pst->Left; | ||
} | ||
return pst; | ||
|
||
} | ||
|
||
// 前序递归 | ||
void PreOrderTraversalByRecursive(BsTree bt) { | ||
if (bt) { | ||
printf("%d ", bt->Data); | ||
PreOrderTraversalByRecursive(bt->Left); | ||
PreOrderTraversalByRecursive(bt->Right); | ||
} | ||
} | ||
|
||
BsTree InsertByIterate(ElementType item, BsTree bst) { | ||
// BsTree pst = bst ; | ||
BsTree parent = bst; | ||
BsTree current = bst; | ||
while (current) { | ||
parent = current ; | ||
if (item > current->Data) { | ||
current = current->Right; | ||
|
||
} else { | ||
current = current->Left; | ||
} | ||
} | ||
|
||
BsTree node = malloc(sizeof(struct TreeNode)); | ||
node->Data = item; | ||
node->Left = NULL; | ||
node->Right = NULL; | ||
if (item > parent->Data) { | ||
parent->Right = node; | ||
} else { | ||
parent->Left = node; | ||
} | ||
|
||
return bst; | ||
|
||
} | ||
|
||
BsTree InsertByRecursive(ElementType item, BsTree bst) { | ||
if ( ! bst ){ | ||
BsTree node = malloc(sizeof(struct TreeNode)); | ||
node->Data = item; | ||
node->Left = node->Right = NULL; | ||
return node; | ||
} | ||
if (item > bst->Data ){ | ||
bst->Right = InsertByRecursive(item,bst->Right); | ||
}else{ | ||
bst->Left = InsertByRecursive(item,bst->Left); | ||
} | ||
return bst ; | ||
// == 这个咱不考虑 | ||
} | ||
|
||
BsTree DeleteByRecursive(ElementType item, BsTree bst) { | ||
if ( !bst ){ | ||
printf("找不到删除元素"); | ||
return NULL; | ||
} | ||
if (item > bst->Data ){ | ||
bst->Right = DeleteByRecursive(item,bst->Right); | ||
}else if(item < bst->Data ){ | ||
bst->Left = DeleteByRecursive(item,bst->Left); | ||
}else{ | ||
// 左节点为空的删除 | ||
if (bst->Left == NULL ){ | ||
bst = bst->Right; | ||
}else if (bst->Right ==NULL){ // 右节点为空。 | ||
bst = bst->Left ; | ||
}else{ | ||
// 左右都有的。 | ||
Position tmp = FindMin(bst->Right); | ||
bst->Data = tmp->Data; | ||
bst->Right = DeleteByRecursive(bst->Data,bst->Right); | ||
} | ||
} | ||
return bst ; | ||
} | ||
|
||
BsTree CreateDemoTree() { | ||
/* | ||
40 | ||
20 60 | ||
10 30 50 | ||
* */ | ||
BsTree bt1 = CreateBsTree(); | ||
bt1->Data = 40; | ||
BsTree bt2 = CreateBsTree(); | ||
bt2->Data = 20; | ||
BsTree bt3 = CreateBsTree(); | ||
bt3->Data = 60; | ||
BsTree bt4 = CreateBsTree(); | ||
bt4->Data = 10; | ||
BsTree bt5 = CreateBsTree(); | ||
bt5->Data = 30; | ||
BsTree bt6 = CreateBsTree(); | ||
bt6->Data = 50; | ||
|
||
bt1->Left = bt2; | ||
bt1->Right = bt3; | ||
|
||
bt2->Left = bt4; | ||
bt2->Right = bt5; | ||
|
||
bt3->Left = bt6; | ||
return bt1; | ||
} | ||
|
||
int mainByIterate() { | ||
Position pos; | ||
BsTree bst = CreateDemoTree(); | ||
printf("PreOrderTraversal\n"); | ||
PreOrderTraversalByRecursive(bst); | ||
printf("\n"); | ||
|
||
pos = FindByIterate(20, bst); | ||
PreOrderTraversalByRecursive(pos); | ||
printf("\n"); | ||
|
||
pos = FindMax(bst); | ||
PreOrderTraversalByRecursive(pos); | ||
printf("\n"); | ||
|
||
pos = FindMin(bst); | ||
PreOrderTraversalByRecursive(pos); | ||
printf("\n"); | ||
|
||
|
||
// bst = InsertByIterate(33, bst); | ||
// PreOrderTraversalByRecursive(bst); | ||
// printf("\n"); | ||
|
||
// bst = DeleteByRecursive(70, bst); | ||
// PreOrderTraversalByRecursive(bst); | ||
// printf("\n"); | ||
// bst = DeleteByRecursive(80, bst); | ||
// PreOrderTraversalByRecursive(bst); | ||
// printf("\n"); | ||
|
||
bst = DeleteByRecursive(20, bst); | ||
PreOrderTraversalByRecursive(bst); | ||
printf("\n"); | ||
} | ||
int mainByRecursive() { | ||
Position pos; | ||
BsTree bst = CreateDemoTree(); | ||
printf("PreOrderTraversal\n"); | ||
PreOrderTraversalByRecursive(bst); | ||
printf("\n"); | ||
|
||
pos = FindByRecursive(20, bst); | ||
PreOrderTraversalByRecursive(pos); | ||
printf("\n"); | ||
|
||
|
||
|
||
pos = FindMax(bst); | ||
PreOrderTraversalByRecursive(pos); | ||
printf("\n"); | ||
|
||
pos = FindMin(bst); | ||
PreOrderTraversalByRecursive(pos); | ||
printf("\n"); | ||
|
||
bst = InsertByRecursive(70, bst); | ||
PreOrderTraversalByRecursive(bst); | ||
printf("\n"); | ||
|
||
|
||
|
||
bst = DeleteByRecursive(40, bst); | ||
PreOrderTraversalByRecursive(bst); | ||
printf("\n"); | ||
|
||
|
||
|
||
} | ||
|
||
int main(){ | ||
mainByRecursive(); | ||
// mainByIterate() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file removed
BIN
-11.7 KB
source/code/cmake-build-debug/CMakeFiles/code.dir/bitree_link_base.c.o
Binary file not shown.
Binary file added
BIN
+9.25 KB
source/code/cmake-build-debug/CMakeFiles/code.dir/bstree_link_base.c.o
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
source/code/cmake-build-debug/CMakeFiles/code.dir/cmake_clean.cmake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
source/code/cmake-build-debug/CMakeFiles/code.dir/depend.internal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
# CMAKE generated file: DO NOT EDIT! | ||
# Generated by "Unix Makefiles" Generator, CMake Version 3.15 | ||
|
||
CMakeFiles/code.dir/bitree_link_base.c.o | ||
/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bitree_link_base.c | ||
CMakeFiles/code.dir/bstree_link_base.c.o | ||
/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bitree_link_base.h | ||
/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/queue_link_base_for_tree.c | ||
/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/stack_link_base_for_tree.c | ||
/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bstree_link_base.c |
6 changes: 2 additions & 4 deletions
6
source/code/cmake-build-debug/CMakeFiles/code.dir/depend.make
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
# CMAKE generated file: DO NOT EDIT! | ||
# Generated by "Unix Makefiles" Generator, CMake Version 3.15 | ||
|
||
CMakeFiles/code.dir/bitree_link_base.c.o: ../bitree_link_base.c | ||
CMakeFiles/code.dir/bitree_link_base.c.o: ../bitree_link_base.h | ||
CMakeFiles/code.dir/bitree_link_base.c.o: ../queue_link_base_for_tree.c | ||
CMakeFiles/code.dir/bitree_link_base.c.o: ../stack_link_base_for_tree.c | ||
CMakeFiles/code.dir/bstree_link_base.c.o: ../bitree_link_base.h | ||
CMakeFiles/code.dir/bstree_link_base.c.o: ../bstree_link_base.c | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/Library/Developer/CommandLineTools/usr/bin/cc -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk -mmacosx-version-min=12.2 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/code.dir/bitree_link_base.c.o -o code | ||
/Library/Developer/CommandLineTools/usr/bin/cc -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk -mmacosx-version-min=12.2 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/code.dir/bstree_link_base.c.o -o code |
Oops, something went wrong.