8.3 Lab: BT - Traversals (in-, post-, and pre-Order)
This commit is contained in:
parent
5b5e2b6d67
commit
6f5d2f2d5e
138
05-trees/BinaryTree.cpp
Normal file
138
05-trees/BinaryTree.cpp
Normal file
@ -0,0 +1,138 @@
|
||||
// Implementation file for the BinaryTree class
|
||||
#include <iostream> // For cout and NULL
|
||||
|
||||
|
||||
#include "BinaryTree.h"
|
||||
#include <cstdlib> // For rand()
|
||||
#include <ctime> // For time()
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**~*~*
|
||||
Constructor
|
||||
*~**/
|
||||
BinaryTree::BinaryTree() {
|
||||
root = NULL;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
This function calls a recursive function to traverse the
|
||||
tree in postorder (the wrapper function)
|
||||
*~**/
|
||||
void BinaryTree::postOrder() const {
|
||||
_postOrder(root);
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
Postorder Traversal of the Binary Tree:
|
||||
Left-Right-Root (recursive)
|
||||
*~**/
|
||||
void BinaryTree::_postOrder(BinaryTree::Node *root) const {
|
||||
if (root == nullptr) return;
|
||||
|
||||
_postOrder(root->left);
|
||||
_postOrder(root->right);
|
||||
cout << root->data.num << " ";
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
This function calls a recursive function to traverse the
|
||||
tree in preorder (the wrapper function)
|
||||
*~**/
|
||||
void BinaryTree::preOrder() const {
|
||||
_preOrder(root);
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
Preorder Traversal of the Binary Tree:
|
||||
Root-Left-Right (recursive)
|
||||
*~**/
|
||||
void BinaryTree::_preOrder(BinaryTree::Node *root) const {
|
||||
if (root == nullptr) return;
|
||||
|
||||
cout << root->data.num << " ";
|
||||
_preOrder(root->left);
|
||||
_preOrder(root->right);
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
This function calls a recursive function to traverse the
|
||||
tree in inorder (the wrapper function)
|
||||
*~**/
|
||||
void BinaryTree::inOrder() const {
|
||||
_inOrder(root);
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
Inorder Traversal of the Binary Tree:
|
||||
Left-Root-Right (recursive)
|
||||
*~**/
|
||||
void BinaryTree::_inOrder(Node *root) const {
|
||||
if (root) {
|
||||
_inOrder(root->left);
|
||||
cout << root->data.num << " ";
|
||||
_inOrder(root->right);
|
||||
}
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
Insert data into a random Binary Tree
|
||||
*~**/
|
||||
void BinaryTree::insert(Data dataIn) {
|
||||
Node *newNode;
|
||||
Node *pWalk;
|
||||
Node *parent;
|
||||
int rand_num;
|
||||
|
||||
// allocate the new node
|
||||
newNode = new Node;
|
||||
newNode->data = dataIn;
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
|
||||
// find a "random" parent
|
||||
if (!root) // tree is empty
|
||||
root = newNode;
|
||||
else {
|
||||
parent = NULL; // root does not have a parent
|
||||
pWalk = root;
|
||||
while (pWalk) {
|
||||
parent = pWalk;
|
||||
rand_num = rand() % 100;
|
||||
if (rand_num % 2) // if odd - take left
|
||||
pWalk = pWalk->left;
|
||||
else
|
||||
pWalk = pWalk->right;
|
||||
}
|
||||
|
||||
// insert the new node
|
||||
if (!parent->left) // no left child
|
||||
parent->left = newNode;
|
||||
else
|
||||
parent->right = newNode;
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
|
||||
/**~*~*
|
||||
Destructor
|
||||
This function calls a recursive function to delete all nodes in the binary tree
|
||||
*~**/
|
||||
BinaryTree::~BinaryTree() {
|
||||
if (root)
|
||||
_destroy(root);
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
This function traverses the binary tree in postorder and deletes every node
|
||||
*~**/
|
||||
void BinaryTree::_destroy(Node *root) {
|
||||
if (root) {
|
||||
_destroy(root->left);
|
||||
_destroy(root->right);
|
||||
delete root;
|
||||
}
|
||||
}
|
47
05-trees/BinaryTree.h
Normal file
47
05-trees/BinaryTree.h
Normal file
@ -0,0 +1,47 @@
|
||||
// Specification file for the BinaryTree class
|
||||
#ifndef BINARY_TREE_H
|
||||
#define BINARY_TREE_H
|
||||
|
||||
struct Data {
|
||||
int num;
|
||||
// more fields could be added if needed
|
||||
};
|
||||
|
||||
class BinaryTree {
|
||||
private:
|
||||
struct Node {
|
||||
Data data; // The value in this node
|
||||
Node *left; // To point to the left node
|
||||
Node *right; // To point to the right node
|
||||
};
|
||||
|
||||
Node *root; // root of the tree
|
||||
int count; // number of nodes in the tree
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
BinaryTree();
|
||||
|
||||
// Destructor
|
||||
~BinaryTree();
|
||||
|
||||
// Binary Tree operations
|
||||
void insert(Data dataIn);
|
||||
|
||||
void inOrder() const;
|
||||
|
||||
void preOrder() const;
|
||||
|
||||
void postOrder() const;
|
||||
|
||||
private:
|
||||
void _inOrder(Node *root) const;
|
||||
|
||||
void _preOrder(Node *root) const;
|
||||
|
||||
void _postOrder(Node *root) const;
|
||||
|
||||
void _destroy(Node *root);
|
||||
};
|
||||
|
||||
#endif
|
68
05-trees/BinaryTree_Demo.cpp
Normal file
68
05-trees/BinaryTree_Demo.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
/**
|
||||
|
||||
Test Driver for Binary Tree functions
|
||||
|
||||
This program builds a BT of random integers
|
||||
|
||||
Note: The BT_insert() function is specific to this exercise
|
||||
|
||||
The main goal of this example is build a binary tree that could be used to
|
||||
test the traversal and other binary tree functions
|
||||
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include "BinaryTree.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void build_BT(BinaryTree &tree, int n);
|
||||
|
||||
int main(void) {
|
||||
BinaryTree tree;
|
||||
int n; // number of nodes
|
||||
char option;
|
||||
cout << "What is the number of nodes in the BT? " << endl;
|
||||
cin >> n;
|
||||
cout << "What traversal[prE/posT]? " << endl;
|
||||
cin >> option;
|
||||
|
||||
build_BT(tree, n);
|
||||
|
||||
cout << " Inorder: ";
|
||||
tree.inOrder();
|
||||
cout << endl;
|
||||
|
||||
if (option == 'T' || option == 't') {
|
||||
cout << "Postorder: ";
|
||||
tree.postOrder();
|
||||
cout << endl;
|
||||
}
|
||||
if (option == 'E' || option == 'e') {
|
||||
cout << " Preorder: ";
|
||||
tree.preOrder();
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
Builds a random Binary Tree of integer numbers within the range
|
||||
[10, 99]; root is always 50
|
||||
*/
|
||||
void build_BT(BinaryTree &tree, int n) {
|
||||
Data data = {50};
|
||||
|
||||
// allocate and initialize the root
|
||||
tree.insert(data);
|
||||
|
||||
//srand((unsigned int)time(0));
|
||||
while (--n) {
|
||||
data.num = rand() % 90 + 10;
|
||||
tree.insert(data);
|
||||
}
|
||||
}
|
8
05-trees/CMakeLists.txt
Normal file
8
05-trees/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(05_trees)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(05_trees BinaryTree_Demo.cpp
|
||||
BinaryTree.cpp
|
||||
BinaryTree.h)
|
Loading…
x
Reference in New Issue
Block a user