Skip to content

Commit

Permalink
cm
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojiedi committed Apr 17, 2022
1 parent fc49545 commit c2d91f2
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 49 deletions.
2 changes: 1 addition & 1 deletion source/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ project(code C)
set(CMAKE_C_STANDARD 99)

#add_executable(code list_base.c)
add_executable(code bstree_link_base.c)
add_executable(code avl_tree.c)
151 changes: 151 additions & 0 deletions source/code/avl_tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#include <stdio.h>
#include <stdlib.h>

#define ElementType int
typedef struct AVLNode *AVLTree;
struct AVLNode {
ElementType Data;
AVLTree Left;
AVLTree Right;
int Height;
};

AVLTree CreateAvlTree(){
AVLTree t = (AVLTree)malloc(sizeof(struct AVLNode));
return t ;
}

int Max(int a, int b) {
if (a > b) {
return a;
}
return b;
}

int GetHeight(AVLTree t) ;
void UpdateHeight(AVLTree t) {
t->Height = Max(GetHeight(t->Left), GetHeight(t->Right)) + 1;
return ;
}


int GetHeight(AVLTree t) {
int rightHeight;
int leftHeight;
if (!t) {
return 0;
}
UpdateHeight(t);
return t->Height;
}

AVLTree RR(AVLTree a) {
AVLTree b = a->Right;
a->Right = b->Left;
b->Left = a;

UpdateHeight(a);
UpdateHeight(b);
return b;
}

AVLTree LL(AVLTree a) {
AVLTree b = a->Left;
a->Left = b->Right;
b->Right = a;

UpdateHeight(a);
UpdateHeight(b);
return b;
}

AVLTree LR(AVLTree a) {
a->Left = RR(a->Left);
return LL(a);
}
AVLTree RL(AVLTree a) {
a->Right = LL(a->Right);
return RR(a);
}

AVLTree Insert(AVLTree t, ElementType item) {
if (!t) {
// 给叶子节点添加一个子节点的时候,走到这个部分。
AVLTree node = (AVLTree)malloc(sizeof(struct AVLNode));
node->Data = item;
node->Left = NULL;
node->Right = NULL;
node->Height = 1;
return node;
}
if (item > t->Data) {
t->Right = Insert(t->Right, item);
if (GetHeight(t->Right) - GetHeight(t->Left) == 2) {
if (item > t->Right->Data) {
t = RR(t);
} else {
t = RL(t);
}
}
} else {
t->Left = Insert(t->Left, item);
if (GetHeight(t->Left) - GetHeight(t->Right) == 2) {
if (item < t->Left->Data) {
t = LL(t);
} else {
t = LR(t);
}
}
}
t->Height = Max(GetHeight(t->Left), GetHeight(t->Right)) + 1;
return t;
}
void PreOrderTraversalByRecursive(AVLTree bt){
if (bt){
printf("%d ", bt->Data);
PreOrderTraversalByRecursive(bt->Left);
PreOrderTraversalByRecursive(bt->Right);
}
}

int main(){
AVLTree avlTree= NULL ;
//avlTree = CreateAvlTree();
avlTree= Insert(avlTree,1);
avlTree = Insert(avlTree,2) ;
avlTree= Insert(avlTree,3) ;
PreOrderTraversalByRecursive(avlTree);
printf("\n");
avlTree= Insert(avlTree,7) ;
PreOrderTraversalByRecursive(avlTree);
printf("\n");
avlTree= Insert(avlTree,6) ;
avlTree= Insert(avlTree,5) ;
avlTree= Insert(avlTree,0) ;
PreOrderTraversalByRecursive(avlTree);
}
/*
* =========================================================
one two three three_adjust(RR)
1 1 1 2
2 2 1 3
3
* =========================================================
four five five_adjust(RL)
2 2 2
1 3 1 3 1 6
7 7 3 7
6
=========================================================
six six_adjust(RL)
2 3
1 6 2 6
3 7 1 5 7
5
=========================================================
seven seven_adjust(LL)
3 3
2 6 1 6
1 5 7 0 2 5 7
0
*/
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@

#IncludeRegexTransform:

/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bitree_link_base.h
/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/avl_tree.c
stdio.h
-
stdlib.h
-

/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bstree_link_base.c
stdio.h
-
stdlib.h
-
bitree_link_base.h
/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bitree_link_base.h

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(CMAKE_DEPENDS_LANGUAGES
)
# The set of files for implicit dependencies of each language:
set(CMAKE_DEPENDS_CHECK_C
"/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bstree_link_base.c" "/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/cmake-build-debug/CMakeFiles/code.dir/bstree_link_base.c.o"
"/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/avl_tree.c" "/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/cmake-build-debug/CMakeFiles/code.dir/avl_tree.c.o"
)
set(CMAKE_C_COMPILER_ID "AppleClang")

Expand Down
Binary file not shown.
24 changes: 12 additions & 12 deletions source/code/cmake-build-debug/CMakeFiles/code.dir/build.make
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@ include CMakeFiles/code.dir/progress.make
# Include the compile flags for this target's objects.
include CMakeFiles/code.dir/flags.make

CMakeFiles/code.dir/bstree_link_base.c.o: CMakeFiles/code.dir/flags.make
CMakeFiles/code.dir/bstree_link_base.c.o: ../bstree_link_base.c
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building C object CMakeFiles/code.dir/bstree_link_base.c.o"
/Library/Developer/CommandLineTools/usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -o CMakeFiles/code.dir/bstree_link_base.c.o -c /Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bstree_link_base.c
CMakeFiles/code.dir/avl_tree.c.o: CMakeFiles/code.dir/flags.make
CMakeFiles/code.dir/avl_tree.c.o: ../avl_tree.c
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building C object CMakeFiles/code.dir/avl_tree.c.o"
/Library/Developer/CommandLineTools/usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -o CMakeFiles/code.dir/avl_tree.c.o -c /Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/avl_tree.c

CMakeFiles/code.dir/bstree_link_base.c.i: cmake_force
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing C source to CMakeFiles/code.dir/bstree_link_base.c.i"
/Library/Developer/CommandLineTools/usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -E /Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bstree_link_base.c > CMakeFiles/code.dir/bstree_link_base.c.i
CMakeFiles/code.dir/avl_tree.c.i: cmake_force
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing C source to CMakeFiles/code.dir/avl_tree.c.i"
/Library/Developer/CommandLineTools/usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -E /Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/avl_tree.c > CMakeFiles/code.dir/avl_tree.c.i

CMakeFiles/code.dir/bstree_link_base.c.s: cmake_force
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/code.dir/bstree_link_base.c.s"
/Library/Developer/CommandLineTools/usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bstree_link_base.c -o CMakeFiles/code.dir/bstree_link_base.c.s
CMakeFiles/code.dir/avl_tree.c.s: cmake_force
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/code.dir/avl_tree.c.s"
/Library/Developer/CommandLineTools/usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/avl_tree.c -o CMakeFiles/code.dir/avl_tree.c.s

# Object files for target code
code_OBJECTS = \
"CMakeFiles/code.dir/bstree_link_base.c.o"
"CMakeFiles/code.dir/avl_tree.c.o"

# External object files for target code
code_EXTERNAL_OBJECTS =

