辅助类BinaryTreeNode(二叉树节点)

辅助类BinaryTreeNode

(二叉树节点)

template<typename T>
class BinaryTreeNode
{
public:
    T key;
    BinaryTreeNode<T>* parent;
    BinaryTreeNode<T>* left;
    BinaryTreeNode<T>* right;
public:
    BinaryTreeNode(){}
    BinaryTreeNode(T key,BinaryTreeNode<T>* parent = nullptr,BinaryTreeNode<T>* left = nullptr,BinaryTreeNode<T>* right = nullptr):key(key),parent(parent),left(left),right(right){}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树结点的定义: ```python class BinaryTreeNode: def __init__(self, val): self.val = val self.left = None self.right = None ``` 二叉树的定义: ```python class BinaryTree: def __init__(self): self.root = None def insert(self, val): node = BinaryTreeNode(val) if self.root is None: self.root = node else: parent = None current = self.root while current is not None: parent = current if val < current.val: current = current.left else: current = current.right if val < parent.val: parent.left = node else: parent.right = node def search(self, val): current = self.root while current is not None: if current.val == val: return True elif val < current.val: current = current.left else: current = current.right return False def inorder(self): def _inorder(node): if node is None: return _inorder(node.left) print(node.val, end=' ') _inorder(node.right) _inorder(self.root) print() def preorder(self): def _preorder(node): if node is None: return print(node.val, end=' ') _preorder(node.left) _preorder(node.right) _preorder(self.root) print() def postorder(self): def _postorder(node): if node is None: return _postorder(node.left) _postorder(node.right) print(node.val, end=' ') _postorder(self.root) print() def levelorder(self): if self.root is None: return queue = [self.root] while queue: node = queue.pop(0) print(node.val, end=' ') if node.left is not None: queue.append(node.left) if node.right is not None: queue.append(node.right) print() ``` 这里定义了二叉树的基本操作,包括插入、查找和遍历(前序、中序、后序和层序遍历)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值