Mix and incorporate these code with binary search engine BST  If root == NULL return NULL; If number == root->data return root->data; If number < root->data return search(root->left) If number > root->data return search(root->right) #include      //Represent a node of the singly linked list   struct node{       int data;       struct node *next;   };           //Represent the head and tail of the singly linked list   struct node *head, *tail = NULL;       //addNode() will add a new node to the list   void addNode(int data) {       //Create a new node       struct node *newNode = (struct node*)malloc(sizeof(struct node));       newNode->data = data;       newNode->next = NULL;              //Checks if the list is empty       if(head == NULL) {           //If list is empty, both head and tail will point to new node           head = newNode;           tail = newNode;       }       else {           //newNode will be added after tail such that tail's next will point to newNode           tail->next = newNode;           //newNode will become new tail of the list           tail = newNode;       }   }       //removeDuplicate() will remove duplicate nodes from the list   void removeDuplicate() {       //Node current will point to head       struct node *current = head, *index = NULL, *temp = NULL;              if(head == NULL) {           return;       }       else {           while(current != NULL){               //Node temp will point to previous node to index.               temp = current;               //Index will point to node next to current               index = current->next;                              while(index != NULL) {                   //If current node's data is equal to index node's data                   if(current->data == index->data) {                       //Here, index node is pointing to the node which is duplicate of current node                       //Skips the duplicate node by pointing to next node                       temp->next = index->next;                   }                   else {                       //Temp will point to previous node of index.                       temp = index;                   }                   index = index->next;               }               current = current->next;           }               }   }       //display() will display all the nodes present in the list   void display() {       //Node current will point to head       struct node *current = head;       if(head == NULL) {           printf("List is empty \n");           return;       }       while(current != NULL) {           //Prints each node by incrementing pointer           printf("%d ", current->data);           current = current->next;       }       printf("\n");   }          int main()   {       //Adds data to the list       addNode(1);       addNode(2);       addNode(3);       addNode(2);       addNode(2);       addNode(4);       addNode(1);              printf("Originals list: \n");       display();              //Removes duplicate nodes       removeDuplicate();              printf("List after removing duplicates: \n");       display();                  return 0;   }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Mix and incorporate these code with binary search engine

BST 

If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)

#include <stdio.h>  
  
//Represent a node of the singly linked list  
struct node{  
    int data;  
    struct node *next;  
};      
   
//Represent the head and tail of the singly linked list  
struct node *head, *tail = NULL;  
   
//addNode() will add a new node to the list  
void addNode(int data) {  
    //Create a new node  
    struct node *newNode = (struct node*)malloc(sizeof(struct node));  
    newNode->data = data;  
    newNode->next = NULL;  
      
    //Checks if the list is empty  
    if(head == NULL) {  
        //If list is empty, both head and tail will point to new node  
        head = newNode;  
        tail = newNode;  
    }  
    else {  
        //newNode will be added after tail such that tail's next will point to newNode  
        tail->next = newNode;  
        //newNode will become new tail of the list  
        tail = newNode;  
    }  
}  
   
//removeDuplicate() will remove duplicate nodes from the list  
void removeDuplicate() {  
    //Node current will point to head  
    struct node *current = head, *index = NULL, *temp = NULL;  
      
    if(head == NULL) {  
        return;  
    }  
    else {  
        while(current != NULL){  
            //Node temp will point to previous node to index.  
            temp = current;  
            //Index will point to node next to current  
            index = current->next;  
              
            while(index != NULL) {  
                //If current node's data is equal to index node's data  
                if(current->data == index->data) {  
                    //Here, index node is pointing to the node which is duplicate of current node  
                    //Skips the duplicate node by pointing to next node  
                    temp->next = index->next;  
                }  
                else {  
                    //Temp will point to previous node of index.  
                    temp = index;  
                }  
                index = index->next;  
            }  
            current = current->next;  
        }          
    }  
}  
   
//display() will display all the nodes present in the list  
void display() {  
    //Node current will point to head  
    struct node *current = head;  
    if(head == NULL) {  
        printf("List is empty \n");  
        return;  
    }  
    while(current != NULL) {  
        //Prints each node by incrementing pointer  
        printf("%d ", current->data);  
        current = current->next;  
    }  
    printf("\n");  
}  
      
int main()  
{  
    //Adds data to the list  
    addNode(1);  
    addNode(2);  
    addNode(3);  
    addNode(2);  
    addNode(2);  
    addNode(4);  
    addNode(1);  
      
    printf("Originals list: \n");  
    display();  
      
    //Removes duplicate nodes  
    removeDuplicate();  
      
    printf("List after removing duplicates: \n");  
    display();  
          
    return 0;  
}  

15 node* binarySearch (int searchItem)
16 {
// first is pointing to beginning of node list
int first = 0;
17
18
19
// points to end
int last = 5;
20
21
22
// points to middle
int mid;
23
24
25
26
bool found = false;
27
// this will compare and set found when it finds it
while (first <= last && !found)
{
mid =
28
29
30
(first + last) / 2;
31
32
// this = your node list
if (this->at(mid)
found = true;
else if (this->at(mid) > searchItem)
last = mid 1;
33
34
searchItem)
==
35
36
37
else
first mid + 1;
38
39
40
}
41
if (found)
return mid;
42
43
44
else
45
return -1;
46 }
Transcribed Image Text:15 node* binarySearch (int searchItem) 16 { // first is pointing to beginning of node list int first = 0; 17 18 19 // points to end int last = 5; 20 21 22 // points to middle int mid; 23 24 25 26 bool found = false; 27 // this will compare and set found when it finds it while (first <= last && !found) { mid = 28 29 30 (first + last) / 2; 31 32 // this = your node list if (this->at(mid) found = true; else if (this->at(mid) > searchItem) last = mid 1; 33 34 searchItem) == 35 36 37 else first mid + 1; 38 39 40 } 41 if (found) return mid; 42 43 44 else 45 return -1; 46 }
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY