commit b03fb3bb347560f399287b1bdf08fc1457b12aac Author: DioBrakery <1325171109@qq.com> Date: Wed Aug 5 15:16:37 2020 +0800 'test' diff --git a/.vs/AVL树/v16/.suo b/.vs/AVL树/v16/.suo new file mode 100644 index 0000000..e825bde Binary files /dev/null and b/.vs/AVL树/v16/.suo differ diff --git a/.vs/AVL树/v16/Browse.VC.db b/.vs/AVL树/v16/Browse.VC.db new file mode 100644 index 0000000..dd04b1e Binary files /dev/null and b/.vs/AVL树/v16/Browse.VC.db differ diff --git a/.vs/AVL树/v16/ipch/AutoPCH/6b8e434d9b1ff070/AVL树.ipch b/.vs/AVL树/v16/ipch/AutoPCH/6b8e434d9b1ff070/AVL树.ipch new file mode 100644 index 0000000..6503b1e Binary files /dev/null and b/.vs/AVL树/v16/ipch/AutoPCH/6b8e434d9b1ff070/AVL树.ipch differ diff --git a/AVL树.cpp b/AVL树.cpp new file mode 100644 index 0000000..16aaabc --- /dev/null +++ b/AVL树.cpp @@ -0,0 +1,105 @@ +#include +#include + +typedef struct AVLNode* AVLTree; +struct AVLNode { //AVL树的结构:左子树,右子树,高度,数据 + AVLTree Left; + AVLTree Right; + int Height; + int Data; +}; + +int Max(int a, int b) { + return a > b ? a : b; +} + +int GetHeight(AVLTree T) { //递归计算树的高度 + int HL, HR; + if (T) { + HL = GetHeight(T->Left); + HR = GetHeight(T->Right); + return Max(HL, HR) + 1; + } + else + return 0; +} + + +AVLTree SingleLeftRotation(AVLTree A) { + AVLTree B = A->Left; + A->Left = B->Right; + B->Right = A; + A->Height = GetHeight(A); + B->Height = GetHeight(B); + return B; +} + +AVLTree SingleRightRotation(AVLTree A) { + AVLTree B = A->Right; + A->Right = B->Left; + B->Left = A; + A->Height = GetHeight(A); + B->Height = GetHeight(B); + + return B; +} + +AVLTree DoubleLeftRightRotation(AVLTree A) { + A->Left = SingleRightRotation(A->Left); + return SingleLeftRotation(A); +} + +AVLTree DoubleRightLeftRotation(AVLTree A) { + A->Right = SingleLeftRotation(A->Right); + return SingleRightRotation(A); +} + +AVLTree Insert(AVLTree T, int x) { + if (!T) { + T = (AVLTree)malloc(sizeof(struct AVLNode)); + T->Data = x; + T->Left = NULL; + T->Right = NULL; + T->Height = 1; + } + else if (x < T->Data) { + T->Left = Insert(T->Left, x); + if (GetHeight(T->Left) - GetHeight(T->Right) == 2) { //注意:这里还没更新树高,不能用T->Left->Height之类 + if (x < T->Data) + T = SingleLeftRotation(T); + else if (x > T->Data) + T = DoubleLeftRightRotation(T); + } + } + else if (x > T->Data) { + T->Right=Insert(T->Right, x); + if (GetHeight(T->Left) - GetHeight(T->Right) == -2) + if (x > T->Right->Data) + T = SingleRightRotation(T); + else + T = DoubleRightLeftRotation(T); + } + T->Height = GetHeight(T); + return T; +} + +void PreorderTravelsal(AVLTree BT) { + if (BT) { + printf("%d ", BT->Data); + PreorderTravelsal(BT->Left); + PreorderTravelsal(BT->Right); + } +} + +int main(void) { + int n,number, i; + AVLTree T = NULL; + scanf_s("%d", &n); + for (i = 0; i < n; i++) { + scanf_s("%d", &number); + T=Insert(T, number); + PreorderTravelsal(T); + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/AVL树.sln b/AVL树.sln new file mode 100644 index 0000000..64a590f --- /dev/null +++ b/AVL树.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29411.108 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "AVL树", "AVL树.vcxproj", "{508852FE-4295-442A-9E1C-94351356E239}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {508852FE-4295-442A-9E1C-94351356E239}.Debug|x64.ActiveCfg = Debug|x64 + {508852FE-4295-442A-9E1C-94351356E239}.Debug|x64.Build.0 = Debug|x64 + {508852FE-4295-442A-9E1C-94351356E239}.Debug|x86.ActiveCfg = Debug|Win32 + {508852FE-4295-442A-9E1C-94351356E239}.Debug|x86.Build.0 = Debug|Win32 + {508852FE-4295-442A-9E1C-94351356E239}.Release|x64.ActiveCfg = Release|x64 + {508852FE-4295-442A-9E1C-94351356E239}.Release|x64.Build.0 = Release|x64 + {508852FE-4295-442A-9E1C-94351356E239}.Release|x86.ActiveCfg = Release|Win32 + {508852FE-4295-442A-9E1C-94351356E239}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0A702C47-D3D6-4610-96F7-A6A711E54015} + EndGlobalSection +EndGlobal diff --git a/AVL树.vcxproj b/AVL树.vcxproj new file mode 100644 index 0000000..1e6e6ad --- /dev/null +++ b/AVL树.vcxproj @@ -0,0 +1,159 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {508852FE-4295-442A-9E1C-94351356E239} + Win32Proj + AVL树 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/AVL树.vcxproj.filters b/AVL树.vcxproj.filters new file mode 100644 index 0000000..84636d6 --- /dev/null +++ b/AVL树.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file diff --git a/AVL树.vcxproj.user b/AVL树.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/AVL树.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Debug/AVL树.exe b/Debug/AVL树.exe new file mode 100644 index 0000000..b931df6 Binary files /dev/null and b/Debug/AVL树.exe differ diff --git a/Debug/AVL树.ilk b/Debug/AVL树.ilk new file mode 100644 index 0000000..0227c03 Binary files /dev/null and b/Debug/AVL树.ilk differ diff --git a/Debug/AVL树.obj b/Debug/AVL树.obj new file mode 100644 index 0000000..f94b1cb Binary files /dev/null and b/Debug/AVL树.obj differ diff --git a/Debug/AVL树.pdb b/Debug/AVL树.pdb new file mode 100644 index 0000000..308fc96 Binary files /dev/null and b/Debug/AVL树.pdb differ diff --git a/Debug/AVL树.tlog/AVL树.lastbuildstate b/Debug/AVL树.tlog/AVL树.lastbuildstate new file mode 100644 index 0000000..f63960c --- /dev/null +++ b/Debug/AVL树.tlog/AVL树.lastbuildstate @@ -0,0 +1,2 @@ +#TargetFrameworkVersion=v4.0:PlatformToolSet=v142:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=10.0 +Debug|Win32|C:\Users\13251\source\repos\AVL树\| diff --git a/Debug/AVL树.tlog/CL.command.1.tlog b/Debug/AVL树.tlog/CL.command.1.tlog new file mode 100644 index 0000000..0ccbc30 Binary files /dev/null and b/Debug/AVL树.tlog/CL.command.1.tlog differ diff --git a/Debug/AVL树.tlog/CL.read.1.tlog b/Debug/AVL树.tlog/CL.read.1.tlog new file mode 100644 index 0000000..6b26ace Binary files /dev/null and b/Debug/AVL树.tlog/CL.read.1.tlog differ diff --git a/Debug/AVL树.tlog/CL.write.1.tlog b/Debug/AVL树.tlog/CL.write.1.tlog new file mode 100644 index 0000000..0f4576e Binary files /dev/null and b/Debug/AVL树.tlog/CL.write.1.tlog differ diff --git a/Debug/AVL树.tlog/link.command.1.tlog b/Debug/AVL树.tlog/link.command.1.tlog new file mode 100644 index 0000000..9303f63 Binary files /dev/null and b/Debug/AVL树.tlog/link.command.1.tlog differ diff --git a/Debug/AVL树.tlog/link.read.1.tlog b/Debug/AVL树.tlog/link.read.1.tlog new file mode 100644 index 0000000..d89ea01 Binary files /dev/null and b/Debug/AVL树.tlog/link.read.1.tlog differ diff --git a/Debug/AVL树.tlog/link.write.1.tlog b/Debug/AVL树.tlog/link.write.1.tlog new file mode 100644 index 0000000..2da665e Binary files /dev/null and b/Debug/AVL树.tlog/link.write.1.tlog differ diff --git a/Debug/vc142.idb b/Debug/vc142.idb new file mode 100644 index 0000000..d962688 Binary files /dev/null and b/Debug/vc142.idb differ diff --git a/Debug/vc142.pdb b/Debug/vc142.pdb new file mode 100644 index 0000000..7899203 Binary files /dev/null and b/Debug/vc142.pdb differ diff --git a/sh.exe.stackdump b/sh.exe.stackdump new file mode 100644 index 0000000..096b514 --- /dev/null +++ b/sh.exe.stackdump @@ -0,0 +1,11 @@ +Stack trace: +Frame Function Args +00800000010 0018006392E (00180279F10, 0018026AFD1, 00800000010, 000FFFFBA00) +00800000010 0018004973A (00100002000, 000FFFFCBA0, 00180350090, 00000000002) +00800000010 00180049772 (00000000002, 001803503A0, 00800000010, 00000000008) +00800000010 0018005CBDD (000FFFFCC94, 000FFFFCBD0, 000FFFFCC68, 000FFFFCC94) +000FFFFCBAC 0018005CD4F (675C645C625C2F5C, 655C2E5C745C695C, 6F5C635C755C645C, 2E5C725C655C645C) +000FFFFCCE0 00180049EE8 (00000000000, 00000000000, 00000000000, 00000000000) +000FFFFFFF0 00180048846 (00000000000, 00000000000, 00000000000, 00000000000) +000FFFFFFF0 001800488F4 (00000000000, 00000000000, 00000000000, 00000000000) +End of stack trace