Пример #1
0
bool isBalanced(BinarySearchTree &t){
  countDepthRecursive(t.getRoot(), 0);
  Node* checker = leafs.front();
  for (vector<Node*>::iterator it = leafs.begin()+1; it != leafs.end(); ++it) {
    if (abs(checker->depth - (*it)->depth) > 1) {
      cout << "node " << checker->value << " depth " << checker->depth << " node " << (*it)->value << " depth " << (*it)->depth << endl;
      return false;
    }
  }
  return true;
}
Пример #2
0
void TestBinaryTreeHeight2() {
  BinarySearchTree<BSTNode1<int>, int> b;
  int test = 0;

  for (int i = 0; i < 10000; i++) {
    test = rand() % 100000;
    b.add(test);
  }

  assert(b.height() - b.depth(b.getNode(test)) == b.height2(b.getNode(test)));
  assert(b.height2(b.getRoot()) == b.height());
}
Пример #3
0
int main() {
  int N;
  cin >> N;
  BinarySearchTree t;
  srand(time(NULL));
  for (int i = 0; i < N; i++) {
    int randNum = 1 + rand() % 100;
    t.insert(randNum);
  }
  t.dfsRecursive(PREORDER, t.getRoot());
  cout << "is balanced: " << isBalanced(t) << endl;

  return 0;
}
BinarySearchTree<T> BinarySearchTree<T>::createMirror() {
  BinarySearchTree<T> tt = *this;
  
  tt.swapChildren(tt.getRoot());
  return tt;
}
BinarySearchTree<T>::BinarySearchTree(const BinarySearchTree& bb) {
  root = NULL;
  Node<T> *curNode = bb.getRoot();
  
  copyTree(curNode);
}