### Binary Search Tree Traversals in C++

So this will pretty much wrap up our short expedition into binary trees. Remember that these functions rely on my earlier posts dealing with the class and nodes.

The three types of traversals we will look at are:

• Preorder (root, left, right)
• Inorder (left, root, right)
• Postorder (left, right, root)

#### Preorder Traversal

This type of traversal can be used to create duplicates of a tree and can be used to implement prefixes. Whatever order of values you put into a tree will be the order that preorder traversal gives you. So If I enter, 5 7 4 2 1 9 8 3 6 into a binary tree, when I use preorder traversal, I will expect to see 5 7 4 2 1 9 8 3 6.

```void preorderTraversal (Node* n) {
if (n != nullptr) { //make sure we have a value
cout << n->data << endl; //Print out the current Node value
preorderTraversal(n->left); //traverse down the left side
preorderTraversal(n->right); //Once we return from the left, go down the right
}
}```

#### Inorder Traversal

This type of traversal will return data sorted in order. So if I entered, 5 7 4 2 1 9 8 3 6, I would expect to see 1 2 3 4 5 6 7 8 9.

```  void inorderTraversal (Node* n) {
if (n != nullptr) { //make sure we have a value
inorderTraversal(n->left); //traverse down the left side
cout << n->data << endl; //Print out the current Node value
inorderTraversal(n->right); //Once we return from the print, go down the right
}
}```

#### Postorder Traversal

This type of traversal can be used to implement postfixes and can cleanly be used to destroy a tree with out leaving Nodes floating around corrupting memory. So if I entered, 5 7 4 2 1 9 8 3 6, I would expect to see 1 3 2 4 6 8 9 7 5

``` void postorderTraversal (Node* n) {
if (n != nullptr) { //make sure we have a value
postorderTraversal(n->left); //traverse down the left side
postorderTraversal(n->right); //Once we return from the left, go down the right
cout << n->data << endl; //Print out the current Node value
}
}```

For more information on binary trees, I would checkout this article. It goes into detail on how traversals on called on the tree and gives some great visuals. I also liked this interactive visual for binary search trees in general.

Good luck!

### Sweet Binary Search Tree Print in C++

I found the basis of a print on Stackover flow the other day and changed it a little bit to be more compatible across compiler versions.

Happy Coding!

```void print() {
}
```

For some reason my code editor wouldn’t do the left slashes so below is a screenshot.

Here is an example of what it will look like for the sequence 5 7 4 2 1 9 8 3 6:

### 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;
}

};```