From 449c12e664976634447bc159f6d941f637288b72 Mon Sep 17 00:00:00 2001 From: dunanshan Date: Tue, 15 Jun 2021 23:34:05 +0800 Subject: [PATCH 1/5] fix preOrder and postOrder; and add function: show tree. --- AVLTree/AVLTree/AVLTree.h | 41 +++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/AVLTree/AVLTree/AVLTree.h b/AVLTree/AVLTree/AVLTree.h index c2d5dc1..68a13b5 100644 --- a/AVLTree/AVLTree/AVLTree.h +++ b/AVLTree/AVLTree/AVLTree.h @@ -25,6 +25,8 @@ class AVLTree AVLTree(); //构造函数 ~AVLTree(); //析构函数 + void show(); // tree print + void preOrder(); //前序遍历AVL树 void InOrder(); //中序遍历AVL树 void postOrder(); //后序遍历AVL树 @@ -46,6 +48,8 @@ class AVLTree AVLTreeNode* root; //AVL树的根节点 private: + void show(AVLTreeNode* pnode, size_t indent) const; // tree print + void preOrder(AVLTreeNode* pnode) const; void inOrder(AVLTreeNode* pnode) const; void postOrder(AVLTreeNode* pnode) const; @@ -328,7 +332,8 @@ AVLTreeNode* AVLTree::remove(AVLTreeNode* & pnode, T key) } else if (key > pnode->key) //要删除的节点比当前节点大,则在右子树进行删除 { - pnode->rchild = remove(pnode->rchild, key); + pnode->rchild = remove(pnode->rchild, key); + if (height(pnode->lchild) - height(pnode->rchild) == 2) //删除右子树节点导致不平衡:相当于情况二或情况四 { if (height(pnode->lchild->rchild)>height(pnode->lchild->lchild)) @@ -339,7 +344,8 @@ AVLTreeNode* AVLTree::remove(AVLTreeNode* & pnode, T key) } else if (key < pnode->key) //要删除的节点比当前节点小,则在左子树进行删除 { - pnode->lchild= remove(pnode->lchild, key); + pnode->lchild = remove(pnode->lchild, key); + if (height(pnode->rchild) - height(pnode->lchild) == 2) //删除左子树节点导致不平衡:相当于情况三或情况一 { if (height(pnode->rchild->lchild)>height(pnode->rchild->rchild)) @@ -357,6 +363,29 @@ void AVLTree::remove(T key) { root =remove(root, key); }; + + +template +void AVLTree::show() +{ + show(root, 0); +} + + +template +void AVLTree::show(AVLTreeNode* pnode, size_t indent) const +{ + if (pnode->rchild != nullptr) + show(pnode->rchild, indent + 1); + for (size_t i = 0; i < indent; ++i) + cout << '\t'; + + cout << pnode->key << endl; + + if (pnode->lchild != nullptr) + show(pnode->lchild, indent + 1); +} + /*中序遍历*/ template void AVLTree::inOrder(AVLTreeNode* pnode) const @@ -382,8 +411,8 @@ void AVLTree::preOrder(AVLTreeNode* pnode) const if (pnode != nullptr) { cout << pnode->key << endl; - inOrder(pnode->lchild); - inOrder(pnode->rchild); + preOrder(pnode->lchild); + preOrder(pnode->rchild); } }; template @@ -398,8 +427,8 @@ void AVLTree::postOrder(AVLTreeNode* pnode) const { if (pnode != nullptr) { - inOrder(pnode->lchild); - inOrder(pnode->rchild); + postOrder(pnode->lchild); + postOrder(pnode->rchild); cout << pnode->key << endl; } } From 2988554424a2404226db4aeafa4fb5da3dd1558b Mon Sep 17 00:00:00 2001 From: dunanshan Date: Tue, 15 Jun 2021 23:50:22 +0800 Subject: [PATCH 2/5] fix remove node with one child --- AVLTree/AVLTree/AVLTree.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/AVLTree/AVLTree/AVLTree.h b/AVLTree/AVLTree/AVLTree.h index 68a13b5..d3d99d2 100644 --- a/AVLTree/AVLTree/AVLTree.h +++ b/AVLTree/AVLTree/AVLTree.h @@ -321,12 +321,14 @@ AVLTreeNode* AVLTree::remove(AVLTreeNode* & pnode, T key) else { AVLTreeNode * ptemp = pnode; - if (pnode->lchild != nullptr) - pnode = pnode->lchild; - else if (pnode->rchild != nullptr) - pnode = pnode->rchild; + if (pnode->lchild != nullptr) + pnode = pnode->lchild; + else if (pnode->rchild != nullptr) + pnode = pnode->rchild; + else + pnode = nullptr; delete ptemp; - return nullptr; + return pnode; } } From ab4bb0bdd207a66b7522b575f3237413d81392db Mon Sep 17 00:00:00 2001 From: dunanshan Date: Wed, 16 Jun 2021 00:18:01 +0800 Subject: [PATCH 3/5] fix update height --- AVLTree/AVLTree/AVLTree.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/AVLTree/AVLTree/AVLTree.h b/AVLTree/AVLTree/AVLTree.h index d3d99d2..ec36440 100644 --- a/AVLTree/AVLTree/AVLTree.h +++ b/AVLTree/AVLTree/AVLTree.h @@ -317,6 +317,8 @@ AVLTreeNode* AVLTree::remove(AVLTreeNode* & pnode, T key) pnode->rchild = remove(pnode->rchild, psuc->key); //递归地删除最小节点 } + pnode->height = max(height(pnode->lchild), height(pnode->rchild)) + 1; + } else { @@ -328,6 +330,9 @@ AVLTreeNode* AVLTree::remove(AVLTreeNode* & pnode, T key) else pnode = nullptr; delete ptemp; + + if (pnode != nullptr) + pnode->height = 1; return pnode; } @@ -343,6 +348,8 @@ AVLTreeNode* AVLTree::remove(AVLTreeNode* & pnode, T key) else pnode = rightRotation(pnode); //相当于情况二 } + + pnode->height = max(height(pnode->lchild), height(pnode->rchild)) + 1; } else if (key < pnode->key) //要删除的节点比当前节点小,则在左子树进行删除 { @@ -355,6 +362,8 @@ AVLTreeNode* AVLTree::remove(AVLTreeNode* & pnode, T key) else pnode = leftRotation(pnode); } + + pnode->height = max(height(pnode->lchild), height(pnode->rchild)) + 1; } return pnode; } From 7dd0add4f923ae638d187f684ed4085f3d659997 Mon Sep 17 00:00:00 2001 From: dunanshan Date: Wed, 16 Jun 2021 00:19:16 +0800 Subject: [PATCH 4/5] update main test --- AVLTree/AVLTree/AVLTree.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/AVLTree/AVLTree/AVLTree.cpp b/AVLTree/AVLTree/AVLTree.cpp index e54a8c7..487e8bf 100644 --- a/AVLTree/AVLTree/AVLTree.cpp +++ b/AVLTree/AVLTree/AVLTree.cpp @@ -2,15 +2,19 @@ // #include "stdafx.h" -#include"AVLTree.h" -#include +#include "AVLTree.h" +#include +#include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { AVLTree a; - for (int i = 0; i < 10; i++) - a.insert(i); + + vector val{ 13, 8, 21, 3, 10, 17, 25, 2, 6, 9, 12, 15, 19, 23, 27, 1, 5, 7, 11, 14, 16, 18, 20, 22, 24, 26, 28, 4 }; + + for (size_t i = 0; i < val.size(); i++) + a.insert(val[i]); cout << "树高:" << a.height() << endl; cout << "前序遍历:" << endl; a.preOrder(); @@ -21,17 +25,26 @@ int _tmain(int argc, _TCHAR* argv[]) cout << "后序遍历:" << endl; a.postOrder(); - cout << "删除元素10"<* b = a.search_iterator(10); if (b != nullptr) - cout << b->key; + cout << b->key << endl; else cout << "无此元素" << endl; - getchar(); + //system("pause"); return 0; } From 5e1cbc498b7f868b6f321225b2690f928d798927 Mon Sep 17 00:00:00 2001 From: dunanshan Date: Wed, 16 Jun 2021 13:19:28 +0800 Subject: [PATCH 5/5] demo AVL delete O(logN) --- AVLTree/AVLTree/AVLTree.cpp | 32 ++++++++++++++++++++++---------- AVLTree/AVLTree/AVLTree.h | 29 +++++++++++++++++++++++++++++ AVLTree/AVLTree/AVLTree.vcxproj | 4 ++-- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/AVLTree/AVLTree/AVLTree.cpp b/AVLTree/AVLTree/AVLTree.cpp index 487e8bf..8f39202 100644 --- a/AVLTree/AVLTree/AVLTree.cpp +++ b/AVLTree/AVLTree/AVLTree.cpp @@ -11,7 +11,14 @@ int _tmain(int argc, _TCHAR* argv[]) { AVLTree a; - vector val{ 13, 8, 21, 3, 10, 17, 25, 2, 6, 9, 12, 15, 19, 23, 27, 1, 5, 7, 11, 14, 16, 18, 20, 22, 24, 26, 28, 4 }; + vector val{ 10, 9, 11, 12 }; + //{ 13, 8, 21, 3, 10, 16, 29, 1, 6, 9, 11, 14, 19, 26, 31, 2, 4, 7, 12, 15, 17, 20, 24, 28, 30, 33, 5, 18, 23, 25, 27, 32, 22 }; + //{ 8, 3, 10, 1, 6, 9, 11, 2, 4, 7, 12, 5 }; + //{ 10, 9, 11, 12 }; + + + // other + //{ 13, 8, 21, 3, 10, 17, 25, 2, 6, 9, 12, 15, 19, 23, 27, 1, 5, 7, 11, 14, 16, 18, 20, 22, 24, 26, 28, 4 }; for (size_t i = 0; i < val.size(); i++) a.insert(val[i]); @@ -27,22 +34,27 @@ int _tmain(int argc, _TCHAR* argv[]) cout << endl; + a.show_build(); + cout << endl; + a.show(); - //cout << "删除元素9"<* b = a.search_iterator(10); + + + //AVLTreeNode* b = a.search_iterator(10); - if (b != nullptr) - cout << b->key << endl; - else - cout << "无此元素" << endl; + //if (b != nullptr) + // cout << b->key << endl; + //else + // cout << "无此元素" << endl; //system("pause"); diff --git a/AVLTree/AVLTree/AVLTree.h b/AVLTree/AVLTree/AVLTree.h index ec36440..edaaff2 100644 --- a/AVLTree/AVLTree/AVLTree.h +++ b/AVLTree/AVLTree/AVLTree.h @@ -6,6 +6,9 @@ */ #ifndef _AVL_TREE_HPP_ #define _AVL_TREE_HPP_ + +#include + template struct AVLTreeNode { @@ -26,6 +29,7 @@ class AVLTree ~AVLTree(); //析构函数 void show(); // tree print + void show_build(); // tree print void preOrder(); //前序遍历AVL树 void InOrder(); //中序遍历AVL树 @@ -383,6 +387,31 @@ void AVLTree::show() } +template +void AVLTree::show_build() +{ + std::queue*> q; + + if (root == nullptr) + return; + + q.push(root); + + while (!q.empty()) + { + auto v = q.front(); + q.pop(); + std::cout << v->key << ", "; + + if (v->lchild != nullptr) + q.push(v->lchild); + if (v->rchild != nullptr) + q.push(v->rchild); + } + +} + + template void AVLTree::show(AVLTreeNode* pnode, size_t indent) const { diff --git a/AVLTree/AVLTree/AVLTree.vcxproj b/AVLTree/AVLTree/AVLTree.vcxproj index 4df91fa..f14eaa0 100644 --- a/AVLTree/AVLTree/AVLTree.vcxproj +++ b/AVLTree/AVLTree/AVLTree.vcxproj @@ -19,13 +19,13 @@ Application true - v120 + v142 Unicode Application false - v120 + v142 true Unicode