-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Binary Tree Sorting using C
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#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 ) ; | ||
} | ||
} |