Answers for "python node class"

0

python node class

class Node(object):
 
    def __init__(self, data=None, next_node=None):
        self.data = data
        self.next_node = next_node
Posted by: Guest on July-17-2021
0

node class python

You need to find the last node without a .nextEl pointer and add the node there:

def add(self, newNode):
    node = self.firstNode
    while node.nextEl is not None:
        node = next.nextEl
    node.nextEl = newNode
Because this has to traverse the whole list, most linked-list implementations also keep a reference to the last element:

class List(object):
    first = last = None

    def __init__(self, fnode):
        self.add(fnode)

    def add(self, newNode):
        if self.first is None:
            self.first = self.last = newNode
        else:
            self.last.nextEl = self.last = newNode
Because Python assigns to multiple targets from left to right, self.last.nextEl is set to newNode before self.last.

Some style notes on your code:

Use is None and is not None to test if an identifier points to None (it's a singleton).
There is no need for accessors in Python; just refer to the attributes directly.
Unless this is Python 3, use new-style classes by inheriting from object:

class Node(object):
    # ...
Posted by: Guest on March-22-2021
-1

python node

class Node:

    def __init__(self, data):

        self.left = None
        self.right = None
        self.data = data


    def PrintTree(self):
        print(self.data)

root = Node(10)

root.PrintTree()
Posted by: Guest on March-31-2020

Python Answers by Framework

Browse Popular Code Answers by Language