code: CMakeFiles/code.dir/bstree_link_base.c.o
code: CMakeFiles/code.dir/avl_tree.c.o
code: CMakeFiles/code.dir/build.make
code: CMakeFiles/code.dir/link.txt
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking C executable code"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
file(REMOVE_RECURSE
"CMakeFiles/code.dir/bstree_link_base.c.o"
"CMakeFiles/code.dir/avl_tree.c.o"
"code"
"code.pdb"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.15

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/bstree_link_base.c
CMakeFiles/code.dir/avl_tree.c.o
/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/avl_tree.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.15

CMakeFiles/code.dir/bstree_link_base.c.o: ../bitree_link_base.h
CMakeFiles/code.dir/bstree_link_base.c.o: ../bstree_link_base.c
CMakeFiles/code.dir/avl_tree.c.o: ../avl_tree.c

2 changes: 1 addition & 1 deletion source/code/cmake-build-debug/CMakeFiles/code.dir/link.txt
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/bstree_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/avl_tree.c.o -o code
36 changes: 18 additions & 18 deletions source/code/cmake-build-debug/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,32 +123,32 @@ code/fast:
$(MAKE) -f CMakeFiles/code.dir/build.make CMakeFiles/code.dir/build
.PHONY : code/fast

bstree_link_base.o: bstree_link_base.c.o
avl_tree.o: avl_tree.c.o

.PHONY : bstree_link_base.o
.PHONY : avl_tree.o

# target to build an object file
bstree_link_base.c.o:
$(MAKE) -f CMakeFiles/code.dir/build.make CMakeFiles/code.dir/bstree_link_base.c.o
.PHONY : bstree_link_base.c.o
avl_tree.c.o:
$(MAKE) -f CMakeFiles/code.dir/build.make CMakeFiles/code.dir/avl_tree.c.o
.PHONY : avl_tree.c.o

bstree_link_base.i: bstree_link_base.c.i
avl_tree.i: avl_tree.c.i

.PHONY : bstree_link_base.i
.PHONY : avl_tree.i

# target to preprocess a source file
bstree_link_base.c.i:
$(MAKE) -f CMakeFiles/code.dir/build.make CMakeFiles/code.dir/bstree_link_base.c.i
.PHONY : bstree_link_base.c.i
avl_tree.c.i:
$(MAKE) -f CMakeFiles/code.dir/build.make CMakeFiles/code.dir/avl_tree.c.i
.PHONY : avl_tree.c.i

bstree_link_base.s: bstree_link_base.c.s
avl_tree.s: avl_tree.c.s

.PHONY : bstree_link_base.s
.PHONY : avl_tree.s

# target to generate assembly for a file
bstree_link_base.c.s:
$(MAKE) -f CMakeFiles/code.dir/build.make CMakeFiles/code.dir/bstree_link_base.c.s
.PHONY : bstree_link_base.c.s
avl_tree.c.s:
$(MAKE) -f CMakeFiles/code.dir/build.make CMakeFiles/code.dir/avl_tree.c.s
.PHONY : avl_tree.c.s

# Help Target
help:
Expand All @@ -159,9 +159,9 @@ help:
@echo "... rebuild_cache"
@echo "... edit_cache"
@echo "... code"
@echo "... bstree_link_base.o"
@echo "... bstree_link_base.i"
@echo "... bstree_link_base.s"
@echo "... avl_tree.o"
@echo "... avl_tree.i"
@echo "... avl_tree.s"
.PHONY : help


Expand Down
Binary file modified source/code/cmake-build-debug/code
Binary file not shown.
2 changes: 1 addition & 1 deletion source/code/cmake-build-debug/code.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
</MakeCommands>
</Target>
</Build>
<Unit filename="/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/bstree_link_base.c">
<Unit filename="/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/avl_tree.c">
<Option target="code"/>
</Unit>
<Unit filename="/Users/bytedance/github/zhaojiedi1992/My_Study_DataStruct/source/code/CMakeLists.txt">
Expand Down
13 changes: 13 additions & 0 deletions source/基本数据结构/06-二叉搜索树和平衡二叉树.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@
:linenos:

.. literalinclude:: ../code/bstree_link_base.c
:encoding: utf-8
:language: c
:linenos:

平衡二叉树(AVL)
==========================================
平衡因子绝对值小于等于1的搜索树才是平衡二叉树。

平衡因子= 左树高度-右树高度。

平衡二叉树实现
==========================================
.. literalinclude:: ../code/avl_tree.c
:encoding: utf-8
:language: c
:linenos:

0 comments on commit c2d91f2

Please sign in to comment.