Question

In: Computer Science

Please do this code with python. Thank you! struct Node {     int data;     struct...

Please do this code with python. Thank you!

struct Node {

    int data;

    struct Node* left;

    struct Node* right;

};

// Node creation

struct Node* newNode(int data)

{

    struct Node* nn

        = new Node;

    nn->data = data;

    nn->left = NULL;

    nn->right = NULL;

    return nn;

}

// Function to insert data in BST

struct Node* insert(struct Node* root, int data)

{

  if (root == NULL)

        return newNode(data);

    else {

        if (data < root->data)

            root->left = insert(root->left, data);

        if (data > root->data)

            root->right = insert(root->right, data);

        return root;

    }

}

// InOrder function to display value of array

// in sorted order

void inOrder(struct Node* root)

{

    if (root == NULL)

        return;

    else {

        inOrder(root->left);

        cout << root->data << " ";

        inOrder(root->right);

    }

}

// Driver code

int main()

{

    int arr[] = { 199, 666, 930, 751, 120, 411, 534, 716, 134, 379, 908, 97, 740, 904, 575, 437, 983, 835, 931, 768, 405, 544, 553, 821, 160, 312, 228, 324, 675, 622, 768, 312, 310, 18, 558, 170, 842, 214, 545, 857, 6, 71, 421, 909, 908, 377, 488, 837, 884, 382, 1, 654, 207, 751, 459, 450, 431, 462, 40, 926, 304, 594, 966, 386, 760, 39, 297, 557, 125, 327, 278, 328, 937, 195, 346, 419, 953, 681, 203, 173, 807, 710, 428, 415, 601, 258, 981, 464, 568, 546, 308, 883, 431, 948, 529, 55, 651, 73, 411, 719, 172, 43, 249, 695, 636, 603, 447, 652, 211, 736, 923, 27, 198, 427, 154, 572, 671, 314, 25, 428, 900, 930, 378, 806, 281, 805, 627, 748, 23, 961, 244, 587, 352, 939, 880, 69, 52, 702, 856, 990, 19, 743, 132, 793, 279, 500, 776, 921, 583, 487, 638, 753, 209, 870, 506, 995, 691, 743, 501, 831, 549, 369, 718, 780, 17, 636, 217, 486, 548, 500, 381, 116, 84, 683, 908, 151, 633, 404, 4, 426, 92, 614, 851, 714, 90, 789, 441, 985, 539, 470, 891, 384, 962, 259, 938, 211, 683, 560, 630, 893, 106, 841, 974, 114, 239, 896, 796, 524, 896, 974, 583, 729, 577, 863, 696, 530, 670, 467, 841, 401, 209, 596, 862, 702, 21, 219, 902, 271, 293, 350, 357, 182, 179, 909, 874, 769, 192, 39, 7, 487, 571, 155, 565, 231, 36, 544, 443, 24, 596, 570, 129, 585, 529, 557, 833, 270, 241, 732, 569, 839, 762, 881, 177, 149, 413, 806, 545, 591, 721, 289, 453, 53, 470, 215, 480, 218, 787, 915, 532, 626, 18, 349, 216, 338, 572, 979, 939, 76, 55, 49, 727, 524, 68, 403, 941, 234, 85, 991, 698, 561 };

    // Finding size of array arr[]

    int n = sizeof(arr) / sizeof(arr[0]);

    struct Node* root = NULL;

    for (int i = 0; i < n; i++) {

        // Insert element of arr[] in BST

        root = insert(root, arr[i]);

    }

    // Inorder Traversal to print nodes of Tree

    inOrder(root);

    return 0;

}

Solutions

Expert Solution

#~ Here we can use class to represent Node
class Node: 
    def __init__(self,key): # constructor for new node
        self.left = None
        self.right = None
        self.data = key

def insert(root,data):
        if root == None:
                return Node(data)
        else:           
                if (data < root.data):
                        root.left = insert(root.left, data)
                if (data > root.data):
                        root.right = insert(root.right, data)
                return root


def inOrder(root):
        if root == None:
                return;
        else:           
                # First recur on left child
                inOrder(root.left)
        
        # then print the data of node, end=' ' means print space after data
        #~ otherwise each line will be printed on new line
                print(root.data, end=' ')
                
        # now recur on right child
                inOrder(root.right)

# Driver code
def main():
        
        arr = [199, 666, 930, 751, 120, 411, 534, 716, 134, 379, 908, 97, 740, 904, 575, 437, 983, 835, 931, 768, 405, 544, 553, 821, 160, 312, 228, 324, 675, 622, 768, 312, 310, 18, 558, 170, 842, 214, 545, 857, 6, 71, 421, 909, 908, 377, 488, 837, 884, 382, 1, 654, 207, 751, 459, 450, 431, 462, 40, 926, 304, 594, 966, 386, 760, 39, 297, 557, 125, 327, 278, 328, 937, 195, 346, 419, 953, 681, 203, 173, 807, 710, 428, 415, 601, 258, 981, 464, 568, 546, 308, 883, 431, 948, 529, 55, 651, 73, 411, 719, 172, 43, 249, 695, 636, 603, 447, 652, 211, 736, 923, 27, 198, 427, 154, 572, 671, 314, 25, 428, 900, 930, 378, 806, 281, 805, 627, 748, 23, 961, 244, 587, 352, 939, 880, 69, 52, 702, 856, 990, 19, 743, 132, 793, 279, 500, 776, 921, 583, 487, 638, 753, 209, 870, 506, 995, 691, 743, 501, 831, 549, 369, 718, 780, 17, 636, 217, 486, 548, 500, 381, 116, 84, 683, 908, 151, 633, 404, 4, 426, 92, 614, 851, 714, 90, 789, 441, 985, 539, 470, 891, 384, 962, 259, 938, 211, 683, 560, 630, 893, 106, 841, 974, 114, 239, 896, 796, 524, 896, 974, 583, 729, 577, 863, 696, 530, 670, 467, 841, 401, 209, 596, 862, 702, 21, 219, 902, 271, 293, 350, 357, 182, 179, 909, 874, 769, 192, 39, 7, 487, 571, 155, 565, 231, 36, 544, 443, 24, 596, 570, 129, 585, 529, 557, 833, 270, 241, 732, 569, 839, 762, 881, 177, 149, 413, 806, 545, 591, 721, 289, 453, 53, 470, 215, 480, 218, 787, 915, 532, 626, 18, 349, 216, 338, 572, 979, 939, 76, 55, 49, 727, 524, 68, 403, 941, 234, 85, 991, 698, 561 ]
        
        # get size of array arr
        n = len(arr)

        root = None     # initially root will be None
        
        #~ insert each number from array to the tree
        for n in arr:
                root = insert(root, n)
        
        inOrder(root)   # display tree inorder
        return 0

main()

Noe: Key differences in c++ and python code in this example

C++                       python

NULL                     None

Struct                    class

Cout                      print

root->data          root.data

Output


Related Solutions

Please show it with python class Node {     int data;     Node left, right;    ...
Please show it with python class Node {     int data;     Node left, right;     public Node(int item)     {         data = item;         left = right = null;     } } public class BinaryTree { // Root of the tree implemented in Node class Node root; Node findLowestCommonAncestor(int node1, int node2) {     return findLowestCommonAncestor(root, node1, node2); } // This function returns pointer to LCA of two given // values node1 and node2. This function assumes that...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
C++ language. struct Node {    int data;    Node *next; } Write a function to...
C++ language. struct Node {    int data;    Node *next; } Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of...
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr);...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr); The function will remove the node after the node pointed by prev_ptr. c++
Please based on my python code. do the following steps: thank you Develop a method build_bst...
Please based on my python code. do the following steps: thank you Develop a method build_bst which takes a list of data (example: [6, 2, 4, 22, 34, 9, 6, 67, 42, 55, 70, 120, 99, 200]) and builds a binary search tree full of Node objects appropriately arranged. Test your build_bst with different sets of inputs similar to the example provided above. Show 3 different examples. Choose 1 example data.  Develop a function to remove a node from a bst...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data;...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; int main() { link ptr,head; int num,i; head = ( link ) malloc(sizeof(node)); ptr = head; printf("enter 10 data \n"); for ( i = 0; i <= 9; i++ ) { scanf("%d",&num); ptr->data = num; ptr->next = ( link ) malloc(sizeof(node)); if ( i == 9 ) ptr->next = NULL; else ptr =...
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT