Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am reading about algorithms and got to the binary search trees. I wanted to ask if the following implementation of a Binary Tree is correct.

import random

class Tree(object):

    def __init__(self):

        self.root = Node()

    def insert(self,data):

        self.root.insert(data)

    def delete(self,data):

        self.root.delete(data)

    def print_elements(self):

        return self.root.print_element()

class Node(object):

    def __init__(self,data = None, parent = None):

        self.parent = parent
        self.data = data
        self.left = None
        self.right = None

    def insert(self,data):

        if self.data == None:

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

    def delete(self,data):

        if self.data == data:

            if self.right == None and self.left == None:        

                if self.parent.left.data == data:

                    self.parent.left = None
                else:

                    self.parent.right = None
            elif self.right == None or self.left == None:

                if self.right == None:

                    self.data , self.left = self.left.data , self.left.left

                else:

                    self.data, self.right = self.right.data , self.right.right

            else:

                counter = self.right
                while True:
                    if counter.right == None:
                        break
                    else:
                        counter = counter.left
                self.data = counter.data
                counter.delete(counter.data)

        else:
            if data < self.data:
                self.left.delete(data)
            else:
                self.right.delete(data)


    def search(self,data):

        if self.data == data:
            return self.data
        else:
            if data < self.data:
                return self.left.search(data)
            else:
                return self.right.search(data)

    def print_element(self):

        if self.parent:

            print self.data, 'is below %s' % (self.parent.data)
        else:
            print self.data

        if self.right != None:
            self.right.print_element()
        if self.left != None:
            self.left.print_element()

The other thing I want to ask is, when I am implementing these data structures (also happened with linked lists) I find it easier to implement the methods in the Node class and not in the BT/LL class. Is this normal with Python ? I mean, I see the guides with C++ and the methods are mostly implemented in the BT/LL classes , but they use pointers and pointers are not available in Python. Am I doing it wrong ?

share|improve this question
    
Is it on purpose that your implementation can not hold more than one identical value? I mean, t = Tree(); t.insert(1); t.insert(1) result in t containing only one node. Why? – Mathias Ettinger Dec 3 '16 at 11:43
    
Yep, I am not sure how I should handle the same value more than once and this is why I did not include it in the implementation – SuburbanFilth Dec 3 '16 at 12:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.