Node* JoinHeap::joinHeap(Node* rootHeap, Node* heapToBeJoined){ if(rootHeap == NULL){ root = heapToBeJoined; return heapToBeJoined; }else if(heapToBeJoined == NULL){ return rootHeap; } if(rootHeap -> value > heapToBeJoined -> value){ Node* reassign = rootHeap; rootHeap = heapToBeJoined; heapToBeJoined = reassign; } rootHeap -> right = joinHeap(rootHeap->right,heapToBeJoined); int leftSize = findHeight(rootHeap->left); int rightSize = findHeight(rootHeap->right); if(leftSize < rightSize){ Node* leash = rootHeap -> left; rootHeap->left = rootHeap->right; rootHeap->right = leash; } return rootHeap; }
int JoinHeap::findHeight(Node* root){ if(root == NULL){ return -1; }else{ return (MIN(findHeight(root->left),findHeight(root->right)) + 1); } }
int findHeight(struct BstNode * root){ if(root=NULL){ return -1; } else{ return max(findHeight(root->left),findHeight(root->right))+1; } };
TreeNode *rightRoate (TreeNode *p) { TreeNode *q = p->left; p->left = q->right; q->right = p; findHeight(p); findHeight(q); return q; }
TreeNode *leftRoate (TreeNode *q) { TreeNode *p = q->right; q->right = p->left; p->left = q; findHeight(p); findHeight(q); return p; }
NodeT *leftRot(NodeT *node) { NodeT *root = node->right; NodeT *n1 = root->left; root->left = node; node->right = n1; node->height = max(findHeight(node->left), findHeight(node->right))+1; root->height = max(findHeight(root->left), findHeight(root->right))+1; return root; }
NodeT *rightRot(NodeT *root) { NodeT *n1 = root->left; NodeT *n2 = n1->right; n1->right = root; root->left = n2; root->height = max(findHeight(root->left), findHeight(root->right))+1; n1->height = max(findHeight(n1->left), findHeight(n1->right))+1; return n1; }
int findHeight(treeNode *aNode) { if (aNode == 0) return -1; int lefth = findHeight(aNode->left); int righth = findHeight(aNode->right); if (lefth > righth) return lefth + 1; else return righth + 1; }
long findHeight(Node *aNode) { if (aNode == 0) return -1; long lefth = findHeight(aNode->left); long righth = findHeight(aNode->right); if (lefth > righth) return lefth + 1; else return righth + 1; }
void printLevelOrder(treeNode* root1) { int h = findHeight(root1); int i; for (i = 1; i <= h; i++) { printGivenLevel(root1, i); printf("\n"); } }
//BstNode* findMin(BstNode* root) int main() { int a,b; struct BstNode* root = NULL; // Creating an empty tree /*Code to test the logic*/ root = Insert(root,15); root = Insert(root,10); root = Insert(root,20); root = Insert(root,25); root = Insert(root,8); root = Insert(root,12); // Ask user to enter a number. findHeight(root); }
NodeT* insertNode(NodeT* node, int data) { if (node == NULL) return(createNode(data)); else if (data < node->data) node->left = insertNode(node->left, data); else node->right = insertNode(node->right, data); node->height = max(findHeight(node->left), findHeight(node->right))+1; int bal=getBalance(node); if (bal>1 && data < node->left->data) return rightRot(node); if (bal<-1 && data>node->right->data) return leftRot(node); if (bal>1 && data>node->left->data) { node->left =leftRot(node->left); return rightRot(node); } if (bal<-1 && data<node->right->data) { node->right =rightRot(node->right); return leftRot(node); } return node; }
TreeNode *balanseNode (TreeNode *node) { findHeight(node); if (balanceFactor(node) == 2) { if (balanceFactor(node->right) < 0) node->right = rightRoate(node->right); return leftRoate(node); } if (balanceFactor(node) == -2) { if (balanceFactor(node->left) > 0) node->left = leftRoate(node->left); return rightRoate(node); } return node; }
treeNode *reBalance(treeNode *nodeN) { if (findHeight(nodeN->left) - findHeight(nodeN->right) > 1) { if (findHeight(nodeN->left->left) > findHeight(nodeN->left->right)) { nodeN = rotateRight(nodeN); } else { nodeN = rotateLeftRight(nodeN); } } else { if (findHeight(nodeN->right) - findHeight(nodeN->left) > 1) { if (findHeight(nodeN->right->right) > findHeight(nodeN->right->left)) { nodeN = rotateLeft(nodeN); } else { nodeN = rotateRightLeft(nodeN); } /* else node is balanced */ } } return nodeN; }
int main(void) { char inputMenu; char inputData; int menu = 0; int type; while (1) { printf("-----------------------\n"); printf("1. Insert a Node\n"); printf("2. Delete a Node\n"); printf("3. Destroy BST\n"); printf("4. Find a Node\n"); printf("5. Tree Traverse\n"); printf("6. Show the height of tree\n"); printf("7. Show the shape of Tree\n"); printf("8. Make the completed BST\n"); printf("9. Cut Tree\n"); printf("0. Exit\n"); printf("-----------------------\n"); printf("->"); fgets(&inputMenu, 3, stdin); menu = atoi(&inputMenu); switch (menu) { case 1: printf("Type the value to insert\n"); printf("->"); fgets(&inputData, 3, stdin); insertNode(inputData); break; case 2: printf("Type the node id to delete\n"); printf("->"); fgets(&inputData, 3, stdin); deleteNode(inputData); break; case 3: destroyBST(); break; case 4: printf("Type the node id to find\n"); printf("->"); fgets(&inputData, 3, stdin); findNode(inputData); break; case 5: printf("Type the type of Tree Traversal (0: Pre-order, 1: In-order, 2: Post-order)\n"); printf("->"); fgets(&inputData, 3, stdin); type = atoi(&inputData); printf("Type the node id to start Traverse\n"); printf("->"); fgets(&inputData, 3, stdin); treeTraversal(type, inputData); break; case 6: findHeight(); break; case 7: showTree(); break; case 8: makeCompleteBST(); break; case 9: printf("Type the node id that will become the new root node\n"); printf("->"); fgets(&inputData, 3, stdin); cutTree(inputData); break; case 0: exit(0); break; default: printf("Input should be 0-9\n"); break; } } return 0; }
int main(){ std::array<float,3> x = findHeight(true,false,false,1); std::cout<<"Max Height: "<<x[0]<<" Min Height: "<<x[1]<<" Target "<<x[2]; }
int findHeight(treeNode *node) { if (node == NULL) { return -1; } return findMaximum(findHeight(node->left), findHeight(node->right)) + 1; }
int getBalance(NodeT *root) { if (root == NULL) return 0; return (findHeight(root->left) - findHeight(root->right)); }
int Height (Tree *T) { int isBalanced = 0; return findHeight(T,&isBalanced); }
int Balanced (Tree *T) { int isBalanced = 1; findHeight(T,&isBalanced); return isBalanced; }
int main() { //treeNode *root = NULL; int x; int ch, num, num1; treeNode * temp; treeNode *med; int m = 1; do { printf("\nSelect a choice from the menu below."); printf("\n1. Generate Binary Search Tree"); printf("\n2. Print the BST in pre-order format"); printf("\n3. Print the BST in in-order format"); printf("\n4. Print the BST in post-order format"); printf("\n5. Print the BST in breadth-first format"); printf("\n6. Find a value in the BST"); printf("\n7. Find the minimum value in the BST nodes"); printf("\n8. Find the maximum value in the BST nodes"); printf("\n9. Calculate the average value of the BST nodes"); printf("\n10. Find the median value of the BST nodes"); printf("\n11. Calculate the sum of the BST nodes"); printf("\n12. Count the number of BST nodes"); printf("\n13. Delete a value in the BST"); printf("\n14. Exit Program"); printf("\n"); printf("\nEnter Choice: "); scanf("%d", &ch); switch (ch) { case 1: genrateTree(); printf("\n"); printf("\nTree in IN-ORDER : "); PrintInorder(root); printf("\n"); break; case 2: PrintPreorder(root); break; case 3: PrintInorder(root); break; case 4: PrintPostorder(root); break; case 5: printLevelOrder(root); break; case 6: printf("\nEnter the element to be Find in TREE: "); scanf("%d", &num); temp=Find(root, num); if (temp->data == num) { printf("Element Found\n"); } else { printf("Element NOT Found\n"); } break; case 7: temp = FindMin(root); printf("Minimum element is %d\n", temp->data); break; case 8: temp = FindMax(root); printf("Maximum element is %d\n", temp->data); break; case 9: in_order_sum(root); printf("Average element is %d\n", sum / counter(root)); sum = 0; break; case 10: med = medianTraverse(root, findHeight(root)); printf("Median Value is %d\n", med->data); break; case 11: in_order_sum(root); printf("\nThe sum of all the elements are:%d\n", sum); sum = 0; break; case 12: printf("Total Number of Nodes %d\n", counter(root)); break; case 13: PrintInorder(root); printf("\n"); printf("\nEnter the element to be Deleted: "); scanf("%d", &num); Delete(root, num); break; case 14: exit(0); break; default: exit(0); } //printf("%d", rootNode->data); printf("\n"); printf("\nIf you want to return to the menu, press 1."); printf("\nChoice: "); scanf("%d", &num); } while (num == 1); }