What is the difference between binary tree sort and heap sort in data structure
Hey friends, I am a student of BCA where in I am having a subject as data structure in which I am having problem in binary tree sort and heap sort. I don’t understand this as I have tried more than a number of times. Is there any term known as node? If there than is it n binary tree sort or heap sort. I confused with both this term. I also wanted to know what are the difference between binary tree sort and heap sort.
Re: What is the difference between binary tree sort and heap sort in data structure
Binary tree search uses a binary search tree. In this method each element is scanned from input list and placed in its proper position in a binary tree. In binary tree each element is known as node. To place an element in its proper position the element is compared with the node element, and then it is placed in the left branch if the element is greater than or equal to the node then it is placed in the right branch. Now if we access the element according to in order traversal we would get the elements in ascending order.
Re: What is the difference between binary tree sort and heap sort in data structure
Code:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct btreenode
{
struct btreenode *leftchild ;
int data ;
struct btreenode *rightchild ;
} ;
void insert ( struct btreenode **, int ) ;
void inorder ( struct btreenode * ) ;
void main( )
{
struct btreenode *bt ;
int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1,
90, 3 } ;
int i ;
bt = NULL ;
clrscr( ) ;
printf ( "Binary tree sort.\n" ) ;
printf ( "\nArray:\n" ) ;
for ( i = 0 ; i <= 9 ; i++ )
printf ( "%d\t", arr[i] ) ;
for ( i = 0 ; i <= 9 ; i++ )
insert ( &bt, arr[i] ) ;
printf ( "\nIn-order traversal of binary
tree:\n" ) ;
inorder ( bt ) ;
getch( ) ;
}
void insert ( struct btreenode **sr, int num )
{
if ( *sr == NULL )
{
*sr = malloc ( sizeof ( struct btreenode ) ) ;
( *sr ) -> leftchild = NULL ;
( *sr ) -> data = num ;
( *sr ) -> rightchild = NULL ;
}
else
{
if ( num < ( *sr ) -> data )
insert ( &( ( *sr ) -> leftchild ), num ) ;
else
insert ( &( ( *sr ) -> rightchild ), num ) ;
}
}
void inorder ( struct btreenode *sr )
{
if ( sr != NULL )
{
inorder ( sr -> leftchild ) ;
printf ( "%d\t", sr -> data ) ;
inorder ( sr -> rightchild ) ;
}
}
This is an example of binary search sort in C language where you will find how stack is been used in this sorting. If you observe carefully you will find the concept of binary tree and node also. If you execute this program you will come to know the process of binary tree sorting.
Re: What is the difference between binary tree sort and heap sort in data structure
In this method, a tree structure called heap is used .A heap is type of binary tree. An ordered balanced binary tree is called min heap where the value at the root of any sub tree is less than or equal to the value of either of its children. An ordered balanced binary tree is called max heap when the value at the root of any sub tree is more than or equal to the value of either of its children. It is not necessary that the two children must be in some order. Sometime the value in the left child may be more than the value at right child and some other times it may be the other way round.