Question

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 .

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *left,*right;
};
struct node *root=NULL;
void insert(struct node *,int num);
void rec_pre(struct node *);
void rec_in(struct node *);
void rec_post(struct node *);
void main()
{
int cnt=7,i;
int no[7];
system("cls");
printf("\nEnter the values:\t");
for(i=0;i<cnt;i++)
scanf("%d",&no[i]);  
for(i=0;i<cnt;i++)   
insert(root,no[i]);
printf("\nIn Preorder\t:");
rec_pre(root); // function to print in preorder
printf("\nIn Inorder\t:");
rec_in(root); // function to print in inorder
printf("\nIn Postorder\t:");
rec_post(root); // function to print in postorder
}
void rec_pre(struct node *ptr)
{
if(ptr!=NULL)
{
printf("%d\t",ptr->info);
rec_pre(ptr->left);
rec_pre(ptr->right);
}
else
return;
}
void rec_in(struct node *ptr)
{
if(ptr!=NULL)
{
rec_in(ptr->left);
printf("%d\t",ptr->info);
rec_in(ptr->right);
}
else
return;
}
void rec_post(struct node *ptr)
{
if(ptr!=NULL)
{
rec_post(ptr->left);
rec_post(ptr->right);
printf("%d\t",ptr->info);
}
else
return;
}
void insert(struct node *ptr,int num)
{
struct node *temp,*parent;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=num;
temp->left=NULL;
temp->right=NULL;
if(ptr==NULL) // if initially tree has no node i.e. tree is empty
root=temp;
else
{
while(ptr!=NULL) // finding the next empty position
{
parent=ptr;
if(num<ptr->info)
ptr=ptr->left;
else
ptr=ptr->right;
}
if(num<parent->info)
parent->left=temp;
else
parent->right=temp;
}
}
EARimjhim)question.c Dev-C+5.11 File Edit Search View Project Execute Tools AStyle WindowHelp Project Classes Debug ree.c question.c include<stdio.h> #includecna lloc. h> 1 3 struct node int info; struct node left, right struct node root-NULL3 9 void insert(struct node ,int num)3 18 void rec _pre(struct node *); 11 void rec in(struct node )j 12 void rec_post (struct node )j 13 void main() 14 15 16 17 18 19 20 21 int cnt-7,ij int nol7]j system(cls)5 printf(nEnter the values:\t); for(i-8ji<cntji++) scanf(%d,&no[i]); for(i-8;i<cnti++) insert (root, no[i]); 23 24 25 26 27 28 printf(nIn Preorder\t:) rec pre(root)i printf(AnIn Inorderlt: rec in(root); printf(nIn Postorder\t:) rec post (root)i I function to print in preorder // function to print in tnorder /7 function to print in postorder 83 Compiler-Resources dh compile Log e Debug鳳Find Results Close Compilation results.. -Errors: 0 -Warnings: 0 Activate Windows Go to Settings to activate Windows □Shorten compiler paths |-Output Filename: E:\Rinj himlquestion.exe Line: Col: 36 Sel: 0 Lines: 88 Length: 1877 Done parsing in 0.031 seconds O Type here to search 12:29 PM ea dx 11/20/2018 ^ DOUTPUT SCREENSHOT:

Add a comment
Know the answer?
Add Answer to:
Write a C program to build a complete binary tree with 7 nodes using link list...
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
  • C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N...

    C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...

  • Write a program in C fro Binary Search tree using following functions 1. Insertion operation using...

    Write a program in C fro Binary Search tree using following functions 1. Insertion operation using recursion 2. Deletion operation 3. Minimum/Maximum of a BST 6. Reorganize the tree so that the tree height is minimum 7. Print all the nodes from the node to the path to the root 8. Find the lowest common shared node between two given nodes

  • [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree,...

    [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...

  • A Binary Search Tree is a binary tree where nodes are ordered in the following way:...

    A Binary Search Tree is a binary tree where nodes are ordered in the following way: each node contains one key (also known as data) the keys in the left subtree are less than the key in its parent node the keys in the right subtree are greater than the key in its parent node duplicate keys are not allowed Create a Binary Search Tree inserting the following list of numbers in order from left to right. 10,          6,           4,            8,            18,          15,          21 Please type...

  • 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);...

  • Write a (void) method to traverse a binary tree in Breadth-First order, where you start from...

    Write a (void) method to traverse a binary tree in Breadth-First order, where you start from the root, print the node values of its children, (left and then right) (these are at level 1), then print all node values at level 2 from left to right, and then level 3 etc. For the example binary tree below, you should get the result A, B, E, C, F, D, G, H. Notice that this type of traversal is not the same...

  • 1. Write a program in C to show how to traverse a tree or visit each...

    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

  • A collection of nodes is arranged as a binary search tree ordered on the field INFO which contain...

    A collection of nodes is arranged as a binary search tree ordered on the field INFO which contains distinct positive integers for each of the nodes. In addition to INFO, LLINK and RLINK, each node has three other fields CLASS SUCC and PRED CLASS is an information field containing a single letter that denotes the class to which the node belongs (there being up to 26 classes). The nodes in each class are arranged as a doubly-linked circular list with...

  • Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not...

    Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree.    Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...

  • 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...

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