Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ArrayHeapChecker {
- //Check if the given array is a representation of a binary tree
- public static boolean isBinaryTree(Integer[] array) {
- if (array == null) {
- return false;
- }
- else if (array.length == 1 || array.length == 0) {
- return true;
- }
- for (int i=1; i<array.length; i++) {
- if (array[i]!=null && array[(int)((i-1)/2)]== null) {
- return false;
- }
- }
- return true;
- }
- //Check if the given array is a complete binary tree
- public static boolean isCompleteBinaryTree(Integer[] array) {
- if(!isBinaryTree(array)) {
- return false;
- }
- // If index assigned to current node is more than number of nodes in tree, then tree is not complete
- int numberOfNodes = 0;
- for (int i=0; i<array.length; i++) {
- if (array[i]!=null) {
- numberOfNodes++;
- }
- }
- for (int j=0; j<array.length; j++) {
- if (array[j]!=null && j >= numberOfNodes) {
- return false;
- }
- }
- return true;
- }
- //Check if the given array is a min-heap
- public static boolean isMinHeap(Integer[] array) {
- if(!isCompleteBinaryTree(array)) {
- return false;
- }
- for (int i=1; i<array.length; i++) {
- if (array[i]!=null && (array[(int)((i-1)/2)] > array[i])) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement