Why is my function returning None (Binary Search Tree)

The function is the min function at line 88 - 91. I'm trying to implement a find minimum value function in my BST. However, it returns None and I can't see why. Logically, its sound if I'm correct. If the

class node():
    def __init__(self, data =None):
        self.data = data
        self.left = None #Left Child
        self.right = None #Right Child
        self.root = None

    def insert(self, data):
        if self.root is None:
            self.root = node(data)
        else:
            self._insert(data, self.root)

    def _insert(self, data, cur_node):
        if data < cur_node.data: #Needs to go left subtree
             if cur_node.left is None: #Check if left child is empty
                 cur_node.left = node(data)
             else:
                 self._insert(data, cur_node.left)

        elif data > cur_node.data:
            if cur_node.right is None:
                cur_node.right = node(data)
            else:
                self._insert(data, cur_node.right)

        else: #Duplicate case
            print ("Value is already present in tree")

    def find(self, data):
        if self.root: #If there is a root
            is_found = self._find(data, self.root)
            if is_found:
                return True
            else:
                return False
        else:
            return None

    def _find(self,data, cur_node):
        if data > cur_node.data and cur_node.right: #Checking if data is > current node data and checking if it has a right child
            return self._find(data, cur_node.right)
        elif data < cur_node.data and cur_node.left:
            return self._find(data, cur_node.left)
        if data == cur_node.data:
            return True

    def preorder(self):
        if self.root:
            self._preorder(self.root)

    def _preorder(self, cur_node):
        if cur_node:
            print(str(cur_node.data))
            self._preorder(cur_node.left)
            self._preorder(cur_node.right)

    def inorder(self):
        if self.root:
            self._inorder(self.root)

    def _inorder(self, cur_node):
        if cur_node:
            self._inorder(cur_node.left)
            print(str(cur_node.data))
... keep reading on reddit ➑

πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/roasting1
πŸ“…︎ Jan 05 2022
🚨︎ report
Print Binary Search Tree in a tree like structure in Haskell

I created a binary search tree and tried to print the binary search tree with this instance

data Tree a = Nil | Node (Tree a) a (Tree a)
instance Show a => Show (Tree a) where
        show t = intercalate "\n"  (map snd (draw t))

draw :: Show a => Tree a -> [(Int,String)]
draw Nil                = [(1,"*")]
draw (Node Nil x Nil)   = [(1,show x)]
draw (Node tl x tr)     = zip (repeat 0) (map shiftl (draw tl)) ++ [(1,show x ++ "-+")] ++ zip (repeat 2) (map shiftr (draw tr)) where
        shiftl (0,x)    =       spaces ++ "  " ++ x 
        shiftl (1,x)    =       spaces ++ "+-" ++ x 
        shiftl (2,x)    =       spaces ++ "| " ++ x 
        shiftr (0,x)    =       spaces ++ "| " ++ x 
        shiftr (1,x)    =       spaces ++ "+-" ++ x 
        shiftr (2,x)    =       spaces ++ "  " ++ x
        spaces          =       replicate  (length (show x) ) ' '

Now I want to print it horizontally, which i am not able to do so. I want to print the binary search tree like this picture below. (Sorry for the low quality of the picture but you get the idea). How can i do it ?

https://preview.redd.it/aabp5f2g64881.jpg?width=343&format=pjpg&auto=webp&s=3eabb06ef3eab3997cbaa7d9f68258b809da3cd2

πŸ‘︎ 7
πŸ’¬︎
πŸ‘€︎ u/Soham-Chatterjee
πŸ“…︎ Dec 27 2021
🚨︎ report
How To Make A Binary Search Tree In ANY Programming Language. youtube.com/watch?v=FaLD1…
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/grumpyCaribou0
πŸ“…︎ Jan 01 2022
🚨︎ report
Question about Binary Search Tree's

Hello! This is the question that I'm failing to understand.

Consider the following BST. How many nodes are in the subtree rooted at node 3 after rotating node 3 left? Include node 3 in this count(The answer to this is 1)

    2 
  /   \
1       3
          \
            4
             \
               5

Can anyone dumb the question down? "at node 3", is that referring to the number three or is "node 3" a specific position of a node? and what does "rotating ___ left" mean? The answer is 1 btw, Thank you!

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/Kronoxpi
πŸ“…︎ Dec 15 2021
🚨︎ report
[Data Structures] Can I implement a binary search tree into my project?

I am making a program that will decide what I will make to eat based on a set of questions. I want to use a data structure like a BST to search for the "optimal" meal based on my answers. Can I use a BST for this? if so how? if not, what can I use instead?

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/467danny
πŸ“…︎ Nov 15 2021
🚨︎ report
Trying to make a Binary search Tree from an Arraylist

I have tried to put a the contents of the text file into an array list the i planned to use the contents of the arraylist to create a binary search tree. My code looks like this:

BSTNode.java

class BSTNode{
    int key;
    BSTNode left, right;
    
    public BSTNode(int item){
        key = item;
        left = right = null;
    }
}

BST.java

import java.util.*;
import java.io.*;
class BST{
    BSTNode root;
    BST(){root = null;}
    
    public BSTNode insertOrder(ArrayList<Integer> arr, BSTNode root , int i ){
        if (i < arr.size()) {
            BSTNode temp = new BSTNode(arr.get(i));
            root = temp;
 
            
            root.left = insertOrder(arr, root.left,
                                             2 * i + 1);
            root.right = insertOrder(arr, root.right,
                                               2 * i + 2);
        }
        return root;
    }
    
    void printPostorder(BSTNode node)
    {
        if (node == null)
            return;
        printPostorder(node.left);
        printPostorder(node.right);
        System.out.print(node.key + " ");
    }
    
    void printInorder(BSTNode node)
    {
        if (node == null)
            return;

        printInorder(node.left);
        System.out.print(node.key + " ");
        printInorder(node.right);
    }
    void printPreorder(BSTNode node)
    {
        if (node == null)
            return;
 
        System.out.print(node.key + " ");
        printPreorder(node.left);
        printPreorder(node.right);
    }
    void printPostorder() { printPostorder(root); }
    void printInorder() { printInorder(root); }
    void printPreorder() { printPreorder(root); }
}

BSTMain.java

import java.util.*;
import java.io.*;

class BSTMain {
    
