forked from JaredBahor/CSE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.cpp
More file actions
136 lines (104 loc) · 2.96 KB
/
AVLTree.cpp
File metadata and controls
136 lines (104 loc) · 2.96 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// Created by jared on 7/7/2021.
//
#include "avl.h"
#include <iostream>
#include <queue>
using namespace std;
void AVL::rotateLeft(Node *& node) {
Node *tmp = node->right;
node->right = tmp->left;
tmp->left = node;
node = tmp;
return;
}
void AVL::rotateRight(Node *& node) {
Node *tmp = node->left;
node->left = tmp->right;
tmp->right = node;
node = tmp;
return;
}
void AVL::rotateLeftRight(Node *& node) {
rotateLeft(node->left);
rotateRight(node);
return;
}
void AVL::rotateRightLeft(Node *& node) {
rotateRight(node->right);
rotateLeft(node);
return;
}
int getHeight(Node *root) {
if (root == nullptr) {
return -1;
} else {
int hLeft = getHeight(root->left);
int hRight = getHeight(root->right);
return std::max(hLeft, hRight) + 1;
}
}
//newly made functions
Node* insert2(Node* node, const int &v){
int bal;
if (node == nullptr){// if tree is empty
Node* node1 = new Node(v);
return node1;
}
if (v > node->val){
node->right = insert2(node->right, v);
}else if (v < node->val){
node->left = insert2(node->left, v);
}else{
return node;
}
node->height = getHeight(node);
bal = getHeight(node->left) - getHeight(node->right);// add if statement to check if node is null
//rotate cases
if (1 < bal && v < node->left->val){//rotate right
AVL::rotateRight(node);
}
if (bal < -1 && v > node->right->val){//rotate left
AVL::rotateLeft(node);
}
if (bal > 1 && v > node->left->val){//rotate left -> right
AVL::rotateLeftRight(node);
}
if (bal < -1 && node->right->val > v){//rotate right -> left
AVL::rotateRightLeft(node);
}
return node;
}
void AVL::insert(const int &v) {
// your code
// <50 lines of code should be OK
root = insert2(root, v);
//correctly insert a num but will not balance
}
int computeHeight(Node *root){
if (root == nullptr){
return -1;
}else{
int hLeft = computeHeight(root->left);
int hRight = computeHeight(root->right);
return std::max(hLeft, hRight) + 1;
}
}
void printBT(const string& prefix, const Node* node, bool isLeft)
{
if( node != nullptr )
{
cout << prefix;
cout << (isLeft ? "├──" : "└──" );
int balance = computeHeight(node->left) - computeHeight(node->right);
// print the value of the node
cout << " " << node->val << "," << node->height << "," << balance << endl;
// enter the next tree level - left and right branch
printBT( prefix + (isLeft ? "│ " : " "), node->left, true);
printBT( prefix + (isLeft ? "│ " : " "), node->right, false);
}
}
void printBT(const Node* node)
{
printBT("", node, false);
}