【树】Level_Order_Traversal

本文介绍了一种实现二叉树层序遍历的方法,使用数组存储每一层的节点,并通过递增的方式记录队列的头部和尾部,确保每个节点只被访问一次。最后对所有节点进行操作。
void Level_order(Tree T, void (*visit)(Tree ThisNode))
{
Tree a[105];
int front, rear, i;
i = front = rear = 0;
a[rear] = T;
if (a[i] == NULL) return;
while (i <= rear)
{

if (a[i]->Left != NULL)
{
rear++;
a[rear] = a[i]->Left;
}
if (a[i]->Right != NULL)
{
rear++;
a[rear] = a[i]->Right;
}
i++;
}
for (i = 0; i <= rear; i++)
{
(*visit)(a[i]);
}
return;
}
class TreeNode: """ 孩子兄弟表示法的节点类 """ def __init__(self, data): self.data = data # 节点数据 self.firstChild = None # 第一个孩子 self.nextSibling = None # 下一个兄弟 class Tree: def __init__(self, root_data): self.root = TreeNode(root_data) def build_tree(): # 创建节点 A = Tree('A').root B = TreeNode('B') C = TreeNode('C') D = TreeNode('D') E = TreeNode('E') F = TreeNode('F') G = TreeNode('G') H = TreeNode('H') I = TreeNode('I') J = TreeNode('J') K = TreeNode('K') L = TreeNode('L') M = TreeNode('M') # 设置孩子关系(firstChild) A.firstChild = B B.firstChild = E C.firstChild = G D.firstChild = M E.firstChild = K # 设置兄弟关系(nextSibling) B.nextSibling = C C.nextSibling = D E.nextSibling = F H.nextSibling = I I.nextSibling = J K.nextSibling = L return A def parent_representation(): """ 双亲表示法:返回 (data, parent_index) 列表 """ nodes = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J', 'K', 'L', 'M'] parents = [-1, 0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5] # 索引对应双亲位置 print("双亲表示法:") for i, data in enumerate(nodes): print(f"Index {i}: {data}, Parent Index: {parents[i]}") def level_order_traversal(root): """ 层次遍历(广度优先) """ if not root: return [] result = [] queue = [root] while queue: node = queue.pop(0) result.append(node.data) # 先加入第一个孩子 child = node.firstChild while child: queue.append(child) child = child.nextSibling return result def pre_order_traversal(root): """ 先序遍历(DLR) """ if not root: return [] return [root.data] + pre_order_traversal(root.firstChild) + pre_order_traversal(root.nextSibling) def in_order_traversal(root): """ 中序遍历(LDR) """ if not root: return [] left = in_order_traversal(root.firstChild) middle = [root.data] right = in_order_traversal(root.nextSibling) return left + middle + right def post_order_traversal(root): """ 后序遍历(LRD) """ if not root: return [] left = post_order_traversal(root.firstChild) right = post_order_traversal(root.nextSibling) return left + right + [root.data] # 主程序执行 if __name__ == "__main__": # 1. 双亲表示法 parent_representation() print() # 2. 构建孩子兄弟表示法的 root = build_tree() # 层次遍历 print("层次遍历:", " ".join(level_order_traversal(root))) # 先序遍历 print("先序遍历:", " ".join(pre_order_traversal(root))) # 中序遍历 print("中序遍历:", " ".join(in_order_traversal(root))) # 后序遍历 print("后序遍历:", " ".join(post_order_traversal(root)))
最新发布
11-14
优化下面代码class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None root = TreeNode('a') root.left = TreeNode('b') root.right = TreeNode('c') root.left.left = TreeNode('d') root.left.right = TreeNode('e') root.right.left = TreeNode('f') root.right.right = TreeNode('g') root.left.left.left = TreeNode('h') root.left.left.right = TreeNode('i') def preorder_traversal(root): if not root: return print(root.val, end=' ') preorder_traversal(root.left) preorder_traversal(root.right) def inorder_traversal(root): if not root: return inorder_traversal(root.left) print(root.val, end=' ') inorder_traversal(root.right) def postorder_traversal(root): if not root: return postorder_traversal(root.left) postorder_traversal(root.right) print(root.val, end=' ') from collections import deque def level_order_traversal(root): if not root: return queue = deque() queue.append(root) while queue: node = queue.popleft() print(node.val, end=' ') if node.left: queue.append(node.left) if node.right: queue.append(node.right) def get_height(root): if not root: return 0 left_height = get_height(root.left) right_height = get_height(root.right) return max(left_height, right_height) + 1 def get_node_count(root): if not root: return 0 left_node_count = get_node_count(root.left) right_node_count = get_node_count(root.right) return left_node_count + right_node_count + 1 print("先序遍历:") preorder_traversal(root) print("中序遍历:") inorder_traversal(root) print("后序遍历:") postorder_traversal(root) print("层次遍历:") level_order_traversal(root) print("该二叉的高度为:") get_height(root) print("该二叉的节点个数为 ") get_node_count(root)
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值