Binary Search Tree : Insertion

Sort by

recency

|

568 Discussions

|

  • + 0 comments
    [ def insert(self, val):
            new_node = Node(val)
            if self.root is None:
                self.root = new_node
                return self.root
            curr = self.root
            while True:
                if new_node.info < curr.info:
                    if curr.left is None:
                        curr.left = new_node
                        break
                    else:
                        curr = curr.left
                elif new_node.info > curr.info:
                    if curr.right is None:
                        curr.right = new_node
                        break
                    else:
                        curr = curr.right
            return self.root](https://)
    
  • + 0 comments
    def insert(self, val):
            new_node = Node(val)
            if self.root is None:
                self.root = new_node
                return self.root
            curr = self.root
            while True:
                if new_node.info < curr.info:
                    if curr.left is None:
                        curr.left = new_node
                        break
                    else:
                        curr = curr.left
                elif new_node.info > curr.info:
                    if curr.right is None:
                        curr.right = new_node
                        break
                    else:
                        curr = curr.right
            return self.root
    
  • + 0 comments

    For those wanted to do C#, they don't have the stub code, so I wrote it.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    public class Node
    {
        public int Value;
        public Node Left;
        public Node Right;
        
        public Node(int value)
        {
            Value = value;
        }
        
        public void Insert(int i)
        {
            // Write your insert code here
        }
        
        public List<int> GetTreeOrder(List<int> list = null)    
        {
            // Write your order code here
        }
    }
    
    class Solution {
                
        static void Main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
            var count = Convert.ToInt32(Console.ReadLine());
            var intArray = Console.ReadLine().Split(" ").Select(i => Convert.ToInt32(i)).ToArray();
            var rootNode = new Node(intArray[0]);
            for(int i = 1; i < intArray.Length; i++)
            {
                rootNode.Insert(intArray[i]);
            }
            var inOrder = rootNode.GetTreeOrder();
            Console.WriteLine(string.Join(" ", inOrder));
        }
    }
    
  • + 0 comments

    "c language" struct node* newNode(int data) { struct node* node=(struct node*)malloc(sizeof(struct node)); node->data=data; node->left=NULL; node->right=NULL; return(node); }

    struct node* insert(struct node* root, int data) { if(root==NULL) {
    return(newNode(data)); } else { struct node* cur; if(data<=root->data) { cur=insert(root->left,data); root->left=cur; } else { cur=insert(root->right,data); root->right=cur; } } return root; }


  • + 0 comments

    typedef struct Node { int data; struct Node *left; struct Node *right; } Node;

    Node *createNode(int data) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; }

    Node *insert(Node *root, int data) { if (root == NULL) { return createNode(data); }

    if (data < root->data) {
        root->left = insert(root->left, data);
    } else if (data > root->data) {
        root->right = insert(root->right, data);
    }
    
    return root;
    

    }

    void printInorder(Node *root) { if (root != NULL) { printInorder(root->left); printf("%d ", root->data); printInorder(root->right); } }