int BinTree::heightHelper(const Node* target) const{ if(target == NULL){ return 0; } else { return max(heightHelper(target->left), heightHelper(target->right)) + 1; } }
int BinarySearchTree<Key, Value>::heightHelper( BinaryTreeNode<Key, Value>* subRoot) { if (subRoot == NULL) { return 0; } int leftHeight = heightHelper(subRoot->leftChild()); int rightHeight = heightHelper(subRoot->rightChild()); return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1; }
int AVL<T>::heightHelper(Node* rt){ if(rt == NULL) return 0; else{ int height, height_left, height_right; height_left = heightHelper(rt->left); height_right = heightHelper(rt->right); height = height_right > height_left ? height_right : height_left; return height + 1; } }
int BSTree<DataType, KeyType>::heightHelper(BSTreeNode *parent) const { if(parent == NULL) { return 0; } if(heightHelper(parent->left) > heightHelper(parent->right)) { return 1 + heightHelper(parent->left); } else { return 1 + heightHelper(parent->right); } }
//------------------------------------------------------------------------------ // getHeight // Returns the height of a given object // Leaves are height of one int Tree::getHeight(const NodeData &nd) const { Node *temp = _root; // Height of object if found int height = 0; heightHelper(temp, nd, height); return height; }
int BinTree::getHeight(const NodeData &source) const { Node* target = NULL; find(root,source,target); if(target != NULL){ return heightHelper(target); } else { return -1; } }
int Tree::heightHelper(Node * &curr, const NodeData &nd, int &height) const { if (curr != NULL) { // Finds height of left and right subtree int left = heightHelper(curr->left, nd, height); int right = heightHelper(curr->right, nd, height); // If both are zero, we are at leaf and return one // If leaf is target, set height to one if (left == 0 && right == 0) { if (*curr->data == nd) { height = 1; } return 1; } else { int result = 0; // Determines greater height of the left and right subtree (left > right) ? result = left : result = right; // If current is target, height is set to result +1 // +1 is to account for self if (*curr->data == nd) { height = result + 1; } // If not found, returns height +1 // +1 is to account for self return result + 1; } } // Not found or NULL else { return 0; } }
int AVL<T>::treeHeight(){ return heightHelper(root); }
int BSTree<DataType, KeyType>::getHeight () const { return heightHelper(root); }