Question

In: Computer Science

i have an array of strings, how do i sort them into a binary tree? C...

i have an array of strings, how do i sort them into a binary tree? C Program

Solutions

Expert Solution

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

static int myCompareString(const void* s, const void* g)

{

    return strcmp(*(const char**)s, *(const char**)g);

}

void sort(const char* a[], int n)

{

    // calling qsort function to sort the array

    // with the help of Comparator

    qsort(a, n, sizeof(const char*),myCompareString);

}

  

int main()

{

    const char* a[]

        = { "hello ", "everyone", "there" };

  

    int n = sizeof(a) / sizeof(a[0]);

    int i;

  

    // Printing the given names

    printf("Given array is\n");

    for (i = 0; i < n; i++)

        printf("%d: %s \n", i, a[i]);

  

    // Sorting the given names

    sort(a, n);

  

    // Printing the sorted names

    printf("\nSorted array is\n");

    for (i = 0; i < n; i++)

        printf("%d: %s \n", i, a[i]);

  

    return 0;

}


Related Solutions

C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
You are provided with an array of Strings and a list of Strings. Sort the elements...
You are provided with an array of Strings and a list of Strings. Sort the elements (1) in natural order, (2) in reverse natural order and (3) by the length of each String. You can fill in the details by using the following stub file: import java.util.Arrays; import java.util.Collections; import java.util.List; public class Midterm01 { public static void main(String[] args) { String[] arrayOfCities = { "Atlanta", "Savannah", "New York", "Dallas", "Rio" }; List<String> listOfCities = Arrays.asList("Atlanta", "Savannah", "New York", "Dallas",...
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
I would like to integrate a bubble sort into this binary search in c ++ Thank...
I would like to integrate a bubble sort into this binary search in c ++ Thank you! #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) {         while (f <= l)         {             int m = f + (l - l) / 2;                 // Check if...
Use C language. I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the...
Use C language. I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
How manyn-digit binary strings have at least two 0s?
How manyn-digit binary strings have at least two 0s?
How do I write a C# and a C++ code for creating a character array containing...
How do I write a C# and a C++ code for creating a character array containing the characters 'p', 'i', 'n','e','P','I','N','E' only and then using these lower and capital case letter character generate all possible combinations like PInE or PinE or PIne or PINE or piNE etc. and only in this order so if this order is created eg. NeIP or EnPi or NeIP or IPnE and on. You can generate all the combinations randomly by creating the word pine...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree sort), will take the following command line arguments: % bstsort [-c] [-o output_file_name] [input_file_name] If -c is present, the program needs to compare the strings case sensitive; otherwise, it's case insensitive. If the output_file_name is given with the -o option,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT