Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'LukicDarkoo'
- class Node(object):
- def __init__(self, data):
- self.left = None
- self.right = None
- self.data = data
- self.parent = None
- self.height = 0
- class Tree(object):
- def __init__(self):
- self.head = None
- def find(self, data):
- current = self.head
- while current is not None:
- if data < current.data:
- current = current.left
- elif data > current.data:
- current = current.right
- else:
- return current
- def insert(self, data):
- if self.head is None:
- self.head = Node(data)
- return
- previous = self.head
- current = self.head
- while current is not None:
- previous = current
- if data < current.data:
- current = current.left
- else:
- current = current.right
- current = Node(data)
- current.parent = previous
- if (current.data < previous.data):
- previous.left = current
- else:
- previous.right = current
- """
- x y
- / \ / \
- A y ----> x C
- / \ / \
- B C A B
- """
- def leftRotate(self, x):
- y = x.right
- x.right = y.left
- if y.left is not None:
- y.left.parent = x
- y.parent = x.parent
- if x.parent is None:
- self.head = y
- elif x.parent.left == x:
- x.parent.left = y
- else:
- x.parent = y
- y.left = x
- x.parent = y
- def delete(self, data):
- current = self.find(data)
- #laksi slucaj
- if current.left is None:
- self._move(current, current.right)
- elif current.right is None:
- self._move(current, current.left)
- #zeznut slucaj
- #komplikovano, ali njihovo rjesenje
- def _move(self, a, b):
- if a.parent is None:
- head = b
- return
- elif a.parent.left == a:
- a.parent.left = b
- else:
- a.parent.right = b
- if b is not None:
- b.parent = a.parent
- def cprint(self):
- current = self.head
- print(current.right.data)
- drvo = Tree()
- drvo.insert(5)
- drvo.insert(6)
- drvo.cprint()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement