Binary Search Tree : Insertion

Sort by

recency

|

571 Discussions

|

  • + 0 comments

    Python solution: def insert(self, val): curr = self.root if curr is None: self.root = Node(val) else: while True: if val > curr.info: if curr.right is None: curr.right = Node(val) break else: curr = curr.right else: if curr.left is None: curr.left = Node(val) break else: curr = curr.left return self.root

  • + 0 comments

    My C code 😎😁

    struct node* creatNode(int data){
        struct node* root = (struct node*)malloc(sizeof(struct node));
        root->data = data;
        root->left = NULL;
        root->right = NULL;
        
        return root;
    }
    
    struct node* insert( struct node* root, int data ) {
    		if(root == NULL){
                struct node* tree = creatNode(data);
                return tree;
            }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
    public static Node insert(Node root,int data) {
            root = addRecursive(root, data);
            return root;
        }
        
        public static Node addRecursive(Node current, int data) {
            if(current == null) return new Node(data);
            else {
                if(current.data < data) {
                    current.right = addRecursive(current.right, data);
                }
                if(current.data > data) {
                    current.left = addRecursive(current.left, data);
                }
            }
            return current;
        }
    
  • + 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