Skip to content

Instantly share code, notes, and snippets.

@MarshallOfSound
Created May 18, 2016 13:40
Show Gist options
  • Save MarshallOfSound/2c93ea55be4688422c7c9632e70ebbeb to your computer and use it in GitHub Desktop.
Save MarshallOfSound/2c93ea55be4688422c7c9632e70ebbeb to your computer and use it in GitHub Desktop.
class Node:
"""
Tree node: left and right child + data which can be any object
"""
def __init__(self, data=None):
"""
Node constructor
@param data node data object
"""
self.left = None
self.right = None
self.data = data
def insert(self, data):
"""
Insert new node with data
@param data: Node data object to insert
"""
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
else:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment