## Simple AVL Tree in C++

An AVL tree is a binary search tree(BST) however, unlike binary search trees, an AVL tree (named after Georgy Adelson-Velsky and Evgenii Landis) is self balancing. So no matter how many nodes you insert into the tree, it will adjust it’s branches and ensure the tree is balanced at all times. Making sure the subtree heights only differ by at most 1. BSTs are great for segregating and storing data with a O(log n) search time. Downside with BST is that it can get weighted on one side and doesn’t have an restrictions to prevent it from getting skewed. By switching to an AVL, data is balanced in the tree and the search time is decreased to log n.

So it is more efficient in most cases to use the AVL tree, below is an example of how to code this in C++. Note that the AVL tree uses a lot of the same code the BST did from this post.

```#pragma once

#include <iomanip>
#include <iostream>

using namespace std;

class AVL
{
public:
AVL(){
root = nullptr;
}
~AVL(){
destroy(root);
}

//My Node class for storing data, note how I add height
struct Node{
int data;
Node *left;
Node *right;
int height;

Node(int d){
data = d;
left = nullptr;
right = nullptr;
height = 0;
}

void updateHeight(){
int lHeight = 0;
int rHeight = 0;
if (left != nullptr) {
lHeight = left->height;
}
if (right != nullptr) {
rHeight = right->height;
}
int max = (lHeight > rHeight) ? lHeight : rHeight;
height = max + 1;
}

};

void insert(int val){
insert(val, root);
}

//Rotate a Node branch to the left, in order to balance things
Node* rotateLeft(Node *&leaf){
Node* temp = leaf->right;
leaf->right = temp->left;
temp->left = leaf;

//update the Nodes new height
leaf->updateHeight();

return temp;
}

//Rotate a Node branch to the right, in order to balance things
Node* rotateRight(Node *&leaf){
Node* temp = leaf->left;
leaf->left = temp->right;
temp->right = leaf;

//update the Nodes new height
leaf->updateHeight();

return temp;
}

//Rotate a Node branch to the right then the left, in order to balance things
Node* rotateRightLeft(Node *&leaf){
Node* temp = leaf->right;
leaf->right = rotateRight(temp);
return rotateLeft(leaf);
}

//Rotate a Node branch to the left then the right, in order to balance things
Node* rotateLeftRight(Node *&leaf){
Node* temp = leaf->left;
leaf->left = rotateLeft(temp);
return rotateRight(leaf);
}

//Function that checks each Node's left and right branches to determine if they are unbalanced
//If they are, we rotate the branches
void rebalance(Node *&leaf){
int hDiff = getDiff(leaf);
if (hDiff > 1){
if (getDiff(leaf->left) > 0) {
leaf = rotateRight(leaf);
} else {
leaf = rotateLeftRight(leaf);
}
} else if(hDiff < -1) {
if (getDiff(leaf->right) < 0) {
leaf = rotateLeft(leaf);
} else {
leaf = rotateRightLeft(leaf);
}
}
}

private:
Node *root;
//Insert a Node (very similar to BST, except we need to update Node height and then check for rebalance)
void insert(int d, Node *&leaf){
if (leaf == nullptr){
leaf = new Node(d);
leaf->updateHeight();
}
else {
if (d < leaf->data){
insert(d, leaf->left);
leaf->updateHeight();
rebalance(leaf);
}
else{
insert(d, leaf->right);
leaf->updateHeight();
rebalance(leaf);
}
}
}

//Same as BST
void destroy(Node *&leaf){
if (leaf != nullptr){
destroy(leaf->left);
destroy(leaf->right);
delete leaf;
}
}

//Get the difference between Node right and left branch heights, if it returns positive
//We know the left side is greater, if negative, we know the right side is greater
int getDiff(Node *leaf){
int lHeight = 0;
int rHeight = 0;
if (leaf->left != nullptr) {
lHeight =  leaf->left->height;
}
if (leaf->right != nullptr) {
rHeight = leaf->right->height
}
return lHeight - rHeight;
}
};```

Let me know if you have any issues!

Here is a short insert on how to remove all Nodes of X value from a list. I use recursion and I believe I’ve created a pretty clean solution. This requires Nodes found in my earlier post.

```#include "Node.h"

//Run the search and remove on the linked list starting at the head
//target is the value we are trying to remove from our list
void searchAndRemove(int target){
}

void searchAndRemove(int target, Node*& n){
if(n != nullptr){
if(n->data == target) { //if we found our target
Node* temp = n;
n = n->next; //delete and replace
delete temp; //delete the node to clean up memory
searchAndRemove(target, n); //continue the search, we need this to check the one used to replace
} else {
searchAndRemove(target, n->next); //continue the search
}
}
}```

Let me know if you have any comments or improvements!

## Simple List Class in C++

Here is an example of a simple List class I wrote. Follow the comments to understand what is going on. This uses the node structure I wrote about here.

Best of luck!

