Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Feb 14, 2023
2 parents 925e05f + 7238c56 commit 113450d
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 39 deletions.
11 changes: 6 additions & 5 deletions codes/cpp/chapter_graph/graph_adjacency_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/* 构造方法 */
Expand Down Expand Up @@ -52,17 +53,17 @@ class GraphAdjList {
/* 添加顶点 */
void addVertex(Vertex* vet) {
if (adjList.count(vet)) return;
// 在邻接表中添加一个新链表(即 HashSet)
// 在邻接表中添加一个新链表
adjList[vet] = unordered_set<Vertex*>();
}

/* 删除顶点 */
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);
}
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_tree/binary_tree_bfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../include/include.hpp"

/* 层序遍历 */
vector<int> hierOrder(TreeNode* root) {
vector<int> levelOrder(TreeNode* root) {
// 初始化队列,加入根结点
queue<TreeNode*> queue;
queue.push(root);
Expand Down Expand Up @@ -35,7 +35,7 @@ int main() {
PrintUtil::printTree(root);

/* 层序遍历 */
vector<int> vec = hierOrder(root);
vector<int> vec = levelOrder(root);
cout << endl << "层序遍历的结点打印序列 = ";
PrintUtil::printVector(vec);

Expand Down
4 changes: 2 additions & 2 deletions codes/csharp/chapter_tree/binary_tree_bfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -41,7 +41,7 @@ public void Test()
Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root);

List<int> list = hierOrder(root);
List<int> list = levelOrder(root);
Console.WriteLine("\n层序遍历的结点打印序列 = " + string.Join(",", list.ToArray()));
}
}
8 changes: 4 additions & 4 deletions codes/go/chapter_graph/graph_adjacency_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func newVertex(val int) vertex {

/* 基于邻接表实现的无向图类 */
type graphAdjList struct {
// 请注意,vertices 和 adjList 中存储的都是 Vertex 对象
// 邻接表(使用哈希表实现), 使用哈希表模拟集合
// 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
// 请注意,adjList 中的元素是 Vertex 对象
adjList map[vertex]map[vertex]struct{}
}

Expand Down Expand Up @@ -78,7 +78,7 @@ func (g *graphAdjList) addVertex(vet vertex) {
if ok {
return
}
// 在邻接表中添加一个新链表(即 set)
// 在邻接表中添加一个新链表
g.adjList[vet] = make(map[vertex]struct{})
}

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_tree/binary_tree_bfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

/* 层序遍历 */
func hierOrder(root *TreeNode) []int {
func levelOrder(root *TreeNode) []int {
// 初始化队列,加入根结点
queue := list.New()
queue.PushBack(root)
Expand Down
4 changes: 2 additions & 2 deletions codes/go/chapter_tree/binary_tree_bfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ 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})
fmt.Println("\n初始化二叉树: ")
PrintTree(root)

// 层序遍历
nums := hierOrder(root)
nums := levelOrder(root)
fmt.Println("\n层序遍历的结点打印序列 =", nums)
}
4 changes: 2 additions & 2 deletions codes/java/chapter_tree/binary_tree_bfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class binary_tree_bfs {
/* 层序遍历 */
static List<Integer> hierOrder(TreeNode root) {
static List<Integer> levelOrder(TreeNode root) {
// 初始化队列,加入根结点
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
// 初始化一个列表,用于保存遍历序列
Expand All @@ -35,7 +35,7 @@ public static void main(String[] args) {
PrintUtil.printTree(root);

/* 层序遍历 */
List<Integer> list = hierOrder(root);
List<Integer> list = levelOrder(root);
System.out.println("\n层序遍历的结点打印序列 = " + list);
}
}
9 changes: 6 additions & 3 deletions codes/javascript/chapter_graph/graph_adjacency_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class Vertex {

/* 基于邻接表实现的无向图类 */
class GraphAdjList {
// 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
// 请注意,adjList 中的元素是 Vertex 对象
adjList;

/* 构造方法 */
constructor(edges) {
this.adjList = new Map();
Expand Down Expand Up @@ -54,7 +57,7 @@ class GraphAdjList {
/* 添加顶点 */
addVertex(vet) {
if (this.adjList.has(vet)) return;
// 在邻接表中添加一个新链表(即 HashSet)
// 在邻接表中添加一个新链表
this.adjList.set(vet, new Set());
}

Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions codes/javascript/chapter_tree/binary_tree_bfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { arrToTree } = require("../include/TreeNode");
const { printTree } = require("../include/PrintUtil");

/* 层序遍历 */
function hierOrder(root) {
function levelOrder(root) {
// 初始化队列,加入根结点
let queue = [root];
// 初始化一个列表,用于保存遍历序列
Expand All @@ -33,5 +33,5 @@ console.log("\n初始化二叉树\n");
printTree(root);

/* 层序遍历 */
let list = hierOrder(root);
let list = levelOrder(root);
console.log("\n层序遍历的结点打印序列 = " + list);
4 changes: 2 additions & 2 deletions codes/python/chapter_tree/binary_tree_bfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


""" 层序遍历 """
def hier_order(root: Optional[TreeNode]):
def level_order(root: Optional[TreeNode]):
# 初始化队列,加入根结点
queue = collections.deque()
queue.append(root)
Expand All @@ -35,6 +35,6 @@ def hier_order(root: Optional[TreeNode]):
print_tree(root)

# 层序遍历
res = hier_order(root)
res = level_order(root)
print("\n层序遍历的结点打印序列 = ", res)
assert res == [1, 2, 3, 4, 5, 6, 7]
12 changes: 7 additions & 5 deletions codes/swift/chapter_graph/graph_adjacency_list.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [:]
// 添加所有顶点和边
Expand Down Expand Up @@ -66,7 +68,7 @@ class GraphAdjList {
if adjList[vet] != nil {
return
}
// 在邻接表中添加一个新链表(即 HashSet)
// 在邻接表中添加一个新链表
adjList[vet] = []
}

Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions codes/swift/chapter_tree/binary_tree_bfs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import utils

/* 层序遍历 */
func hierOrder(root: TreeNode) -> [Int] {
func levelOrder(root: TreeNode) -> [Int] {
// 初始化队列,加入根结点
var queue: [TreeNode] = [root]
// 初始化一个列表,用于保存遍历序列
Expand Down Expand Up @@ -36,7 +36,7 @@ enum BinaryTreeBFS {
PrintUtil.printTree(root: node)

/* 层序遍历 */
let list = hierOrder(root: node)
let list = levelOrder(root: node)
print("\n层序遍历的结点打印序列 = \(list)")
}
}
9 changes: 6 additions & 3 deletions codes/typescript/chapter_graph/graph_adjacency_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class Vertex {

/* 基于邻接表实现的无向图类 */
class GraphAdjList {
// 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
// 请注意,adjList 中的元素是 Vertex 对象
adjList: Map<Vertex, Set<Vertex>>;

/* 构造方法 */
constructor(edges: Vertex[][]) {
this.adjList = new Map();
Expand Down Expand Up @@ -54,7 +57,7 @@ class GraphAdjList {
/* 添加顶点 */
addVertex(vet: Vertex): void {
if (this.adjList.has(vet)) return;
// 在邻接表中添加一个新链表(即 HashSet)
// 在邻接表中添加一个新链表
this.adjList.set(vet, new Set());
}

Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions codes/typescript/chapter_tree/binary_tree_bfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
// 初始化一个列表,用于保存遍历序列
Expand All @@ -35,7 +35,7 @@ console.log('\n初始化二叉树\n');
printTree(root);

/* 层序遍历 */
const list = hierOrder(root);
const list = levelOrder(root);
console.log('\n层序遍历的结点打印序列 = ' + list);

export {};
4 changes: 2 additions & 2 deletions codes/zig/chapter_tree/binary_tree_bfs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 113450d

Please sign in to comment.