本文整理汇总了Python中Node.Node.insert方法的典型用法代码示例。如果您正苦于以下问题:Python Node.insert方法的具体用法?Python Node.insert怎么用?Python Node.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Node.Node
的用法示例。
在下文中一共展示了Node.insert方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BinarySearchTree
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import insert [as 别名]
class BinarySearchTree(object):
def __init__(self):
self.rootNode = None
def insert(self, data):
if not self.rootNode:
# first element
self.rootNode = Node(data)
else:
self.rootNode.insert(data)
def remove(self, dataToRemove):
if self.rootNode:
if self.rootNode == dataToRemove:
tempNode = Node(None)
tempNode.leftChild = self.rootNode
self.rootNode.remove(dataToRemove, tempNode)
else:
self.rootNode.remove(dataToRemove, None)
def getMax(self):
if self.rootNode:
return self.rootNode.getMax()
def getMin(self):
if self.rootNode:
return self.rootNode.minValue()
def traverseInOrder(self):
if self.rootNode:
self.rootNode.traverseInOrder()
示例2: BinarySearchTree
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import insert [as 别名]
class BinarySearchTree(object):
def __init__(self):
self.root_node = None
def insert(self, data):
if self.root_node is None:
self.root_node = Node(data)
else:
self.root_node.insert(data)
def remove(self, data):
if self.root_node:
if self.root_node.data == data:
temp_node = Node(None)
temp_node.left_child = self.root_node
self.root_node.remove(data, temp_node)
self.root_node = temp_node.left_child
else:
self.root_node.remove(data, None)
def getMax(self):
if self.root_node:
return self.root_node.getMax()
def getMin(self):
if self.root_node:
return self.root_node.getMin()
def inOrderTraversal(self):
if self.root_node:
return self.root_node.inOrderTraversal()
def inOrderTraversalWithStack(self):
stack = []
if self.root_node:
current = self.root_node
done = False
while not done:
if current is not None:
stack.append(current)
current = current.left_child
else:
if len(stack) != 0:
temp = stack.pop()
print temp.data
current = temp.right_child
else:
done = True
示例3: BinarySearch
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import insert [as 别名]
class BinarySearch(object):
def __init__(self):
self.rootNode = None;
def insert(self, data):
if not self.rootNode:
self.rootNode = Node(data)
else:
self.rootNode.insert(data)
def remove(self, dataToRemove):
if self.rootNode:
if self.rootNode.data == dataToRemove:
tempNode = Node(Noneo)
tempNode.leftChild = self.rootNode
self.rootNode.remove(dataToRemove, tempNode)
self.rootNode = tempNode.leftChild
else:
self.rootNode.remove(dataToRemove, None)
def getMax(self):
maxNode = self.rootNode
while maxNode.rightChild:
maxNode = maxNode.rightchild
return maxNode.data
def getMin(self):
minNode = self.rootNode
while minNode.leftChild:
minNode =minNode.leftChild
return minNode.data
def traverseInOrder(self):
if self.rootNode:
self.rootNode.traverseInOrder()
示例4: BinaryTree
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import insert [as 别名]
class BinaryTree(object):
def __init__(self, *args):
self.root = None
if len(args) is 0:
#===================================================================
# Default, empty, constructor.
# >>> tree = BinaryTree()
#===================================================================
pass
elif isinstance(args[0], Node):
#===================================================================
# Use the given node as the root of this tree.
#===================================================================
self.root = args[0]
elif '__iter__' in dir(args[0]):
#===================================================================
# Construct the binary tree using the given iterable.
# >>> evens = BinaryTree(number for number in range(101) if number % 2 is 0)
#===================================================================
for element in args[0]:
self.insert(element)
else:
#===================================================================
# Construct the binary tree using all given elements.
# >>> random = BinaryTree(56,7,2,5,8,23)
#===================================================================
for element in args:
self.insert(element)
def __contains__(self, element):
return element in self.root
def __str__(self):
return str(self.inOrder())
def isEmpty(self):
return self.root is None
def insert(self, element):
if self.isEmpty():
self.root = Node(element)
else:
self.root.insert(element)
return self
def inOrder(self):
return tuple(item for item in self.root.inOrder())
def preOrder(self):
return tuple(item for item in self.root.preOrder())
def postOrder(self):
return tuple(item for item in self.root.postOrder())
@NotEmpty
def decendantsOf(self, element):
return self.root.descendants(element)
@NotEmpty
def ancestorsOf(self, element):
return tuple(ancestor for ancestor in self.root.ancestors(element))
@NotEmpty
def isAncestorOf(self, targetAncestor, targetDescendant):
return self.root.isAncestorOf(targetAncestor, targetDescendant)
@NotEmpty
def isDescendantOf(self, targetDescendant, targetAncestor):
return self.root.isAncestorOf(targetAncestor, targetDescendant)
@NotEmpty
def min(self):
return self.root.min()
@NotEmpty
def max(self):
return self.root.max()
@NotEmpty
def root(self):
return self.root.element
@NotEmpty
def detachAt(self, element):
return BinaryTree(self.root.detachAt(element))
@NotEmpty
def levelOf(self, element):
return self.root.levelOf(element)
@NotEmpty
def height(self):
return max(self.root.height())
def attach(self, tree):
if not isinstance(tree, BinaryTree):
raise TypeError('Expected a Node. Received a {CLASS}'.format(CLASS=tree.__class__))
if self.root is None:
self.root = tree
#.........这里部分代码省略.........