Using RUST implement a binary search treehttpsenwikipediaorg

 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikipedia.org/wiki/Tree_traversal).  The program must use the following public API.  ``` pub struct Tree<T> {     ... }  impl<T: Ord> Tree<T> {     /// Creates an empty tree     pub fn new() -> Self {         unimplemented!()     }      /// Returns `false` if `key` already exists in the tree, and `true` otherwise.     pub fn insert(&mut self, key: T) -> bool {         unimplemented!()     }      /// Returns `true` if `key` exists in the tree, and `false` otherwise.     pub fn find(&self, key: &T) -> bool {         unimplemented!()     }      /// Returns the preorder traversal of the tree.     pub fn preorder(&self) -> Vec<&T> {         unimplemented!()     }      /// Returns the inorder traversal of the tree.     pub fn inorder(&self) -> Vec<&T> {         unimplemented!()     }      /// Returns the postorder traversal of the tree.     pub fn postorder(&self) -> Vec<&T> {         unimplemented!()     } } 

Solution

/*

* C Program to Construct a Binary Search Tree and perform deletion, norder traversal on it

*/

#include <stdio.h>

#include <stdlib.h>

struct btnode

{

    int value;

    struct btnode *l;

    struct btnode *r;

}*root = NULL, *temp = NULL, *t2, *t1;

void delete1();

void insert();

void delete();

void inorder(struct btnode *t);

void create();

void search(struct btnode *t);

void preorder(struct btnode *t);

void postorder(struct btnode *t);

void search1(struct btnode *t,int data);

int smallest(struct btnode *t);

int largest(struct btnode *t);

int flag = 1;

void main()

{

    int ch;

    printf(\"\ OPERATIONS ---\");

    printf(\"\ 1 - Insert an element into tree\ \");

    printf(\"2 - Delete an element from the tree\ \");

    printf(\"3 - Inorder Traversal\ \");

    printf(\"4 - Preorder Traversal\ \");

    printf(\"5 - Postorder Traversal\ \");

    printf(\"6 - Exit\ \");

    while(1)

    {

        printf(\"\ Enter your choice : \");

        scanf(\"%d\", &ch);

        switch (ch)

        {

        case 1:   

            insert();

            break;

        case 2:   

            delete();

            break;

        case 3:   

            inorder(root);

            break;

        case 4:   

            preorder(root);

            break;

        case 5:   

            postorder(root);

            break;

        case 6:   

            exit(0);

        default :    

printf(\"Wrong choice, Please enter correct choice \");

            break;   

        }

    }

}

/* To insert a node in the tree */

void insert()

{

    create();

    if (root == NULL)

        root = temp;

    else   

        search(root);   

}

/* To create a node */

void create()

{

    int data;

    printf(\"Enter data of node to be inserted : \");

    scanf(\"%d\", &data);

    temp = (struct btnode *)malloc(1*sizeof(struct btnode));

    temp->value = data;

    temp->l = temp->r = NULL;

}

/* Function to search the appropriate position to insert the new node */

void search(struct btnode *t)

{

    if ((temp->value > t->value) && (t->r != NULL))      /* value more than root node value insert at right */

        search(t->r);

    else if ((temp->value > t->value) && (t->r == NULL))

        t->r = temp;

    else if ((temp->value < t->value) && (t->l != NULL))    /*

value less than root node value insert at left */

        search(t->l);

    else if ((temp->value < t->value) && (t->l == NULL))

        t->l = temp;

}

/* recursive function to perform inorder traversal of tree */

void inorder(struct btnode *t)

{

    if (root == NULL)

    {

        printf(\"No elements in a tree to display\");

        return;

    }

    if (t->l != NULL)   

        inorder(t->l);

    printf(\"%d -> \", t->value);

    if (t->r != NULL)   

        inorder(t->r);

}

/* To check for the deleted node */

void delete()

{

    int data;

    if (root == NULL)

    {

        printf(\"No elements in a tree to delete\");

        return;

    }

    printf(\"Enter the data to be deleted : \");

    scanf(\"%d\", &data);

    t1 = root;

    t2 = root;

    search1(root, data);

}

/* To find the preorder traversal */

void preorder(struct btnode *t)

{

    if (root == NULL)

    {

        printf(\"No elements in a tree to display\");

        return;

    }

    printf(\"%d -> \", t->value);

    if (t->l != NULL)   

        preorder(t->l);

    if (t->r != NULL)   

        preorder(t->r);

}

/* To find the postorder traversal */

void postorder(struct btnode *t)

{

    if (root == NULL)

    {

        printf(\"No elements in a tree to display \");

        return;

    }

    if (t->l != NULL)

        postorder(t->l);

    if (t->r != NULL)

        postorder(t->r);

    printf(\"%d -> \", t->value);

}


/* Search for the appropriate position to insert the new node /

void search1(struct btnode *t, int data)

{

    if ((data>t->value))

    {

        t1 = t;

        search1(t->r, data);

    }

    else if ((data < t->value))

    {

        t1 = t;

        search1(t->l, data);

    }

    else if ((data==t->value))

    {

        delete1(t);

    }

}

/* To delete a node */

void delete1(struct btnode *t)

{

    int k;

    /* To delete leaf node */

    if ((t->l == NULL) && (t->r == NULL))

    {

        if (t1->l == t)

        {

            t1->l = NULL;

        }

        else

        {

            t1->r = NULL;

        }

        t = NULL;

        free(t);

        return;

    }

    /* To delete node having one left hand child */

    else if ((t->r == NULL))

    {

        if (t1 == t)

        {

            root = t->l;

            t1 = root;

        }

        else if (t1->l == t)

        {

            t1->l = t->l;

        }

        else

        {

            t1->r = t->l;

        }

        t = NULL;

        free(t);

        return;

    }

    /* To delete node having right hand child */

    else if (t->l == NULL)

    {

        if (t1 == t)

        {

            root = t->r;

            t1 = root;

        }

        else if (t1->r == t)

            t1->r = t->r;

        else

            t1->l = t->r;

        t == NULL;

        free(t);

        return;

    }

/* To delete node having two child */

    else if ((t->l != NULL) && (t->r != NULL))

    {

        t2 = root;

        if (t->r != NULL)

        {

            k = smallest(t->r);

            flag = 1;

        }

        else

        {

            k =largest(t->l);

            flag = 2;

        }

        search1(root, k);

        t->value = k;

    }

}

/* To find the smallest element in the right sub tree */

int smallest(struct btnode *t)

{

    t2 = t;

    if (t->l != NULL)

    {

        t2 = t;

        return(smallest(t->l));

    }

    else   

        return (t->value);

}

/* To find the largest element in the left sub tree */

int largest(struct btnode *t)

{

    if (t->r != NULL)

    {

        t2 = t;

        return(largest(t->r));

    }

    else   

        return(t->value);

}

OR

# include <stdio.h>

# include <conio.h>

# include <stdlib.h>

typedef struct BST {

   int data;

   struct BST *lchild, *rchild;

} node;

void insert(node *, node *);

void inorder(node *);

void preorder(node *);

void postorder(node *);

node *search(node *, int, node **);

void main() {

   int choice;

   char ans = \'N\';

   int key;

   node *new_node, *root, *tmp, *parent;

   node *get_node();

   root = NULL;

   clrscr();

   printf(\"\ Program For Binary Search Tree \");

   do {

      printf(\"\ 1.Create\");

      printf(\"\ 2.Search\");

      printf(\"\ 3.Recursive Traversals\");

      printf(\"\ 4.Exit\");

      printf(\"\ Enter your choice :\");

      scanf(\"%d\", &choice);

      switch (choice) {

      case 1:

         do {

            new_node = get_node();

            printf(\"\ Enter The Element \");

            scanf(\"%d\", &new_node->data);

            if (root == NULL) /* Tree is not Created */

               root = new_node;

            else

               insert(root, new_node);

            printf(\"\ Want To enter More Elements?(y/n)\");

            ans = getch();

         } while (ans == \'y\');

         break;

      case 2:

         printf(\"\ Enter Element to be searched :\");

         scanf(\"%d\", &key);

         tmp = search(root, key, &parent);

         printf(\"\ Parent of node %d is %d\", tmp->data, parent->data);

         break;

      case 3:

         if (root == NULL)

            printf(\"Tree Is Not Created\");

         else {

            printf(\"\ The Inorder display : \");

            inorder(root);

            printf(\"\ The Preorder display : \");

            preorder(root);

            printf(\"\ The Postorder display : \");

            postorder(root);

         }

         break;

      }

   } while (choice != 4);

}

/*

Get new Node

*/

node *get_node() {

   node *temp;

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

   temp->lchild = NULL;

   temp->rchild = NULL;

   return temp;

}

/*

This function is for creating a binary search tree

*/

void insert(node *root, node *new_node) {

   if (new_node->data < root->data) {

      if (root->lchild == NULL)

         root->lchild = new_node;

      else

         insert(root->lchild, new_node);

   }

   if (new_node->data > root->data) {

      if (root->rchild == NULL)

         root->rchild = new_node;

      else

         insert(root->rchild, new_node);

   }

}

/*

This function is for searching the node from

binary Search Tree

*/

node *search(node *root, int key, node **parent) {

   node *temp;

   temp = root;

   while (temp != NULL) {

      if (temp->data == key) {

         printf(\"\ The %d Element is Present\", temp->data);

         return temp;

      }

      *parent = temp;

      if (temp->data > key)

         temp = temp->lchild;

      else

         temp = temp->rchild;

   }

   return NULL;

}

/*

This function displays the tree in inorder fashion

*/

void inorder(node *temp) {

   if (temp != NULL) {

      inorder(temp->lchild);

      printf(\"%d\", temp->data);

      inorder(temp->rchild);

   }

}

/*

This function displays the tree in preorder fashion

*/

void preorder(node *temp) {

   if (temp != NULL) {

      printf(\"%d\", temp->data);

      preorder(temp->lchild);

      preorder(temp->rchild);

   }

}

/*

This function displays the tree in postorder fashion

*/

void postorder(node *temp) {

   if (temp != NULL) {

      postorder(temp->lchild);

      postorder(temp->rchild);

      printf(\"%d\", temp->data);

   }

}

 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped
 Using RUST, implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikiped

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site