Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix preOrder and postOrder; and add function: show tree. #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions AVLTree/AVLTree/AVLTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@
//

#include "stdafx.h"
#include"AVLTree.h"
#include<iostream>
#include "AVLTree.h"
#include <iostream>
#include <vector>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
AVLTree<int> a;
for (int i = 0; i < 10; i++)
a.insert(i);

vector<int> 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]);
cout << "���ߣ�" << a.height() << endl;
cout << "ǰ�����:" << endl;
a.preOrder();
Expand All @@ -21,17 +32,31 @@ int _tmain(int argc, _TCHAR* argv[])
cout << "�������:" << endl;
a.postOrder();

cout << "ɾ��Ԫ��10"<<endl;
a.remove(10);
cout << endl;

a.show_build();
cout << endl;

a.show();

cout << "ɾ��Ԫ��9"<<endl;
a.remove(9);
//cout << "ɾ��Ԫ��12" << endl;
//a.remove(12);
cout << endl;

a.show();



AVLTreeNode<int>* b = a.search_iterator(10);
//AVLTreeNode<int>* b = a.search_iterator(10);

if (b != nullptr)
cout << b->key;
else
cout << "�޴�Ԫ��" << endl;
//if (b != nullptr)
// cout << b->key << endl;
//else
// cout << "�޴�Ԫ��" << endl;

getchar();
//system("pause");

return 0;
}
Expand Down
91 changes: 80 additions & 11 deletions AVLTree/AVLTree/AVLTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
#ifndef _AVL_TREE_HPP_
#define _AVL_TREE_HPP_

#include <queue>

template <typename T>
struct AVLTreeNode
{
Expand All @@ -25,6 +28,9 @@ class AVLTree
AVLTree(); //���캯��
~AVLTree(); //��������

void show(); // tree print
void show_build(); // tree print

void preOrder(); //ǰ�����AVL��
void InOrder(); //�������AVL��
void postOrder(); //�������AVL��
Expand All @@ -46,6 +52,8 @@ class AVLTree
AVLTreeNode<T>* root; //AVL���ĸ��ڵ�

private:
void show(AVLTreeNode<T>* pnode, size_t indent) const; // tree print

void preOrder(AVLTreeNode<T>* pnode) const;
void inOrder(AVLTreeNode<T>* pnode) const;
void postOrder(AVLTreeNode<T>* pnode) const;
Expand Down Expand Up @@ -313,40 +321,53 @@ AVLTreeNode<T>* AVLTree<T>::remove(AVLTreeNode<T>* & pnode, T key)
pnode->rchild = remove(pnode->rchild, psuc->key); //�ݹ��ɾ����С�ڵ�
}

pnode->height = max(height(pnode->lchild), height(pnode->rchild)) + 1;

}
else
{
AVLTreeNode<T> * 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;

if (pnode != nullptr)
pnode->height = 1;
return pnode;
}

}
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))
pnode = leftRightRotation(pnode); //�൱�������
else
pnode = rightRotation(pnode); //�൱�������
}

pnode->height = max(height(pnode->lchild), height(pnode->rchild)) + 1;
}
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))
pnode = rightLeftRotation(pnode);
else
pnode = leftRotation(pnode);
}

pnode->height = max(height(pnode->lchild), height(pnode->rchild)) + 1;
}
return pnode;
}
Expand All @@ -357,6 +378,54 @@ void AVLTree<T>::remove(T key)
{
root =remove(root, key);
};


template<typename T>
void AVLTree<T>::show()
{
show(root, 0);
}


template<typename T>
void AVLTree<T>::show_build()
{
std::queue<AVLTreeNode<T>*> 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<typename T>
void AVLTree<T>::show(AVLTreeNode<T>* 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<typename T>
void AVLTree<T>::inOrder(AVLTreeNode<T>* pnode) const
Expand All @@ -382,8 +451,8 @@ void AVLTree<T>::preOrder(AVLTreeNode<T>* pnode) const
if (pnode != nullptr)
{
cout << pnode->key << endl;
inOrder(pnode->lchild);
inOrder(pnode->rchild);
preOrder(pnode->lchild);
preOrder(pnode->rchild);
}
};
template<typename T>
Expand All @@ -398,8 +467,8 @@ void AVLTree<T>::postOrder(AVLTreeNode<T>* pnode) const
{
if (pnode != nullptr)
{
inOrder(pnode->lchild);
inOrder(pnode->rchild);
postOrder(pnode->lchild);
postOrder(pnode->rchild);
cout << pnode->key << endl;
}
}
Expand Down
4 changes: 2 additions & 2 deletions AVLTree/AVLTree/AVLTree.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down