    public static void main(String args [])throws FileNotFoundException{
        Scanner scan  = new Scanner("bst.txt");
        BST tree = new BST();
        int c = 0;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        
        while(scan.hasNext()){
           arr.add(scan.nextInt());
            
        }
        tree.root = tree.insertOrd
... keep reading on reddit ➑

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/saltmine123
πŸ“…︎ Oct 29 2021
🚨︎ report
MICROSOFT Coding Interview Question - Search in a Binary Search Tree | LeetCode youtube.com/watch?v=VWPdG…
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/cmcoffee91
πŸ“…︎ Dec 03 2021
🚨︎ report
what to pass in main method of binary search tree

I know i have to pass and int in the main method but I am unsure which one. or should i redo my main method?

import java.util.*;
import java.io.*; 

public class BinarySearchTree {
    
        private int root;
        private int left[];
        private int data[];
        private int right[];
        private int currentSize;     
        
        public BinarySearchTree(int size) {
           root = -1;
           left = new int[size];
           data = new int[size];
           right = new int[size];
           currentSize = 0;
 }
      
    // constructor for initialise the root to null BYDEFAULT
    
    public boolean full() {
        return currentSize == data.length;
    }
    private int createNode(int value){
        data[currentSize]=value;
        left[currentSize]=-1;
        right[currentSize]=-1;
        return currentSize++;
    }
    // insert method to insert the new Date
    public void insert(int d) {
        if(root==-1){
            root=createNode(d);
        }
        else{
            insert(d, 0);
        }
    }

    private void insert( int d, int index) {
        if (data[index] == d) {
            return;
        }
        if (data[index] > d) {
            if (left[index] == -1) {
                left[index] = createNode(d);
            } else {
                insert(d, left[index]);
            }
        } else {
            if (right[index] == -1) {
                right[index] = createNode(d);
            } else {
                insert(d, right[index]);
            }
        }
        return;
    }

    
    public void print(){
     print(root);
        System.out.println();
    }
    private void print(int index){
        if(index!=-1){
            print(left[index]);
            System.out.print(data[index] + " ");
            print(right[index]);
        
        }
    }


    public static void main(String[] args) {
        // Creating the object of BinarySearchTree class
        BinarySearchTree bst = new BinarySearchTree();
        // call the method insert
        bst.insert(8);
        bst.insert(5);
        bst.insert(9);
        bst.insert(3);
        bst.insert(7);  
... keep reading on reddit ➑

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/fungirlem12
πŸ“…︎ Nov 16 2021
🚨︎ report
Odd Number and Binary Search Tree - Laurent Rosenfeld blogs.perl.org/users/laur…
πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/liztormato
πŸ“…︎ Sep 18 2021
🚨︎ report
What is the difference between NULL and nullptr when using them for something like a Binary Search Tree? Are those interchangeable?

As far as I know, NULL is just another name for 0 (edit 3: I used the NULL from <iostream>)for which nullptr is a pointer pointing 0. Some references I have used NULL, but others have nullptr. They are writing their trees in different ways so I am not sure if they are interchangeable, but I saw someone on an overflow post saying that versions of C++ do consider NULL == nullptr. But my past experiences are saying that int *some operator here* pointer is a mess. Can anyone clear this up for me? Thank you!

edit:sample for NULL:TreeNode *leftPtr = NULL;TreeNode *rightPtr = NULL;

sample for nullptr:TreeNode *leftPtr = nullptr;TreeNode *rightPtr = nullptr;// idk if these work, or if they only work if:// for example rootPtr-&gt;rightPtr = nullptr

// edit 2: running this, it prints "1" so it must be true,

#include &lt;iostream&gt;

using namespace std;

int main() {
    cout &lt;&lt; (NULL == nullptr) &lt;&lt; endl;
    return 0;
}
πŸ‘︎ 7
πŸ’¬︎
πŸ‘€︎ u/RecursiveInquirer
πŸ“…︎ Jun 29 2021
🚨︎ report
Can nodes in balanced binary search trees contain parent node field?

The background of this question comes from me the fact that in Skiena's book "The Algorithm Design Manual: Second Edition" page 85:

https://preview.redd.it/sc6mewa9lz681.png?width=961&format=png&auto=webp&s=893bdc9adb5bd9b79ce1f17cf2fdcb0c63741169

He claims that the delete-minimum operation for the priority queue when implemented with a balanced search tree will have O(log n) time complexity. But, I disagree with that statement IF the nodes in a balanced search tree have a parent node field. Because since we have a pointer to the minimum it takes constant time pointer manipulation to unlink the minimum node and since the new minimum node will be the parent node, setting the pointer to the new minimum also takes constant time. Unless my assumptions on how a balanced search tree is misguided (I assume a balanced search tree is functionally equivalent to a binary tree, except its self-balancing process), then the delete-minimum operation should have O(1) time complexity. Where have I gone wrong?

πŸ‘︎ 26
πŸ’¬︎
πŸ‘€︎ u/gr3atm4n
πŸ“…︎ Dec 22 2021
🚨︎ report
Binary search tree
#STEP 1
class Node():
  def __init__(self,data):
    self.data = data
    self.leftChild = None
    self.rightChild = None

class BinarySearchTree():
  def __init__ (self):
    self.root = None
  
  def insert(self, data):
      if not self.root:
        self.root = Node(data)
      else:
        self.insertNode(data, self.root)

  def insertNode(self, data, node):
    if data &lt; node.data:
      if node.leftChild:
       self.insertNode(data, node.leftChild)
      else:
        node.leftChild = Node(data)
    else:
      if node.rightChild:
        self.insertNode(data, node.rightChild)
      else:
        node.rightChild = Node(data)

    #STEP2
  def getMinValue(self):
    if self.root:
      return self.getMin(self.root)
  def getMin(self, node):
    if node.leftChild:
      return self.getMin(node.leftChild)
    return node.data

  def getMaxValue(self):
    if self.root:
      return self.getMax(self.root)
  def getMax(self, node):
    if node.rightChild:
      return self.getMax(node.rightChild)
    return node.data



#STEPΒ 5#AffordableΒ productsdef 
affordable_products(self):
    if self.rootΒ is not None:
        self.traverse_in_order(self.root)

def traverse_in_order(self,Β node):
  ifΒ node.leftChild:
    self.traverse_in_order(node.leftChild)
  ifΒ node.rightChild:
    self.traverse_in_order(node.rightChild)
  ifΒ node.data[0]Β &lt;=Β money:
    print(node.data)

I am trying to run this code to get all the products someone can buy depending on the amount of money they have based on a tree diagram, but every time I run it I get this error < 'BinarySearchTree' object has no attribute 'traverse_in_order' >.

Can anyone help me please, if the tree code is also needed please ask me.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/juliofulli
πŸ“…︎ Nov 01 2021
🚨︎ report
Is it normal to build a Binary Search Tree with a separate function like this?

I'm making a BST and the guide I followed has a seperate function to build the tree instead of including it in the BST class. Is that normal? Should I change this and include it inside the BST class instead of a seperate function? Or is this how its done? Doesn't look right to me. I marked what im talking about in the code. The code is:

class BinarySearchTreeNode:
      def __init__(self, data):
          self.data = data
          self.left_child = None
          self.right_child = None
             
      def add_child(self, data):
        if data == self.data:
          return "Node already exists"
        
        if data &lt; self.data:
          if self.left_child:
            self.left_child.add_child(data)
          else:
            self.left_child = BinarySearchTreeNode(data)
        
        if data &gt; self.data:
          if self.right_child:
            self.right_child.add_child(data)
          else:
            self.right_child = BinarySearchTreeNode(data)
            
      def search(self, val):
        if self.data == val:
          return True
        
        if val &gt; self.data:
          if self.right_child:
            return self.right_child.search(val)
          else:
            return False
          
        if val &lt; self.data:
          if self.left_child:
            return self.left_child.search(val)
          else:
            return False
        
      def in_order_traversal(self):
        elements = []
        
        if self.left_child:
          elements += self.left_child.in_order_traversal()
        
        elements.append(self.data)
        
        if self.right_child:
          elements += self.right_child.in_order_traversal()
          
        return elements
        
        
 def build_tree(elements): &lt;---------- RIGHT HERE---------------
        print("Building tree with these elements", elements)
        root = BinarySearchTreeNode(elements[0])
        for i in range(1, len(elements)):
          root.add_child(elements[i])
        return root
    
 if __name__ == '__main__':
        numbers = [17, 4, 1, 20, 9, 23, 18, 34]
        numbers2 = [15,12,7,14,27,20,23,88 ]
    
        numbers_tree = build_tree(numbers)
        print("Input 
... keep reading on reddit ➑

πŸ‘︎ 7
πŸ’¬︎
πŸ“…︎ Oct 17 2021
🚨︎ report
Creating a Binary Search Tree with matrixes

I have to create a BST with matrixes, preferably a random BST.

I was thinking of adapting this BST program I did some time ago, but I can't think of a way to use matrixes beside changing the value variable to an int matrix. Can anyone help me out?

Here's the code:

BST class:

public class BSTree {

    //inserting a node section
    public Node insert(Node node, int value){
        if(node == null){
            return createNewNode(value);
        }
        if(value &lt; node.value){
            node.left = insert(node.left, value);
        }
        else if(value &gt; node.value){
            node.right = insert(node.right, value);
        }
        return node;
    }

    private Node createNewNode(int value) {
        Node a = new Node(value);
        a.left = null;
        a.right = null;
        return a;
    }

    //deleting a node section
    public Node delete(Node node, int value){
        if(node == null){
            return null;
        }
        else if(value == node.value){
            if(node.left == null &amp;&amp; node.right == null){
                return null;
            }
            else if(node.right == null){
                return node.left;
            }
            else if(node.left == null){
                return node.right;
            }
            else if(node.left != null &amp;&amp; node.right != null){
                int smallestValue = findSmallest(node.right);
                node.value = smallestValue;
                node.right = delete(node.right, smallestValue);
                return node;
            }
        }
        else if(value &lt; node.value){
            node.left = delete(node.left, value);
        }
        else if(value &gt; node.value){
            node.right = delete(node.right, value);
        }
        node.right = delete(node.right, value);
        return node;
    }

    private int findSmallest(Node root){
        return root.left == null ? root.value : findSmallest(root.left);
    }

    //searching for a node section
    private boolean containsNode(Node node, int value){
        if(node == null){
            return false;
        }
        if(value == node.value){
            return true;
        }
        return value &
... keep reading on reddit ➑

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/Ar5ene
πŸ“…︎ Nov 07 2021
🚨︎ report

Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.