int FindHeight(struct TNode* root){ if(!root){ return 0; } int height=max(FindHeight(root->left),FindHeight(root->right)); return height+1; }
int FindHeight(BtNode root) { int height; if(root == NULL){ height = -1; return height; } printf("testing: node is %d\n", root->data); height = ChooseMax(FindHeight(root->left), FindHeight(root->right)) + 1; printf("testing: height is %d\n", height); return height; }
int FindHeight(struct node* root) { if (root==NULL) return -1; int left_height,right_height; left_height=FindHeight(root->left); right_height=FindHeight(root->right); if(left_height>right_height) { return left_height+1; } else { return right_height+1; } }
int main(){ struct TNode* root=NULL; printf("\nEnter the number of elements you want to insert : "); int n; scanf("%d",&n); int i,data; for(i=0;i<n;i++){ printf("\nEnter data : "); scanf("%d",&data); InsertBinaryTree(&root,data,n); } printf("\nInserted... %d nodes \n",n); getch(); printf("\nPress enter to do level-order traversal : \n\n"); getch(); LevelOrderTraversal(root,n); getch(); printf("\nPress enter to find the height of the tree : "); getch(); int height=FindHeight(root); printf("\nHeight of the tree is : %d",height); getch(); return 0; }
int main() { struct node* root=NULL; printf("How many nodes you want in tree:\n"); int nodes,i,data; scanf("%d",&nodes); for(i=0;i<nodes;i++) { printf("Enter the value\n"); scanf("%d",&data); root=Insert(root,data); } int element,found; printf("Enter the element you want to search\n"); scanf("%d",&element); found=Search(root,element); if(found==0) printf("Not found\n"); else if(found==1) printf("Found\n"); int min=FindMin(root); printf("min is %d\n",min); int max=FindMax(root); printf("max is %d\n",max); int Height=FindHeight(root); printf("Heigth of tree is %d\n",Height); }
bool IsBalancedTree(BtNode root) { if(root == NULL) { return true; } if(abs(FindHeight(root->left) - FindHeight(root->right)) > 1) { return false; } else { if(IsBalancedTree(root->left) && IsBalancedTree(root->right)) { return true; } else { return false; } } }