From 6f5d2f2d5eebc28b2f1418d0089c5060b4a92153 Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Thu, 2 May 2024 17:34:56 -0700 Subject: [PATCH] 8.3 Lab: BT - Traversals (in-, post-, and pre-Order) --- 05-trees/BinaryTree.cpp | 138 +++++++++++++++++++++++++++++++++++ 05-trees/BinaryTree.h | 47 ++++++++++++ 05-trees/BinaryTree_Demo.cpp | 68 +++++++++++++++++ 05-trees/CMakeLists.txt | 8 ++ 4 files changed, 261 insertions(+) create mode 100644 05-trees/BinaryTree.cpp create mode 100644 05-trees/BinaryTree.h create mode 100644 05-trees/BinaryTree_Demo.cpp create mode 100644 05-trees/CMakeLists.txt diff --git a/05-trees/BinaryTree.cpp b/05-trees/BinaryTree.cpp new file mode 100644 index 0000000..bc5934c --- /dev/null +++ b/05-trees/BinaryTree.cpp @@ -0,0 +1,138 @@ +// Implementation file for the BinaryTree class +#include // For cout and NULL + + +#include "BinaryTree.h" +#include // For rand() +#include // 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; + } +} diff --git a/05-trees/BinaryTree.h b/05-trees/BinaryTree.h new file mode 100644 index 0000000..40ef39e --- /dev/null +++ b/05-trees/BinaryTree.h @@ -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 diff --git a/05-trees/BinaryTree_Demo.cpp b/05-trees/BinaryTree_Demo.cpp new file mode 100644 index 0000000..e02359e --- /dev/null +++ b/05-trees/BinaryTree_Demo.cpp @@ -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 +#include +#include +#include +#include +#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); + } +} diff --git a/05-trees/CMakeLists.txt b/05-trees/CMakeLists.txt new file mode 100644 index 0000000..bb0cbb1 --- /dev/null +++ b/05-trees/CMakeLists.txt @@ -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)