```#include <cstdlib>
#include "Node.h" //Your Node class, see previous post

using namespace std;

class List
{
public:
List(){
}
~List(){
}
void append(int d){
}
void print(){
}
void insertInOrder(int d){
}
bool findValue (int d){
}
int getSize(){
}

private:
Node *head; //Remember this struct from the previous post

/* Destroy the list
* We recursively call the destroy function to cycle through a list to the end.
* Once the end node is reached, it is deleted. Then as we progress back through
* our recursive call, each associated node is deleted from the end up.
* This is called by our destructor to clean everything up.
*/
void destroyList(Node *n){
if (n->next != nullptr){
destroyList(n->next);
}
delete n;
}

/* Add a Node to the end of a list
* Use recursion to cycle through the list to the end
* Once the end is reached, the next pointer of the last node will by nullptr.
* A new Node will then be inserted replacing that nullptr and chaining to the list
*/
Node* append(Node *n, int d){
if (n == nullptr){
return new Node(d);
}
else {
n->next= append(n->next, d);
}
return n;
}

/* Print the List
* Use recursion to loop through and print each Node in a list
*/
void print(Node *n){
cout << n->data << endl;
if (n->next != nullptr){
print(n->next);
}
}

/* Insert a Node in Numeric order
* Loop throug a list using recursion
* Once the inserting value is less than the current Node
* we know we need to insert the Node in that position
* else we keep cycling through
*/
Node* insertInOrder(Node *n, int d){
if (n == nullptr){
return new Node(d);
}
else if (d > n->data){
n->next= insertInOrder(n->next, d);
}
else {
Node* temp = n; //Temp copy
n = new Node(d); //Set the pointer to a new Node
n->next = temp; //Chain the original onto the new Node's next pointer
}
return n;
}

/* Find a Node
* Loop through the enitre list using recursion
* Return true once the Node value we want is found
*/
bool findValue(Node *n, int d){
if (n->data == d){
return true;
}
else if(n->next != nullptr){
return findValue(n->next, d);
}
return false;
}

/* Get the Size of a List
* Use recursion to cycle through the list
* all the while keeping a counter going of each Node encountered
*/
int getSize(Node *n, int i = 0){
if (n != nullptr){
++i;
return getSize(n->next, i);
}
return i;
}

};```

## Sequential Search in C++

Okay the sequential search is easier than the recursive and iterative binary searches, I figure I would include it to wrap up searches for now. For the sequential search, you loop through an array (or whatever data structure you are using) checking each position for the search value. Works just as well on sorted array lists as it does on unsorted array lists!

Good luck, let me know if you have questions or improvements!

```int sizeOfArray; //size of array, way easy to store it than calculate it everytime
int* array; //contains a list of integers that you will search through

bool sequentialFind(int number){
for(int i = 0; i < sizeOfArray; i++){
if (array[i] == number){
return true;
}
}
return false;
}```

## Iterative Binary Search in C++

So yesterday’s post was on how to do a recursive binary search, today is looking at performing the exact same search on an array but doing it iteratively.  So instead of calling a function over and over and over again, we will use a loop. Again, this search is best on a sorted array.

The search function takes in an integer and will return a bool if the integer was found in a given array. The first thing we do is determine the search range, for the first loop, this will be from start of array to the end of array (0 to the size of the array),

Now we jump into the array, we will keep looping as long as the low bound is less than the high. Once the low bound is greater or equal to the high bound, we know that we exhausted the entire loop and did not find our integer in it.

Just like the recursive search, we find our middle first thing inside the loop. From there, we check if our search value is at that middle position. If not, we determine if the value is less than the middle or greater. If it is less than the middle position, we now know that the search value is in between our low bound and the middle and we can adjust our bounds for the next loop accordingly. If our value is greater than the middle, it is in between the middle and our high bound.

We keep adjusting our bounds and narrowing down the search range in this manner until the number is found or the low bound is no longer less than the high.

Happy programing, let me know if you have any questions or improvements!

```int sizeOfArray; //contains the size of the search array
int* array; //integer array that we will search through

bool iterativeBinaryFind(int number){
int highBound = sizeOfArray;
int lowBound = 0;
while (lowBound < highBound){
int middle = (lowBound + highBound)/2;

if (array[middle] == number) return true;
else if (array[middle] > number) highBound = middle - 1;
else lowBound = middle + 1;
}
return false;
}```

## Recursive Binary Search in C++

I wrote a recursive binary find that looks through a sorted array for a given number.  (You can use this on an unsorted array, it just isn’t as performant.)

This is how it works, the recursive function call requires the number to search and the lowest and highest positions in a search range. To start, you give the low position, 0, and the high position, the size of the array. This is seen in the first function calling the recursive helper function. Each recursive call, first checks to make sure the low position is actually lower than the high, if it is not, the number was not found in the array. Next, we find the middle of the search section (right now it’s 0 to the size of the array). Let’s say our size is 10, so we determine the middle to be 5.

The if/else statement then works a little magic to determine if the value we have is actually in the array. First we check if the middle position contains the value we want. If it does, return true, we found our number in the array! If not, is the middle position higher than the searching value, if it is make a recursive call to our function, passing the search number, keep the low position bound and pass middle -1 as the high position bound. This is because we know the middle is higher than the search value, so the search value must be between the low and the middle positions. Else, if the two previous conditions are not true, we know the search number must be between the middle and the high position value so we pass the recursive call the search number, middle + 1 and the high value.

This process then recursively repeats itself until it either narrows down the search range and finds the search value or it cycles through and never finds the search value. Below is the code demonstrating this process. Leave comments if you have any questions or improvements.

```int sizeOfArray; //size of array

bool recursiveBinaryFind(int searchNum){
//pass 0 as the low pasotion and the size of the array as the highest search range position
return recursiveBinaryFind(searchNum, 0, sizeOfArray);
}

bool recursiveBinaryFind(int searchNum, int lowBound, int highBound){
//If the low position is greater than the high, quit we looped through everything and didn\'t find our value
if (lowBound > highBound)    return false;

//Find the middle position
int middle = (lowBound + highBound)/2;

//Did we find our value?
if (array[middle] == searchNum) {
return true;
} else if (array[middle] > searchNum) {
//Search the lower search range half
return recursiveBinaryFind(searchNum, lowBound, middle - 1);
} else {
//Search the higher search range half
return recursiveBinaryFind(searchNum, middle + 1, highBound);
}
}```