简体   繁体   English

在 Python 中将值插入到二叉搜索树中

[英]Inserting a value into a Binary Search Tree in Python

I am reviewing for my final and one of the practice problem asks to implement a function that puts a value into a binary search tree in Python.我正在复习我的期末考试,其中一个练习题要求实现一个函数,该函数将一个值放入 Python 中的二叉搜索树中。 Here is the Tree implementation I am using.这是我正在使用的 Tree 实现。

class Tree(object):
    def __init__(self, entry, left=None, right=None):
        self.entry = entry
        self.left = left
        self.right = right

Here is the function I need to fill in.这是我需要填写的功能。

def insert(item, tree):
    """
    >>> t = Tree(5, Tree(1, None, Tree(4)), Tree(7, Tree(6), Tree(8)))
    >>> insert(2, t)
    >>> t
    Tree(5, Tree(1, None, Tree(4, Tree(2), None)), Tree(7, Tree(6), Tree(8)))
    """

Can anyone help me implement this code, as I have no idea where to start?谁能帮我实现这段代码,因为我不知道从哪里开始? Thanks!谢谢!

def insert(item, tree):
    if (item < tree.entry):
        if (tree.left != None):
            insert(item, tree.left)
        else:
            tree.left = Tree(item)
    else:
        if (tree.right != None):
            insert(item, tree.right)
        else:
            tree.right = Tree(item)

Tree is a Non linear data structure.A tree is created by set of vertices and set of edges.Average searching complexity is logn .树是一种非线性数据结构。树由一组顶点和一组边创建。平均搜索复杂度为 logn 。 Let's consider how to insert values to tree.让我们考虑如何向树插入值。 First of all , you would create a Vertices.In another way, you would create nodes.then , Those nodes which is created insert hierarchical manner.In creating Node class , You can initialize all the properties of Node class in constructor function.Actually like this,首先,你会创建一个顶点。另一种方式,你会创建节点。然后,那些创建的节点插入分层方式。在创建节点类时,你可以在构造函数中初始化节点类的所有属性。实际上就像这,

class Node:
      def __init__(self,data):
            self.data=data
            self.left=None
            self.right=None

At beginning, Node's data=data, left child of Node is None and right child of Node is None.And then , you can create a binary search tree using those created nodes.一开始,Node的data=data,Node的左孩子是None,右孩子是None。然后,你可以使用这些创建的节点创建一个二叉搜索树。

class tree:

     def __init__(self):
           self.root=None

     def insert(self,data):
           if(self.root==None):
                  self.root=Node(data)
           else:
                  self._insert(data,self.root)


     def _insert(self, data, curNode):
           if(curNode.data>data):
                  if(curNode.left==None):
                         curNode.left=Node(data)

                  else:
                         self._insert(data,curNode.left)
           else:
                   if(curNode.right==None):
                         curNode.right=Node(data)
                   else:
                         self._insert(data,curNode.right)

At first, root node is initialized under constructor method of tree class.And then insert nodes using insert function.Using any tree traversal method can be printed elements of tree..I think , you could understand.thank you!首先在树类的constructor方法下初始化根节点,然后使用insert函数插入节点。使用任何树遍历方法都可以打印树的元素..我想,你可以理解。谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM