TREE
Ordered List yaitu metode penyimpanan data atau mengumpulkan data lalu diurutkan secara ascending atau descending.
Ordered list menggunakan class yang bernama node, yang terdiri dari data sekarang dan data selanjutnya.
Awal
data pada Ordered list disebut Head sedangkan data akhir disebut
tail. Data yang baru diambil dan diinputkan ditaruh disebelah kanan
head.
Berikut ini program python dari Tree Search :
class BinaryTree:
def __init__(self,rootObj):
self.key = rootObj
self.leftChild = None
self.rightChild = None
def insertLeft(self,newNode):
if self.leftChild == None:
self.leftChild = BinaryTree(newNode)
else:
t = BinaryTree(newNode)
t.leftChild = self.leftChild
self.leftChild = t
def insertRight(self,newNode):
if self.rightChild == None:
self.rightChild = BinaryTree(newNode)
else:
t = BinaryTree(newNode)
t.rightChild = self.rightChild
self.rightChild = t
def getRightChild(self):
return self.rightChild
def getLeftChild(self):
return self.leftChild
def setRootVal(self,obj):
self.key = obj
def getRootVal(self):
return self.key
def show(self):
current = self.key
print (current)
selfleft = self
selfright = self
while selfleft.getLeftChild() != None and selfright.getRightChild() != None :
print(selfleft.getLeftChild().getRootVal() )
print(selfright.getRightChild().getRootVal() )
selfleft = selfleft.getLeftChild()
while selfright.getRightChild() != None :
print(selfright.getRightChild().getRootVal() )
selfright = selfright.getRightChild()
def height(self):
selfleft = self
selfright = self
count = 0
while selfleft.getLeftChild() != None or selfright.getRightChild() != None :
count = count + 1
if selfleft.getLeftChild() != None:
selfleft = selfleft.getLeftChild()
if selfright.getRightChild() != None:
selfright = selfright.getRightChild()
return count
r = BinaryTree('a')
print(r.getRootVal())
r.insertLeft('b')
r.insertRight('c')
print(r.getLeftChild().getRootVal())
print(r.getRightChild().getRootVal())
print(r.height())
Tidak ada komentar:
Posting Komentar