java - Would building a max heap from an Unsorted array would follow Binary Tree properties? -


given unsorted array of size 10

int[] arr={∞,1,2,3,4,5,6,7,8,9,10}; 

if execute code

public void build_heap(){             for(int i=size/2;i>=1;i--)                     max_heapify(i); } 

the resulting array in what case follow binary tree properties

( ie left subtree < root & root < right subtree )

? how generate such array ?

is right approach : instead of using build_heap , keep keep inserting element heap ? (is there better solution?)

no, confused on data structures.

heaps not guarantee ordering in way bst(binary search tree) does. heaps guarantee given node's children satisfy heap property (ie. either less given subtree root max heap, or greater min heap).

bst's on other hand, guarantee given subtree's root has keys less root's key on left subtree, , keys greater on right subtree.

edit: run heapsort on heap generate sorted array in nlogn time, construct balanced bst in linear time.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -