Advertisement
lukicdarkoo

Malo sa ovim amaterima sa softverskog :)

May 30th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. __author__ = 'LukicDarkoo'
  2.  
  3.  
  4. class Node(object):
  5.     def __init__(self, data):
  6.         self.left = None
  7.         self.right = None
  8.         self.data = data
  9.         self.parent = None
  10.  
  11.         self.height = 0
  12.  
  13.  
  14. class Tree(object):
  15.     def __init__(self):
  16.         self.head = None
  17.  
  18.  
  19.     def find(self, data):
  20.         current = self.head
  21.  
  22.         while current is not None:
  23.             if data < current.data:
  24.                 current = current.left
  25.             elif data > current.data:
  26.                 current = current.right
  27.             else:
  28.                 return current
  29.  
  30.     def insert(self, data):
  31.         if self.head is None:
  32.             self.head = Node(data)
  33.             return
  34.  
  35.         previous = self.head
  36.         current = self.head
  37.  
  38.         while current is not None:
  39.             previous = current
  40.  
  41.             if data < current.data:
  42.                 current = current.left
  43.             else:
  44.                 current = current.right
  45.  
  46.         current = Node(data)
  47.         current.parent = previous
  48.  
  49.         if (current.data < previous.data):
  50.             previous.left = current
  51.         else:
  52.             previous.right = current
  53.  
  54.  
  55.     """
  56.      x                  y      
  57.     / \               / \
  58.    A   y     ---->    x   C
  59.       / \           / \
  60.      B   C          A   B
  61.    """
  62.  
  63.     def leftRotate(self, x):
  64.         y = x.right
  65.         x.right = y.left
  66.         if y.left is not None:
  67.             y.left.parent = x
  68.            
  69.         y.parent = x.parent
  70.         if x.parent is None:
  71.             self.head = y
  72.            
  73.         elif x.parent.left == x:
  74.             x.parent.left = y
  75.         else:
  76.             x.parent = y
  77.            
  78.         y.left = x
  79.         x.parent = y
  80.  
  81.  
  82.     def delete(self, data):
  83.         current = self.find(data)
  84.  
  85.         #laksi slucaj
  86.         if current.left is None:
  87.             self._move(current, current.right)
  88.         elif current.right is None:
  89.             self._move(current, current.left)
  90.  
  91.  
  92.         #zeznut slucaj
  93.         #komplikovano, ali njihovo rjesenje
  94.  
  95.  
  96.  
  97.  
  98.     def _move(self, a, b):
  99.         if a.parent is None:
  100.             head = b
  101.             return
  102.  
  103.         elif a.parent.left == a:
  104.             a.parent.left = b
  105.         else:
  106.             a.parent.right = b
  107.  
  108.         if b is not None:
  109.             b.parent = a.parent
  110.  
  111.  
  112.  
  113.     def cprint(self):
  114.         current = self.head
  115.  
  116.         print(current.right.data)
  117.  
  118.  
  119.  
  120. drvo = Tree()
  121. drvo.insert(5)
  122. drvo.insert(6)
  123.  
  124. drvo.cprint()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement