Tuesday, May 3, 2016

Python implementation of a Binary Search Tree

Here is the python implementation of a binary search tree. Here the binary tree is printed out using preorder.
#! \usr\bin\env python
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class binaryTree:
def __init__(self):
self.root = None
def insertNode(self, data, node):
if self.root == None:
print "Adding first node %d" %data
self.root = Node(data)
else:
if (node.data == data):
print "Node already exists"
elif (data < node.data) and (node.left is not None):
print "visiting left node %d" %node.left.data
self.insertNode(data,node.left)
elif (data > node.data) and (node.right is not None):
print "visiting right node %d" %node.right.data
self.insertNode(data,node.right)
elif (data < node.data) and (node.left is None):
print "adding the %d node to the left of %d" % (data , node.data)
temp = Node(data)
node.left = temp
elif (data > node.data) and (node.right is None):
print "adding the %d node to the right of %d" % (data , node.data)
temp = Node(data)
node.right = temp
def preOrder(self, node):
if node == None:
return
else:
self.preOrder(node.left)
print "Node %d" %node.data
self.preOrder(node.right)
b = binaryTree()
print "---------------------"
b.insertNode(10, b.root)
print "---------------------"
b.insertNode(22, b.root)
print "---------------------"
b.insertNode(15, b.root)
print "---------------------"
b.insertNode(24, b.root)
print "---------------------"
b.insertNode(34, b.root)
print "---------------------"
b.insertNode(47, b.root)
print "---------------------"
b.insertNode(44, b.root)
print "---------------------"
b.insertNode(34, b.root)
print "---------------------"
b.insertNode(1, b.root)
print "---------------------"
b.insertNode(2, b.root)
print "---------------------"
b.insertNode(4, b.root)
print "---------------------"
b.insertNode(3, b.root)
print "---------------------"
b.insertNode(84, b.root)
print "---------------------"
b.insertNode(44, b.root)
print "---------------------"
b.preOrder(b.root)
view raw btpython.py hosted with ❤ by GitHub

Monday, April 25, 2016

Singly Linked List Implementation in python

Below is a very simplistic implementation of a singly linked list. The linked list implementation is done using two classes Node and LinkedList. Node is a class representing each node of the linked list and LinkedList is a class representing the whole linked list itself.
# !/usr/bin/python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def AddNode(self, data):
temp = Node(data)
if self.head == None:
self.head = temp
self.tail = temp
else:
self.tail.next = temp
self.tail = temp
def RemoveNode(self, index):
temp = self.head
if temp.data == index:
temp = self.head = temp.next
while ( temp.next != None):
print temp.data
if temp.next.data == index:
node = temp.next
temp.next = temp.next.next
del node
else:
temp = temp.next
def PrintList(self):
temp = self.head
while ( temp.next != None):
print temp.data,
print "->",
temp = temp.next
if(temp.next == None):
print temp.data
llist = LinkedList()
llist.AddNode(1)
llist.AddNode(2)
llist.AddNode(3)
llist.AddNode(4)
llist.AddNode(5)
llist.AddNode(6)
llist.PrintList()
llist.RemoveNode(6)
llist.PrintList()
view raw llist.py hosted with ❤ by GitHub