grscheller.datastructures.nodes

Nodes for Graphs

Node classes used with graph-like data structures.

  • designed to be "tinker-toyed" together in a variety of ways
  • passive data structures manipulated by the classes containing them
 1# Copyright 2023-2024 Geoffrey R. Scheller
 2#
 3# Licensed under the Apache License, Version 2.0 (the "License");
 4# you may not use this file except in compliance with the License.
 5# You may obtain a copy of the License at
 6#
 7#     http://www.apache.org/licenses/LICENSE-2.0
 8#
 9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""
16### Nodes for Graphs
17
18Node classes used with graph-like data structures.
19
20* designed to be "tinker-toyed" together in a variety of ways
21* passive data structures manipulated by the classes containing them
22
23"""
24from __future__ import annotations
25from typing import Generic, Optional, TypeVar
26
27__all__ = ['SL_Node', 'DL_Node']
28
29T = TypeVar('T')
30
31class SL_Node(Generic[T]):
32    """
33    #### Singularly Linked Node
34
35    Class implements singularly link nodes for graph-like data structures.
36
37    * this type of node always contain data, even if that data is None
38      * in a Boolean context always returns true
39    * more than one node can point to the same node forming bush like graphs
40    * circular graphs are possible
41
42    """
43    __slots__ = '_data', '_next'
44
45    def __init__(self, data: T, next: Optional[SL_Node[T]]):
46        self._data = data
47        self._next = next
48
49    def __bool__(self) -> bool:
50        return True
51
52class DL_Node(Generic[T]):
53    """
54    #### Doubly Linked Node
55
56    Class implements doubly linked nodes for graph-like data structures.
57
58    * this type of node always contain data, even if that data is None
59      * in a Boolean context always returns true
60    * recursive binary trees possible
61    * doubly link lists possible
62    * circular graphs are possible
63
64    """
65    __slots__ = '_data', '_left', '_right'
66
67    def __init__(self, left: Optional[DL_Node[T]], data: T, right: Optional[DL_Node[T]]):
68        self._data = data
69        self._left = left
70        self._right = right
71
72    def __bool__(self) -> bool:
73        return True
class SL_Node(typing.Generic[~T]):
32class SL_Node(Generic[T]):
33    """
34    #### Singularly Linked Node
35
36    Class implements singularly link nodes for graph-like data structures.
37
38    * this type of node always contain data, even if that data is None
39      * in a Boolean context always returns true
40    * more than one node can point to the same node forming bush like graphs
41    * circular graphs are possible
42
43    """
44    __slots__ = '_data', '_next'
45
46    def __init__(self, data: T, next: Optional[SL_Node[T]]):
47        self._data = data
48        self._next = next
49
50    def __bool__(self) -> bool:
51        return True

Singularly Linked Node

Class implements singularly link nodes for graph-like data structures.

  • this type of node always contain data, even if that data is None
    • in a Boolean context always returns true
  • more than one node can point to the same node forming bush like graphs
  • circular graphs are possible
SL_Node( data: ~T, next: Optional[SL_Node[~T]])
46    def __init__(self, data: T, next: Optional[SL_Node[T]]):
47        self._data = data
48        self._next = next
class DL_Node(typing.Generic[~T]):
53class DL_Node(Generic[T]):
54    """
55    #### Doubly Linked Node
56
57    Class implements doubly linked nodes for graph-like data structures.
58
59    * this type of node always contain data, even if that data is None
60      * in a Boolean context always returns true
61    * recursive binary trees possible
62    * doubly link lists possible
63    * circular graphs are possible
64
65    """
66    __slots__ = '_data', '_left', '_right'
67
68    def __init__(self, left: Optional[DL_Node[T]], data: T, right: Optional[DL_Node[T]]):
69        self._data = data
70        self._left = left
71        self._right = right
72
73    def __bool__(self) -> bool:
74        return True

Doubly Linked Node

Class implements doubly linked nodes for graph-like data structures.

  • this type of node always contain data, even if that data is None
    • in a Boolean context always returns true
  • recursive binary trees possible
  • doubly link lists possible
  • circular graphs are possible
DL_Node( left: Optional[DL_Node[~T]], data: ~T, right: Optional[DL_Node[~T]])
68    def __init__(self, left: Optional[DL_Node[T]], data: T, right: Optional[DL_Node[T]]):
69        self._data = data
70        self._left = left
71        self._right = right