mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	Merge branch 'develop'
This commit is contained in:
		@ -14,8 +14,9 @@ struct Vertex {
 | 
			
		||||
 | 
			
		||||
/* 基于邻接表实现的无向图类 */
 | 
			
		||||
class GraphAdjList {
 | 
			
		||||
    // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象
 | 
			
		||||
    unordered_map<Vertex*, unordered_set<Vertex*>> adjList;  // 邻接表(使用哈希表实现)
 | 
			
		||||
    // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
 | 
			
		||||
    // 请注意,adjList 中的元素是 Vertex 对象
 | 
			
		||||
    unordered_map<Vertex*, unordered_set<Vertex*>> adjList;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    /* 构造方法 */
 | 
			
		||||
@ -52,7 +53,7 @@ public:
 | 
			
		||||
    /* 添加顶点 */
 | 
			
		||||
    void addVertex(Vertex* vet) {
 | 
			
		||||
        if (adjList.count(vet)) return;
 | 
			
		||||
        // 在邻接表中添加一个新链表(即 HashSet)
 | 
			
		||||
        // 在邻接表中添加一个新链表
 | 
			
		||||
        adjList[vet] = unordered_set<Vertex*>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -60,9 +61,9 @@ public:
 | 
			
		||||
    void removeVertex(Vertex* vet) {
 | 
			
		||||
        if (!adjList.count(vet))
 | 
			
		||||
            throw invalid_argument("不存在顶点");
 | 
			
		||||
        // 在邻接表中删除顶点 vet 对应的链表(即 HashSet)
 | 
			
		||||
        // 在邻接表中删除顶点 vet 对应的链表
 | 
			
		||||
        adjList.erase(vet);
 | 
			
		||||
        // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边
 | 
			
		||||
        // 遍历其它顶点的链表,删除所有包含 vet 的边
 | 
			
		||||
        for (auto& [key, set_] : adjList) {
 | 
			
		||||
            set_.erase(vet);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -7,7 +7,7 @@
 | 
			
		||||
#include "../include/include.hpp"
 | 
			
		||||
 | 
			
		||||
/* 层序遍历 */
 | 
			
		||||
vector<int> hierOrder(TreeNode* root) {
 | 
			
		||||
vector<int> levelOrder(TreeNode* root) {
 | 
			
		||||
    // 初始化队列,加入根结点
 | 
			
		||||
    queue<TreeNode*> queue;
 | 
			
		||||
    queue.push(root);
 | 
			
		||||
@ -35,7 +35,7 @@ int main() {
 | 
			
		||||
    PrintUtil::printTree(root);
 | 
			
		||||
 | 
			
		||||
    /* 层序遍历 */
 | 
			
		||||
    vector<int> vec = hierOrder(root);
 | 
			
		||||
    vector<int> vec = levelOrder(root);
 | 
			
		||||
    cout << endl << "层序遍历的结点打印序列 = ";
 | 
			
		||||
    PrintUtil::printVector(vec);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,7 @@ public class binary_tree_bfs
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    /* 层序遍历 */
 | 
			
		||||
    public List<int> hierOrder(TreeNode root)
 | 
			
		||||
    public List<int> levelOrder(TreeNode root)
 | 
			
		||||
    {
 | 
			
		||||
        // 初始化队列,加入根结点
 | 
			
		||||
        Queue<TreeNode> queue = new();
 | 
			
		||||
@ -41,7 +41,7 @@ public class binary_tree_bfs
 | 
			
		||||
        Console.WriteLine("\n初始化二叉树\n");
 | 
			
		||||
        PrintUtil.PrintTree(root);
 | 
			
		||||
 | 
			
		||||
        List<int> list = hierOrder(root);
 | 
			
		||||
        List<int> list = levelOrder(root);
 | 
			
		||||
        Console.WriteLine("\n层序遍历的结点打印序列 = " + string.Join(",", list.ToArray()));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -24,8 +24,8 @@ func newVertex(val int) vertex {
 | 
			
		||||
 | 
			
		||||
/* 基于邻接表实现的无向图类 */
 | 
			
		||||
type graphAdjList struct {
 | 
			
		||||
	// 请注意,vertices 和 adjList 中存储的都是 Vertex 对象
 | 
			
		||||
	// 邻接表(使用哈希表实现), 使用哈希表模拟集合
 | 
			
		||||
	// 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
 | 
			
		||||
	// 请注意,adjList 中的元素是 Vertex 对象
 | 
			
		||||
	adjList map[vertex]map[vertex]struct{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -78,7 +78,7 @@ func (g *graphAdjList) addVertex(vet vertex) {
 | 
			
		||||
	if ok {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	// 在邻接表中添加一个新链表(即 set)
 | 
			
		||||
	// 在邻接表中添加一个新链表
 | 
			
		||||
	g.adjList[vet] = make(map[vertex]struct{})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -90,7 +90,7 @@ func (g *graphAdjList) removeVertex(vet vertex) {
 | 
			
		||||
	}
 | 
			
		||||
	// 在邻接表中删除顶点 vet 对应的链表
 | 
			
		||||
	delete(g.adjList, vet)
 | 
			
		||||
	// 遍历其它顶点的链表(即 Set),删除所有包含 vet 的边
 | 
			
		||||
	// 遍历其它顶点的链表,删除所有包含 vet 的边
 | 
			
		||||
	for _, set := range g.adjList {
 | 
			
		||||
		// 操作
 | 
			
		||||
		delete(set, vet)
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,7 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
/* 层序遍历 */
 | 
			
		||||
func hierOrder(root *TreeNode) []int {
 | 
			
		||||
func levelOrder(root *TreeNode) []int {
 | 
			
		||||
	// 初始化队列,加入根结点
 | 
			
		||||
	queue := list.New()
 | 
			
		||||
	queue.PushBack(root)
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,7 @@ import (
 | 
			
		||||
	. "github.com/krahets/hello-algo/pkg"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestHierOrder(t *testing.T) {
 | 
			
		||||
func TestLevelOrder(t *testing.T) {
 | 
			
		||||
	/* 初始化二叉树 */
 | 
			
		||||
	// 这里借助了一个从数组直接生成二叉树的函数
 | 
			
		||||
	root := ArrToTree([]any{1, 2, 3, 4, 5, 6, 7})
 | 
			
		||||
@ -19,6 +19,6 @@ func TestHierOrder(t *testing.T) {
 | 
			
		||||
	PrintTree(root)
 | 
			
		||||
 | 
			
		||||
	// 层序遍历
 | 
			
		||||
	nums := hierOrder(root)
 | 
			
		||||
	nums := levelOrder(root)
 | 
			
		||||
	fmt.Println("\n层序遍历的结点打印序列 =", nums)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,7 @@ import java.util.*;
 | 
			
		||||
 | 
			
		||||
public class binary_tree_bfs {
 | 
			
		||||
    /* 层序遍历 */
 | 
			
		||||
    static List<Integer> hierOrder(TreeNode root) {
 | 
			
		||||
    static List<Integer> levelOrder(TreeNode root) {
 | 
			
		||||
        // 初始化队列,加入根结点
 | 
			
		||||
        Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
 | 
			
		||||
        // 初始化一个列表,用于保存遍历序列
 | 
			
		||||
@ -35,7 +35,7 @@ public class binary_tree_bfs {
 | 
			
		||||
        PrintUtil.printTree(root);
 | 
			
		||||
 | 
			
		||||
        /* 层序遍历 */
 | 
			
		||||
        List<Integer> list = hierOrder(root);
 | 
			
		||||
        List<Integer> list = levelOrder(root);
 | 
			
		||||
        System.out.println("\n层序遍历的结点打印序列 = " + list);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -14,7 +14,10 @@ class Vertex {
 | 
			
		||||
 | 
			
		||||
/* 基于邻接表实现的无向图类 */
 | 
			
		||||
class GraphAdjList {
 | 
			
		||||
    // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
 | 
			
		||||
    // 请注意,adjList 中的元素是 Vertex 对象
 | 
			
		||||
    adjList;
 | 
			
		||||
 | 
			
		||||
    /* 构造方法 */
 | 
			
		||||
    constructor(edges) {
 | 
			
		||||
        this.adjList = new Map();
 | 
			
		||||
@ -54,7 +57,7 @@ class GraphAdjList {
 | 
			
		||||
    /* 添加顶点 */
 | 
			
		||||
    addVertex(vet) {
 | 
			
		||||
        if (this.adjList.has(vet)) return;
 | 
			
		||||
        // 在邻接表中添加一个新链表(即 HashSet)
 | 
			
		||||
        // 在邻接表中添加一个新链表
 | 
			
		||||
        this.adjList.set(vet, new Set());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -63,9 +66,9 @@ class GraphAdjList {
 | 
			
		||||
        if (!this.adjList.has(vet)) {
 | 
			
		||||
            throw new Error("Illegal Argument Exception");
 | 
			
		||||
        }
 | 
			
		||||
        // 在邻接表中删除顶点 vet 对应的链表(即 HashSet)
 | 
			
		||||
        // 在邻接表中删除顶点 vet 对应的链表
 | 
			
		||||
        this.adjList.delete(vet);
 | 
			
		||||
        // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边
 | 
			
		||||
        // 遍历其它顶点的链表,删除所有包含 vet 的边
 | 
			
		||||
        for (let set of this.adjList.values()) {
 | 
			
		||||
            set.delete(vet);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -8,7 +8,7 @@ const { arrToTree } = require("../include/TreeNode");
 | 
			
		||||
const { printTree } = require("../include/PrintUtil");
 | 
			
		||||
 | 
			
		||||
/* 层序遍历 */
 | 
			
		||||
function hierOrder(root) {
 | 
			
		||||
function levelOrder(root) {
 | 
			
		||||
    // 初始化队列,加入根结点
 | 
			
		||||
    let queue = [root];
 | 
			
		||||
    // 初始化一个列表,用于保存遍历序列
 | 
			
		||||
@ -33,5 +33,5 @@ console.log("\n初始化二叉树\n");
 | 
			
		||||
printTree(root);
 | 
			
		||||
 | 
			
		||||
/* 层序遍历 */
 | 
			
		||||
let list = hierOrder(root);
 | 
			
		||||
let list = levelOrder(root);
 | 
			
		||||
console.log("\n层序遍历的结点打印序列 = " + list);
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@ from include import *
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
""" 层序遍历 """
 | 
			
		||||
def hier_order(root: Optional[TreeNode]):
 | 
			
		||||
def level_order(root: Optional[TreeNode]):
 | 
			
		||||
    # 初始化队列,加入根结点
 | 
			
		||||
    queue = collections.deque()
 | 
			
		||||
    queue.append(root)
 | 
			
		||||
@ -35,6 +35,6 @@ if __name__ == "__main__":
 | 
			
		||||
    print_tree(root)
 | 
			
		||||
 | 
			
		||||
    # 层序遍历
 | 
			
		||||
    res = hier_order(root)
 | 
			
		||||
    res = level_order(root)
 | 
			
		||||
    print("\n层序遍历的结点打印序列 = ", res)
 | 
			
		||||
    assert res == [1, 2, 3, 4, 5, 6, 7]
 | 
			
		||||
 | 
			
		||||
@ -23,9 +23,11 @@ class Vertex: Hashable {
 | 
			
		||||
 | 
			
		||||
/* 基于邻接表实现的无向图类 */
 | 
			
		||||
class GraphAdjList {
 | 
			
		||||
    // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象
 | 
			
		||||
    private var adjList: [Vertex: Set<Vertex>] // 邻接表(使用哈希表实现)
 | 
			
		||||
    // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
 | 
			
		||||
    // 请注意,adjList 中的元素是 Vertex 对象
 | 
			
		||||
    private var adjList: [Vertex: Set<Vertex>]
 | 
			
		||||
 | 
			
		||||
    /* 构造方法 */
 | 
			
		||||
    init(edges: [[Vertex]]) {
 | 
			
		||||
        adjList = [:]
 | 
			
		||||
        // 添加所有顶点和边
 | 
			
		||||
@ -66,7 +68,7 @@ class GraphAdjList {
 | 
			
		||||
        if adjList[vet] != nil {
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
        // 在邻接表中添加一个新链表(即 HashSet)
 | 
			
		||||
        // 在邻接表中添加一个新链表
 | 
			
		||||
        adjList[vet] = []
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -75,9 +77,9 @@ class GraphAdjList {
 | 
			
		||||
        if adjList[vet] == nil {
 | 
			
		||||
            fatalError("参数错误")
 | 
			
		||||
        }
 | 
			
		||||
        // 在邻接表中删除顶点 vet 对应的链表(即 HashSet)
 | 
			
		||||
        // 在邻接表中删除顶点 vet 对应的链表
 | 
			
		||||
        adjList.removeValue(forKey: vet)
 | 
			
		||||
        // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边
 | 
			
		||||
        // 遍历其它顶点的链表,删除所有包含 vet 的边
 | 
			
		||||
        for key in adjList.keys {
 | 
			
		||||
            adjList[key]?.remove(vet)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -7,7 +7,7 @@
 | 
			
		||||
import utils
 | 
			
		||||
 | 
			
		||||
/* 层序遍历 */
 | 
			
		||||
func hierOrder(root: TreeNode) -> [Int] {
 | 
			
		||||
func levelOrder(root: TreeNode) -> [Int] {
 | 
			
		||||
    // 初始化队列,加入根结点
 | 
			
		||||
    var queue: [TreeNode] = [root]
 | 
			
		||||
    // 初始化一个列表,用于保存遍历序列
 | 
			
		||||
@ -36,7 +36,7 @@ enum BinaryTreeBFS {
 | 
			
		||||
        PrintUtil.printTree(root: node)
 | 
			
		||||
 | 
			
		||||
        /* 层序遍历 */
 | 
			
		||||
        let list = hierOrder(root: node)
 | 
			
		||||
        let list = levelOrder(root: node)
 | 
			
		||||
        print("\n层序遍历的结点打印序列 = \(list)")
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -14,7 +14,10 @@ class Vertex {
 | 
			
		||||
 | 
			
		||||
/* 基于邻接表实现的无向图类 */
 | 
			
		||||
class GraphAdjList {
 | 
			
		||||
    // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
 | 
			
		||||
    // 请注意,adjList 中的元素是 Vertex 对象
 | 
			
		||||
    adjList: Map<Vertex, Set<Vertex>>;
 | 
			
		||||
 | 
			
		||||
    /* 构造方法 */
 | 
			
		||||
    constructor(edges: Vertex[][]) {
 | 
			
		||||
        this.adjList = new Map();
 | 
			
		||||
@ -54,7 +57,7 @@ class GraphAdjList {
 | 
			
		||||
    /* 添加顶点 */
 | 
			
		||||
    addVertex(vet: Vertex): void {
 | 
			
		||||
        if (this.adjList.has(vet)) return;
 | 
			
		||||
        // 在邻接表中添加一个新链表(即 HashSet)
 | 
			
		||||
        // 在邻接表中添加一个新链表
 | 
			
		||||
        this.adjList.set(vet, new Set());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -63,9 +66,9 @@ class GraphAdjList {
 | 
			
		||||
        if (!this.adjList.has(vet)) {
 | 
			
		||||
            throw new Error("Illegal Argument Exception");
 | 
			
		||||
        }
 | 
			
		||||
        // 在邻接表中删除顶点 vet 对应的链表(即 HashSet)
 | 
			
		||||
        // 在邻接表中删除顶点 vet 对应的链表
 | 
			
		||||
        this.adjList.delete(vet);
 | 
			
		||||
        // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边
 | 
			
		||||
        // 遍历其它顶点的链表,删除所有包含 vet 的边
 | 
			
		||||
        for (let set of this.adjList.values()) {
 | 
			
		||||
            set.delete(vet);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -9,7 +9,7 @@ import { arrToTree } from '../module/TreeNode';
 | 
			
		||||
import { printTree } from '../module/PrintUtil';
 | 
			
		||||
 | 
			
		||||
/* 层序遍历 */
 | 
			
		||||
function hierOrder(root: TreeNode | null): number[] {
 | 
			
		||||
function levelOrder(root: TreeNode | null): number[] {
 | 
			
		||||
    // 初始化队列,加入根结点
 | 
			
		||||
    const queue = [root];
 | 
			
		||||
    // 初始化一个列表,用于保存遍历序列
 | 
			
		||||
@ -35,7 +35,7 @@ console.log('\n初始化二叉树\n');
 | 
			
		||||
printTree(root);
 | 
			
		||||
 | 
			
		||||
/* 层序遍历 */
 | 
			
		||||
const list = hierOrder(root);
 | 
			
		||||
const list = levelOrder(root);
 | 
			
		||||
console.log('\n层序遍历的结点打印序列 = ' + list);
 | 
			
		||||
 | 
			
		||||
export {};
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@ const std = @import("std");
 | 
			
		||||
const inc = @import("include");
 | 
			
		||||
 | 
			
		||||
// 层序遍历
 | 
			
		||||
fn hierOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {
 | 
			
		||||
fn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {
 | 
			
		||||
    // 初始化队列,加入根结点
 | 
			
		||||
    const L = std.TailQueue(*inc.TreeNode(T));
 | 
			
		||||
    var queue = L{};
 | 
			
		||||
@ -48,7 +48,7 @@ pub fn main() !void {
 | 
			
		||||
    try inc.PrintUtil.printTree(root, null, false);
 | 
			
		||||
 | 
			
		||||
    // 层序遍历
 | 
			
		||||
    var list = try hierOrder(i32, mem_allocator, root.?);
 | 
			
		||||
    var list = try levelOrder(i32, mem_allocator, root.?);
 | 
			
		||||
    defer list.deinit();
 | 
			
		||||
    std.debug.print("\n层序遍历的结点打印序列 = ", .{});
 | 
			
		||||
    inc.PrintUtil.printList(i32, list);
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user