-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTreeClass.cpp
More file actions
103 lines (86 loc) · 1.73 KB
/
AVLTreeClass.cpp
File metadata and controls
103 lines (86 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "stdafx.h"
#include "AVLTreeClass.h"
#include "AVLNode.h"
#include <algorithm>
// declarations?
// empty AVLTreeClass
AVLTreeClass::AVLTreeClass()
{
// create a clear method in here somehow.
}
// create an AVLTree with a specified root
AVLTreeClass::AVLTreeClass(int obj)
{
// constructor
// TODO creat "add" method
// add(obj, root) -> signature
}
AVLTreeClass::~AVLTreeClass()
{
// destructor
}
// fix the imbalances
// case 1
AVLNode* AVLTreeClass::caseOneRotation(AVLNode* k2)
{
AVLNode* k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2->height = std::max(height(k2->left), height(k2->right)) + 1;
k1->height = std::max(height(k1->left), k2->height) + 1;
// zero pointer
k2 = 0;
return k1;
}
// case 2
AVLNode* AVLTreeClass::caseTwoRotation(AVLNode* k3)
{
// TODO
k3->left = caseFourRotation(k3->left);
return caseOneRotation(k3);
}
// case 3
AVLNode* AVLTreeClass::caseThreeRotation(AVLNode* k3)
{
// TODO
k3->right = caseOneRotation(k3->right);
return caseFourRotation(k3);
}
// case 4
AVLNode* AVLTreeClass::caseFourRotation(AVLNode* k2)
{
AVLNode* k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
k2->height = std::max(height(k2->left), height(k2->right)) + 1;
k1->height = std::max(height(k1->right), k2->height) + 1;
// zero pointer
k2 = 0;
return k1;
}
// implement method to get node height
int AVLTreeClass::height(AVLNode* node)
{
return node == 0 ? -1 : node->height;
}
// return item in root node
int AVLTreeClass::getRootItem()
{
// this checks for nullptr
if (this == nullptr)
{
throw _EXCEPTION_;
}
return root.theItem;
}
// return minimum item in tree
int AVLTreeClass::getMinItem()
{
// TODO
return 0;
}
AVLNode AVLTreeClass::getMinNode(AVLNode node)
{
// TODO
return node;
}