-
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
fc49545
commit c2d91f2
Showing
14 changed files
with
203 additions
and
49 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
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 | ||
*/ |
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 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
5 changes: 2 additions & 3 deletions
5
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,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 |
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,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 | ||
|
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/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 |
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 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
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