The BinaryTree class represents a templated linked-memory tree data structure.
More...
#include <binarytree.h>
template<typename T>
class BinaryTree< T >
The BinaryTree class represents a templated linked-memory tree data structure.
◆ BinaryTree() [1/3]
Constructor to create an empty tree.
◆ BinaryTree() [2/3]
Constructor to that wraps raw nodes as a BinaryTree class.
◆ BinaryTree() [3/3]
◆ ~BinaryTree()
Destructor; frees all nodes associated by this tree.
◆ clear() [1/2]
Frees all nodes associated with this tree and sets it to be empty.
◆ clear() [2/2]
Private helper function for clear that clears beneath the parameter node.
- Parameters
-
subRoot | The current node in the recursion |
◆ copy()
Helper function for operator= and cctor.
- Parameters
-
subRoot | The current node in the recursion |
◆ getPaths()
creates vectors of all the possible paths from the root of the tree to any leaf node and adds it to another vector.
Path is, all sequences starting at the root node and continuing downwards, ending at a leaf node. Paths ending in a left node should be added before paths ending in a node further to the right.
- Parameters
-
paths | vector of vectors that contains path of nodes |
◆ getRoot()
- Returns
- The root of the binary tree
-
The root of the binary tree.
◆ height() [1/2]
This lab deals with the following six helper functions:
- Returns
- The height of the binary tree. Recall that the height of a binary tree is just the length of the longest path from the root to a leaf, and that the height of an empty tree is -1.
◆ height() [2/2]
Put your own private helper functions here.
Private helper function for the public height function.
Look at the private helpers for height and printLeftToRight as examples.Private helper function for the public height function.
- Parameters
-
subRoot | The current node in the recursion |
- Returns
- The height of the subtree
- Parameters
-
- Returns
- The height of the subtree
◆ inOrder() [1/2]
Uses vector to store values of the nodes of a binary tree in order.
That is, everything to the left of a node will be pushed before that node itself, and everything to the right of a node will be pushed after that node.
- Parameters
-
treeVector | stores nodes in order |
◆ inOrder() [2/2]
Private helper function for the public inOrder function.
- Parameters
-
subRoot | The current node in the recursion |
treeVector | stores nodes in order |
◆ insert() [1/2]
Inserts into the BinaryTree in BST order.
- Parameters
-
elem | The element to insert |
◆ insert() [2/2]
Private helper function for the sorted public insert function.
- Parameters
-
node | The current node in the recursion |
elem | The element to insert |
◆ insertRandom() [1/2]
Inserts the given value into the BinaryTree, taking a random path to the leaf where it is inserted.
- Parameters
-
elem | The element to insert |
rng | The random number generator used to compute the path |
◆ insertRandom() [2/2]
Private helper function for the random public insert function.
- Parameters
-
node | The current node in the recursion |
elem | The element to insert |
rng | The random number generator used to compute the path |
◆ isOrderedIterative()
template<typename T >
bool BinaryTree< T >::isOrderedIterative |
( |
| ) |
const |
isOrdered() function iterative version
- Returns
- True if an in-order traversal of the tree would produce a nondecreasing list output values, and false otherwise. This is also the criterion for a binary tree to be a binary search tree.
◆ isOrderedRecursive()
template<typename T >
bool BinaryTree< T >::isOrderedRecursive |
( |
| ) |
const |
isOrdered() function recursive version
- Returns
- True if an in-order traversal of the tree would produce a nondecreasing list output values, and false otherwise. This is also the criterion for a binary tree to be a binary search tree.
◆ mirror()
Flips the tree over a vertical axis, modifying the tree itself (not creating a flipped copy).
◆ operator=()
Assignment operator.
- Parameters
-
rhs | The tree to make a copy of |
- Returns
- A reference to the current tree
◆ print()
Prints the contents of the tree to stdout.
◆ printLeftToRight() [1/2]
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.
◆ printLeftToRight() [2/2]
Private helper function for the public printLeftToRight function.
- Parameters
-
subRoot | The current node in the recursion |
subRoot | |
◆ sumDistances()
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.
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.
- Returns
- The sum of the distances of all nodes to the root
The documentation for this class was generated from the following files: