Question

In: Computer Science

Write a function that takes a binary search tree as input and produces a linked list...

Write a function that takes a binary search tree as input and produces a linked list of the entries, with the entries sorted

(smallest entries at the front of the list and largest entries at the back). *Hint: use in-order traversal.*

C++

there is no any structure

Solutions

Expert Solution

#include<iostream>
using namespace std;
//assuming
//structure/node for BST IS
class node_bst
{
   public:
   int value;
   node_bst *left,*right;
   //constructor
   node_bst(int v)
   {
       value=v;
       left=right=NULL;
   }
};

//structure for linked list node
class lnode
{
   public:
   int value;
   lnode *next;
   //constructor
   lnode(int v)
   {
       value=v;
       next=NULL;  
   }
};

//class for linkedlist
class linked_list
{
   public:
   lnode *head,*tail;
   //constructor
   linked_list()
   {
       head=tail=NULL;  
   }  
   //method to add a node to linked list at tail
   void add(int v)
   {
       lnode *n = new lnode(v);//creating new node
       if(head==NULL)//if list is empty
       {
           head=tail=n;  
       }
       else
       {
           //adding at tail
           tail->next=n;
           tail=n;
       }
   }
  
  
   //method to create linked list with sorted entries from given binarysearchtree
   //by traversing in inorder
   void create_linked_list(node_bst *r)
   {
       if(r==NULL)return;
       create_linked_list(r->left);
       add(r->value);//adding to linked list
       create_linked_list(r->right);
      
   }
  
   //method to print linked list
   void print()
   {
       lnode *t=head;
       while(t!=NULL)
       {
           cout<<t->value<<" ";
           t=t->next;  
       }
       cout<<endl;
   }
};


int main()
{
   //now testing abovemethod
   //creating bst
   node_bst *b = new node_bst(5);
   b->left=new node_bst(3);
   b->right=new node_bst(7);
   b->left->right = new node_bst(4);
   b->right->left= new node_bst(6);
  
  
   //now creating sorted linked list from bst, using implemented method
   linked_list *l = new linked_list();
   l->create_linked_list(b);
   //now printing linked list
   l->print();
}

output:

3 4 5 6 7


Process exited normally.
Press any key to continue . . .


//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.


//if you have any doubts, ask me in the comments


Related Solutions

write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
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,...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT