Question

In: Computer Science

in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order...

in C programming

Write the code to traverse the binary tree using in-order, post-order and pre-order methods. Make sure to validate that your output is correct.

Review the terminology for tree structures and list structures. What is the root of the tree, what’s a parent, what’s a child, what’s an ancestor, what is an external node, what is an internal node, what is the head of the list, what is the tail of the list, etc.

Traverse the binary tree using the pre-order method and print out the data to a text file named “IWILLPASSEXAM2.txt”

Use the bubble sort method to sort [4 3 1 2]

Solutions

Expert Solution

Here is the answer to the first question:

#include<stdio.h>

#include<stdlib.h>

typedef struct m{

    struct m* left;

    int data;

    struct m* right;

}Node;

void inOrderTraversal(Node* root){

    if(!root){

        return;

    }

    inOrderTraversal(root -> left);

    printf(" %d " , root->data);

    inOrderTraversal(root -> right);

}

void preOrderTraversal(Node* root){

    if(!root){

        return;

    }

    printf(" %d " , root->data);

    preOrderTraversal(root -> left);

    preOrderTraversal(root -> right);

}

void postOrderTraversal(Node* root){

    if(!root){

        return;

    }

    postOrderTraversal(root -> left);

    postOrderTraversal(root -> right);

    printf(" %d " , root->data);

}

//Function for creating a node of binary tree

Node* createNode(int data){

    Node* node = malloc(sizeof(Node));

    if(!node){

        return NULL;

    }

    node->data = data;

    node->left = NULL;

    node->right = NULL;

    return node;

}

int main(){

    //Creating a tree to test the functions on

    /*The tree is

                        0

                      /   \

                     1     2

                    / \   / \

                   7   9 4   8

    */

    Node *root;

    root = createNode(0);

    root->left = createNode(1);

    root->right = createNode(2);

    root->left->left = createNode(7);

    root->left->right = createNode(9);

    root->right->left = createNode(4);

    root->right->right = createNode(8);

    printf("Preorder Traversal \n");

    preOrderTraversal(root);

    printf("\nInorder Traversal \n");

    inOrderTraversal(root);

    printf("\nPostorder Traversal \n");

    postOrderTraversal(root);

    return 0;

}

Image for indentation reference:


Related Solutions

(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...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string.
Programming CWrite 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 parameters: the root...
Code using C++ A binary search tree is a data structure designed for efficient item insertion,...
Code using C++ A binary search tree is a data structure designed for efficient item insertion, deletion, and retrieval. These 3 operations share an average run time complexity of O(log(n)). Such time complexities are guaranteed whenever you are working with a balanced binary search tree. However, if you have a tree that begins leaning heavily to either its left or right side, then you can expect the performances of the insertion, deletion, and retrieval operations to degrade. As an example,...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal of the tree. 50, 60, 25, 40, 30, 70, 35, 10, 55, 65, 5 Write an algorithm to delete a node in Singly Linked List                            [12 Write an algorithm of Binary Search                                                              [10] Write a program in ‘C’ to generate Fibonacci series using recursion            [8]
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use serializable interface implantation /** Class to encapsulate a tree node. */ protected static class Node implements Serializable {
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT