Compare commits
2 Commits
5b5e2b6d67
...
41c45e09fb
Author | SHA1 | Date | |
---|---|---|---|
41c45e09fb | |||
6f5d2f2d5e |
145
05-trees/BinaryTree.cpp
Normal file
145
05-trees/BinaryTree.cpp
Normal file
@ -0,0 +1,145 @@
|
||||
// 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
|
||||
*~**/
|
||||
void BinaryTree::postOrder(void visit(const Data &)) const {
|
||||
_postOrder(root, visit);
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
Postorder Traversal of the Binary Tree:
|
||||
Left-Right-Root
|
||||
*~**/
|
||||
void BinaryTree::_postOrder(BinaryTree::Node *root, void visit(const Data &)) const {
|
||||
if (root == nullptr) return;
|
||||
|
||||
_postOrder(root->left, visit);
|
||||
_postOrder(root->right, visit);
|
||||
visit(root->data);
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
This function calls a recursive function to traverse the
|
||||
tree in preorder
|
||||
*~**/
|
||||
void BinaryTree::preOrder(void visit(const Data &)) const {
|
||||
_preOrder(root, visit);
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
Postorder Traversal of the Binary Tree:
|
||||
Left-Right-Root
|
||||
*~**/
|
||||
void BinaryTree::_preOrder(BinaryTree::Node *root, void visit(const Data &)) const {
|
||||
if (root == nullptr) return;
|
||||
|
||||
visit(root->data);
|
||||
_preOrder(root->left, visit);
|
||||
_preOrder(root->right, visit);
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
This function calls a recursive function to traverse the
|
||||
tree in inorder
|
||||
*~**/
|
||||
void BinaryTree::inOrder(void visit(const Data &)) const {
|
||||
_inOrder(root, visit);
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
Inorder Traversal of the Binary Tree:
|
||||
Left-Root-Right
|
||||
*~**/
|
||||
void BinaryTree::_inOrder(Node *root, void visit(const Data &)) const {
|
||||
if (root) {
|
||||
_inOrder(root->left, visit);
|
||||
//cout << root->data.num << " ";
|
||||
visit(root->data);
|
||||
// cout has been replaced with a call for visit
|
||||
// What is visit?
|
||||
// visit is a generic name for a display function
|
||||
// in main(), when inOrder is called, it is decided what function address to assign to visit
|
||||
// here, you just use visit the way you would use/call a function
|
||||
_inOrder(root->right, visit);
|
||||
// ------------------^^^^^^ visit as an argument
|
||||
}
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
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(void visit(const Data &)) const;
|
||||
|
||||
void preOrder(void visit(const Data &)) const;
|
||||
|
||||
void postOrder(void visit(const Data &)) const;
|
||||
|
||||
private:
|
||||
void _inOrder(Node *root, void visit(const Data &)) const;
|
||||
|
||||
void _preOrder(Node *root, void visit(const Data &)) const;
|
||||
|
||||
void _postOrder(Node *root, void visit(const Data &)) const;
|
||||
|
||||
void _destroy(Node *root);
|
||||
};
|
||||
|
||||
#endif
|
90
05-trees/BinaryTree_Demo.cpp
Normal file
90
05-trees/BinaryTree_Demo.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
/**
|
||||
|
||||
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);
|
||||
|
||||
void hDisplay(const Data &item); // horizontal display: all items on one line
|
||||
void vDisplay(const Data &item); // vertical display: one item per line
|
||||
|
||||
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(hDisplay); // hDisplay is the inOrder's argument
|
||||
cout << endl;
|
||||
|
||||
if (option == 'T' || option == 't') {
|
||||
cout << "Postorder: ";
|
||||
tree.postOrder(hDisplay); // passing hDisplay to postOrder
|
||||
cout << endl;
|
||||
tree.postOrder(vDisplay); // passing vDisplay to postOrder
|
||||
cout << endl;
|
||||
}
|
||||
if (option == 'E' || option == 'e') {
|
||||
cout << " Preorder: ";
|
||||
tree.preOrder(hDisplay); // passing hDisplay to preOrder
|
||||
cout << endl;
|
||||
tree.preOrder(vDisplay); // passing vDisplay to 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);
|
||||
}
|
||||
}
|
||||
|
||||
// The following two functions are used as arguments to other functions
|
||||
/**~*~*
|
||||
horizontal display: all items on one line
|
||||
*/
|
||||
void hDisplay(const Data &item) {
|
||||
cout << item.num << " ";
|
||||
}
|
||||
|
||||
/**~*~*
|
||||
// vertical display: one item per line
|
||||
*/
|
||||
void vDisplay(const Data &item) {
|
||||
cout << item.num << endl;
|
||||
}
|
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