Exemple #1
0
    def _insert2(self, value):
        """ 循环的方式实现

        :param node:
        :param value:
        :return:
        """
        if self.root is None:
            self.root = Node(value)
            return

        cur_node = self.root
        while True:
            if value < cur_node.value:
                if cur_node.left is None:
                    cur_node.left = Node(value, cur_node)
                    return
                else:
                    cur_node = cur_node.left
            elif value > cur_node.value:
                if cur_node.right is None:
                    cur_node.right = Node(value, cur_node)
                    return
                else:
                    cur_node = cur_node.right
    def p_calc_grammar(self, p):
        """calc_grammar : expr SEMI"""

        self.nodeRoot = Node("CONTAINER", None, "_root")
        self.nodeRoot.children.append(p[1])
        node = Node("calc_END", None, ";")
        self.nodeRoot.children.append(node)
    def p_group(self, p):
        """expr : LPAREN expr RPAREN"""

        node = Node("CONTAINER", None, "_expr")
        node.children.append(Node("KEYWORD", None, "("))
        node.children.append(p[2])
        node.children.append(Node("KEYWORD", None, ")"))
        p[0] = node
    def p_term(self, p):
        """term : STRING
                | NUMBER"""

        if p.slice[1].type == 'STRING':
            node = Node("STRING", None, p[1])
        else:
            node = Node("NUMBER", None, p[1])
        p[0] = node
Exemple #5
0
    def _insert1(self, node: Node, value):
        """ 递归的方式实现

        :param node:
        :param value:
        :return:
        """
        if value < node.value:
            if node.left is None:
                node.left = Node(value, node)
                return
            else:
                self._insert1(node.left, value)
        elif value > node.value:
            if node.right is None:
                node.right = Node(value, node)
                return
            else:
                self._insert1(node.right, value)
    def p_operation_expression(self, p):
        """operation_expression : expr PLUS expr
                                | expr MINUS expr
                                | expr MULTI expr
                                | expr DIVIDE expr
                                | expr EQUALS expr
                                """

        node = Node("OPERATION", None, p[2])
        node.children.append(p[1])
        node.children.append(p[3])
        p[0] = node
Exemple #7
0
 def insert(self, value):
     """ 插入数据
     1. 如头节点为空,插入头节点。
     2. 如果比当前节点小,则查找左子节点树,否则查找右子节点树。递归遍历,直到某个子节点为空时,插入数据。
     3. 对于有相同的元素,不处理。
     :param value:
     :param parent:
     :return:
     """
     if not self.root:
         self.root = Node(value)
         return
     return self._insert1(self.root, value)
Exemple #8
0
    def _delete(self, node: Node):
        if node == self.root:
            self.root = None
            return

        # 1. 无子节点
        if node.left is None and node.right is None:
            if node.value < node.parent.value:
                node.parent.left = None
            else:
                node.parent.right = None

        # 2. 只有一个子节点树
        elif node.left is not None and node.right is None:
            if node.value < node.parent.value:
                node.parent.left = node.left
            else:
                node.parent.right = node.left
            node.left.parent = node.parent
            node.parent = None
            node.left = None
        elif node.left is None and node.right is not None:
            if node.value < node.parent.value:
                node.parent.left = node.right
            else:
                node.parent.right = node.right
            node.right.parent = node.parent
            node.parent = None
            node.right = None

        # 3. 左右子节点树都有
        # 找到右节点树最小值
        else:
            min_node = self._get_min(node.right)
            if node.value < node.parent.value:
                node.parent.left = min_node
            else:
                node.parent.right = min_node
            min_node.parent = node.parent
            min_node.left = node.left
            min_node.right = node.right

            node.left = None
            node.right = None
            node.parent = None
Exemple #9
0
import asyncio
from pprint import pprint

from __init__ import Node

loop = asyncio.get_event_loop()

node = Node()

initial_peers = loop.run_until_complete(node.resolve_seeds())
pprint(initial_peers)
    def p_uminus(self, p):
        """term : MINUS term %prec UMINUS"""

        node = Node("NUMBER", None, p[2])
        # node.leaf = -node.leaf
        p[0] = node
    def p_value(self, p):
        """expr : term"""

        node = Node("CONTAINER", None, "_expr")
        node.children.append(p[1])
        p[0] = node
    def p_expr(self, p):
        """expr : operation_expression"""

        node = Node("CONTAINER", None, "_expr")
        node.children.append(p[1])
        p[0] = node
                print(t)
            print("-------------")

    def get_root(self):
        return self.nodeRoot

    def get_fun_list(self):
        return self._fun_list


if __name__ == "__main__":

    s = CalcParser()
    while 1:
        s.run()
        nod = Node()
        nod.debug_node(s.get_root())
        print("-------------")
        nod.debug_tree(s.get_root())
        print()
        print("-------------")
        a = nod.to_dict_from_node(s.get_root(), {})
        print(a)
        print("-------------")
        a = nod.to_json_string_from_dict(a)
        print(a)
        print("-------------")
        a = nod.to_node_from_json(a)
        print(nod.debug_node(a))
        print("-------------")
Exemple #14
0
 def __init__(self, *args, **kwargs):
     """ A concept in the sematic network.
     """
     Node.__init__(self, *args, **kwargs)
     self._properties = None
'''
reverse a linked list



'''

from __init__ import Node

a = Node(1)
b = Node(2)
c = Node(3)

# Linking the nodes
a.nextnode = b
b.nextnode = c


def Reversal(node):
    lst = []
    while node != None and node.nextnode != None:
        lst.append(node)
        node = node.nextnode
    lst.append(node)
    print(lst)
    lst = lst[::-1]
    for e in range(len(lst) - 1):
        lst[e].nextnode = lst[e + 1]
    lst[-1].nextnode = None

Exemple #16
0
 def __init__(self, *args, **kwargs):
     """ A concept in the sematic network.
     """
     Node.__init__(self, *args, **kwargs)
     self._properties = None