lab_trees
Tempestuous Trees
|
The BinaryTree class represents a templated linked-memory tree data structure. More...
#include "binarytree.h"
Public Member Functions | |
BinaryTree () | |
Constructor to create an empty tree. More... | |
BinaryTree (const BinaryTree &other) | |
Copy constructor. More... | |
~BinaryTree () | |
Destructor; frees all nodes associated by this tree. More... | |
const BinaryTree & | operator= (const BinaryTree &rhs) |
Assignment operator. More... | |
void | clear () |
Frees all nodes associated with this tree and sets it to be empty. More... | |
void | insert (const T &elem, bool sorted=false) |
Inserts into the BinaryTree. More... | |
void | print () const |
Prints the contents of the tree to stdout. More... | |
int | height () const |
This lab deals with the following six helper functions: More... | |
void | printLeftToRight () const |
Prints out the values of the nodes of a binary tree in order. More... | |
void | mirror () |
Flips the tree over a vertical axis, modifying the tree itself (not creating a flipped copy). More... | |
bool | isOrdered () const |
Private helper function for the public isOrdered function. More... | |
void | printPaths () const |
Prints out all the possible paths from the root of the tree to any leaf node. More... | |
int | sumDistances () const |
Each node in a tree has a distance from the root node - the depth of that node, or the number of edges along the path from that node to the root. More... | |
The BinaryTree class represents a templated linked-memory tree data structure.
BinaryTree< T >::BinaryTree | ( | ) |
Constructor to create an empty tree.
BinaryTree< T >::BinaryTree | ( | const BinaryTree< T > & | other | ) |
Copy constructor.
BinaryTree< T >::~BinaryTree | ( | ) |
Destructor; frees all nodes associated by this tree.
const BinaryTree< T > & BinaryTree< T >::operator= | ( | const BinaryTree< T > & | rhs | ) |
Assignment operator.
rhs | The tree to make a copy of |
void BinaryTree< T >::clear | ( | ) |
Frees all nodes associated with this tree and sets it to be empty.
void BinaryTree< T >::insert | ( | const T & | elem, |
bool | sorted = false |
||
) |
Inserts into the BinaryTree.
elem | The element to insert |
sorted | By default, this parameter is false. That means that the element takes a pseudo-random path to a leaf where it is inserted. If true, the insert function will act like it does in a BST. |
void BinaryTree< T >::print | ( | ) | const |
Prints the contents of the tree to stdout.
int BinaryTree< T >::height | ( | ) | const |
This lab deals with the following six helper functions:
void BinaryTree< T >::printLeftToRight | ( | ) | const |
Prints out the values of the nodes of a binary tree in order.
That is, everything to the left of a node will be printed out before that node itself, and everything to the right of a node will be printed out after that node.
BinaryTree< T >::Node * BinaryTree< T >::mirror | ( | ) |
Flips the tree over a vertical axis, modifying the tree itself (not creating a flipped copy).
Private helper function for the public mirror function.
subRoot | The current node in the recursion |
bool BinaryTree< T >::isOrdered | ( | ) | const |
Private helper function for the public isOrdered function.
subRoot |
void BinaryTree< T >::printPaths | ( | ) | const |
Prints out all the possible paths from the root of the tree to any leaf node.
Private helper function for the public printPaths function.
That is, all sequences starting at the root node and continuing downwards, ending at a leaf node. Paths ending in a left node should be printed before paths ending in a node further to the right.
node | The current node in the recursion |
path | An aggregated path to the current node |
int BinaryTree< T >::sumDistances | ( | ) | const |
Each node in a tree has a distance from the root node - the depth of that node, or the number of edges along the path from that node to the root.
Private helper function for the public sumDistances function.
This function returns the sum of the distances of all nodes to the root node (the sum of the depths of all the nodes). Your solution should take O(n) time, where n is the number of nodes in the tree.
node | The current node in the recursion |
numchildren | Used to count how many children there are in a subtree. |