Ejemplo n.º 1
0
int MaxDepth(BinTree r) {
	if (r == NULL)
		return 0;
	int left = MaxDepth(r->lchild);
	int right = MaxDepth(r->rchild);
	return max(left, right) + 1;
}
Ejemplo n.º 2
0
int isBalanced(treeElem* root)
{
	if (root == 0)
		return 1;
	else
	{
		return isBalanced(root->left)&&isBalanced(root->right)&&(abs(MaxDepth(root->left)-MaxDepth(root->right)) <= 1);
	}

}
Ejemplo n.º 3
0
int MaxDepth(treeElem* root)
{
	int left, right, max;
	if (root == 0)
		return 0;
	else
	{
		left = MaxDepth(root->left);
		right = MaxDepth(root->right);
		max = (left > right) ? left:right;
		return max +1;
	}
}
Ejemplo n.º 4
0
void testProblems() {
	BinTree r, s;
	freopen("input/BinTree1.txt", "r", stdin);
	r = Create_BinTree();
	s = Create_BinTree();
	printf("%d\n", IsSubTree(r, s));
	freopen("input/BinTree2.txt", "r", stdin);
	int res = 0;
	r = Create_BinTree();
	MaxPathSum(r, res);
	printf("%d\n", res);
	printf("%d\n", MinDepth(r));
	printf("%d\n", MaxDepth(r));
	printf("%d\n", IsAvlTree(r));
	printf("%d\n", IsSymmetricTree(r));
	freopen("input/BinTree4.txt", "r", stdin);
	r = Create_BinTree();
	PreOrder(r);
	printf("\n");
	MirrorTree(r);
	PreOrder(r);
	printf("\n");
	freopen("input/BinTree3.txt", "r", stdin);
	r = Create_BinTree();
	BinNode *p = LastCommonParent(r, 10, 13);
	if (p) {
		printf("%d\n", ToInt(p));
	}
    int node = findSucc(r,6);
    printf("%d\n", node);
}
Ejemplo n.º 5
0
int main(void){

  int i=0, array[]={34,12,65,23,2,100,120};
  Node* root = NULL;
  root = TreeInit(49);

  for (i=0;i<7;++i){
    NodeInsert(root,array[i]);
  }

  InorderPrintTree(root);

  putchar('\n');

  PostorderPrintTree(root);

  printf("\nAddress of node with key=13 is %p\n",(void *)TreeSearch(root,13));

  printf("Minimum : %d \n", (TreeMinimum(root))->key);

  printf("Maximum : %d \n", (TreeMaximum(root))->key);

  printf("Treesize : %d \n", TreeSize(root));
  
  printf("Max depth : %d \n", MaxDepth(root));

  TreeDelete(root);

getchar();
return 0;
}
Ejemplo n.º 6
0
void TestCreateTree(const std::vector<int>& testVector) {
    Node* tree = CreateTree(testVector.begin(), testVector.end());
    std::vector<int> traversedVector;
    InOrderTraversal(tree, traversedVector);
    CHECK_EQUAL(traversedVector, testVector);
    // CHECK_EQUAL(CheckBalanced(tree), true);
    int minimalHeight = testVector.size() == 0?
                        0 : floor(std::log2(testVector.size())) + 1;
    CHECK_EQUAL(MaxDepth(tree), minimalHeight);
    DeleteTree(tree);
}
Ejemplo n.º 7
0
int main()
{
	int * testlist=0;
	int *testlist2=0;
	int testlist_len=100;
	treeElem* root;
	int test, i;

	makelist(&testlist, testlist_len);	// a list for test is built

	BSTinsert(&root, testlist, 0, testlist_len-1);

	BSTInorder(root);
	printf("\n");

	//printf("value = %d\n",testlist[0]);

	test = MaxDepth(root);

	printf("it has %d lvls\n", test);

	test = isBalanced(root);

	printf("balance condition is %d\n", test);

	test = ElemCounts(root);

	printf("this tree contains %d of elements\n", test);

	tree2arr(root, &testlist2, test);

	for (i=0; i<100; i++)
		printf("%d ",testlist2[i]);

	printf("\n");

	free(testlist);
	return 0;
}
Ejemplo n.º 8
0
int main(){

	node* root = NULL;
	Insert(root, 4);
	Insert(root, 2);
	Insert(root, 6);
	Insert(root, 1);
	Insert(root, 3);

	PrintPaths(root);
	std::cout << std::endl;
	int x = Size(root);
		std::cout << x <<" ---" << std::endl;
	int y = MaxDepth(root);	
	std::cout << "depth= " << y << std::endl;

	int min = MinValue(root);
	std::cout << "min= " << min << std::endl;

	if(HasPathSum(root,10))
		std::cout <<"yessss"<< std::endl;
return 0;
}
Ejemplo n.º 9
0
bool CheckBalanced(const Node* tree) {
    return MaxDepth(tree) - MinDepth(tree) <= 1;
}
Ejemplo n.º 10
0
int MaxDepth(const Node* tree) {
    if (tree == nullptr) {
        return 0;
    }
    return 1 + std::max(MaxDepth(tree->left), MaxDepth(tree->right));
}
Ejemplo n.º 11
0
bool IsBalanced(Node<T> *root) {
    return (MaxDepth(root) - MinDepth(root) <= 1);
}
Ejemplo n.º 12
0
int MaxDepth(Node<T> *root) {
    if (!root)
        return 0;
    return std::max(MaxDepth(root->left), MaxDepth(root->right)) + 1;
}
Ejemplo n.º 13
0
 int MaxDepth()
 {
     return MaxDepth(root);
 }
Ejemplo n.º 14
0
 int MaxDepth(Node* in_root)
 {
     if(NULL==in_root)
         return 0;
     return Max(MaxDepth(in_root->left), MaxDepth(in_root->right))+1;
 }