μ μμν
μ μμνλ λ Έλλ₯Ό μκΈ° μμλ Έλλ³΄λ€ μμμ λ°©λ¬Ένλ λ°©λ²μ΄λ€.
μ΄λ μ¬κ·μ μΌλ‘ ꡬνν μ μλ€.
νμμν
μ μμνλ λ Έλλ₯Ό μκΈ° μμλ Έλλ³΄λ€ λμ€μ λ°©λ¬Ένλ λ°©λ²μ΄λ€.
μ΄λν μ¬κ·μ μΌλ‘ ꡬνν μ μλ€.
λκ°μ§ λ°©λ²μ ꡬννλλ° μ°¨μ΄μ μ λ Έλλ₯Ό λ°©λ¬Ένλ νμ΄λ°μ μλ€. κ°λ¨νκ² μμλ§ λ°κΏμ£Όλ©΄ ꡬνν μ μλ€.
μλλ μ 체μ μΈ νΈλ¦¬ ADTμ λ©μλμ μ μμν, νμμνλ₯Ό μΆκ°ν΄λμ κ²μ΄λ€.
class Node:
def __init__(self,data,L=None,R=None,parent=None,num=None):
self.data = data
self.L = L
self.R = R
self.parent = parent
self.num = num
class tree:
def __init__(self):
self.root = None
self.n=0
def root(self):
return self.root
def element(self,v):
return v.data
def isRoot(self,v):
return v==self.root
def parent(self,v):
return v.parant
def leftchild(self,v):
return v.L
def rightchild(self,v):
return v.R
def sibling(self,v):
p = v.parent
if (p.L == v):
return p.R
else:
return p.L
def isInternal(self,v):
return (v.L != None) and (v.R != None)
def isExternal(self,v):
return (v.L == None) and (v.R == None)
def setElement(self,v,e):
v.data = e
return e
def swapElements(self,v,w):
tmp = v.data
v.data = w.data
w.data = tmp
print(f"v.data : {tmp} => {v.data} / w.data : {v.data} => {w.data} ")
def preorder(self,v): # μ μμν
print(v.data)
if self.leftchild(v) != None:
self.preorder(self.leftchild(v))
if self.rightchild(v) != None:
self.preorder(self.rightchild(v))
def postorder(self,v): # νμ μν
if self.leftchild(v) != None:
self.postorder(self.leftchild(v))
if self.rightchild(v) != None:
self.postorder(self.rightchild(v))
print(v.data)
def addNode(self,v,data,location,num):
if location =='L':
v.L = Node(data,None,None,v,num)
self.n+=1
elif location == 'root':
self.root = Node('Root',None,None,None,num)
self.n+=1
else:
v.R = Node(data,None,None,v,num)
self.n+=1
728x90