Question

1. Write a program in C to show how to traverse a tree or visit each node in the tree exactly once? Use the following figure to implement the three methods, LVR (inorder), LRV (postorder), VLR (preorder). 17 1819 14 12 8 Figure 5.15: Binary tree with arithmetic expression
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Assuming TreeNode has following structure:
struct Node {
    char value;    // value stored in node
    Node * left;   // left child
    Node * right;  // right child
};

===============================
Below methods should be called
with input paramter root of tree.
===============================
void inorder(TreeNode * t) {
  if(t != NULL) {
    inorder(t->left);
    printf("%c ", t->value);
    inorder(t->right);
  }
}

/* should visit the nodes in preorder fashion */
void preorder(TreeNode * t) {
  if(t != NULL) {
    printf("%c ", t->value);
    preorder(t->left);
    preorder(t->right);
  }
}

void postorder(Node * t) {
  if(t != NULL) {
    preorder(t->left);
    preorder(t->right);
    printf("%c ", t->value);
  }
}

============
Working code:

#include <iostream>

using namespace std;

struct TreeNode {
    char value;    // value stored in node
    TreeNode * left;   // left child
    TreeNode * right;  // right child
};

/*===============================
Below methods should be called
with input paramter root of tree.
===============================*/
void inorder(TreeNode * t) {
  if(t != NULL) {
    inorder(t->left);
    printf("%c ", t->value);
    inorder(t->right);
  }
}

/* should visit the nodes in preorder fashion */
void preorder(TreeNode * t) {
  if(t != NULL) {
    printf("%c ", t->value);
    preorder(t->left);
    preorder(t->right);
  }
}

void postorder(TreeNode * t) {
  if(t != NULL) {
    preorder(t->left);
    preorder(t->right);
    printf("%c ", t->value);
  }
}

int main() {
        TreeNode *root = new TreeNode;
        root->value = '+';
        root->right = new TreeNode;
        root->right->value = 'E';

        TreeNode* x = new TreeNode;
        x->value = '*';
        root->left = x;
        x->right = new TreeNode;
        x->right->value = 'D';

        TreeNode* y = new TreeNode;
        y->value = '*';
        x->left = y;
        x = y;
        x->right = new TreeNode;
        x->right->value = 'C';

        y = new TreeNode;
        y->value = '/';
        x->left = y;
        x = y;
        x->right = new TreeNode;
        x->right->value = 'B';

        y = new TreeNode;
        y->value = 'A';
        x->left = y;

        printf("\nInorder: ");
        inorder(root);
        printf("\nPreorder: ");
        preorder(root);
        printf("\nPostorder: ");
        postorder(root);
}
Add a comment
Know the answer?
Add Answer to:
1. Write a program in C to show how to traverse a tree or visit each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Extend the array based Binary Tree lab by adding these methods. (Remember 2i + 1 &...

    Extend the array based Binary Tree lab by adding these methods. (Remember 2i + 1 & 2i + 2) Preorder(int) -Recursive method that prints all the nodes in a VLR pattern. Inorder(int) -Recursive method that prints all the nodes in a LVR pattern. Postorder(int) -Recursive method that prints all the nodes in a LRV pattern. C++, no libraries are allowed

  • PYTHON 3 CODING Review the following tree and then implement Depth First Traversals. Your program should...

    PYTHON 3 CODING Review the following tree and then implement Depth First Traversals. Your program should display the output from Inorder to Preorder and Postorder. Include the following items in your answer: Write the implementation for Inorder, Preorder, and Postorder Print the output for each of the Inorder, Preorder, and Postorder Use the following steps to help with your solution: Create the node structure. Each node structure should contain the data, left node and right node. Create a function to...

  • LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display...

    LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.

  • Fill a tree called Pine with 25 elements from an input file. Traverse the tree using...

    Fill a tree called Pine with 25 elements from an input file. Traverse the tree using each of the following methods. Print the smallest element in the binary search tree, Pine. Find the number of edges between the root of the tree and the node that contains the smallest value in the tree. Return the count to the calling unit. Count the number of internal nodes in the original tree, Pine. Print the count and return it to the calling...

  • Please help me with this C++ program, thank you!!! You are to develop some binary tree...

    Please help me with this C++ program, thank you!!! You are to develop some binary tree routines that will handle single words. The binary tree is to be maintain as an ordered tree. The routines you need are: ADD (add a new word to tree, do not allow duplicates), DELETE ( deletes a word out of the tree), SEARCH (look up a word in the tree and indicate the word is in the structure or not) TRAVERSE ( inorder, preorder,...

  • (1) (50%) Write a C program that takes as input a fully parenthesized, arithmetic expression of...

    (1) (50%) Write a C program that takes as input a fully parenthesized, arithmetic expression of binary operators +, -,*,/, and converts the expression into a binary expression tree. Your program should take input from the command line. The entire expression should be in a character string without any space in it An input string only includes floating numbers in the format of Y.YY, that is, one digit to the left of the decimal point and two digits to the...

  • Write a C program to build a complete binary tree with 7 nodes using link list...

    Write a C program to build a complete binary tree with 7 nodes using link list implementation. Requirements: 1) Ask the user to enter randomly seven integer numbers as data value for each node 2) Build the tree using link list 3) Print the tree in inorder , preorder , and post order 4) Use functions for each print type .

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT