MCPcopy Index your code
hub / github.com/subbarayudu-j/TheAlgorithms-Python / insert_node

Function insert_node

data_structures/binary tree/AVLtree.py:122–141  ·  view source on GitHub ↗
(node,data)

Source from the content-addressed store, hash-verified

120
121
122def insert_node(node,data):
123 if node is None:
124 return my_node(data)
125 if data < node.getdata():
126 node.setleft(insert_node(node.getleft(),data))
127 if getheight(node.getleft()) - getheight(node.getright()) == 2: #an unbalance detected
128 if data < node.getleft().getdata(): #new node is the left child of the left child
129 node = leftrotation(node)
130 else:
131 node = rlrotation(node) #new node is the right child of the left child
132 else:
133 node.setright(insert_node(node.getright(),data))
134 if getheight(node.getright()) - getheight(node.getleft()) == 2:
135 if data < node.getright().getdata():
136 node = lrrotation(node)
137 else:
138 node = rightrotation(node)
139 h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1
140 node.setheight(h1)
141 return node
142
143def getRightMost(root):
144 while root.getright() is not None:

Callers 1

insertMethod · 0.85

Calls 13

my_nodeClass · 0.85
getheightFunction · 0.85
leftrotationFunction · 0.85
rlrotationFunction · 0.85
lrrotationFunction · 0.85
rightrotationFunction · 0.85
my_maxFunction · 0.85
getdataMethod · 0.80
setleftMethod · 0.80
getleftMethod · 0.80
getrightMethod · 0.80
setrightMethod · 0.80

Tested by

no test coverage detected