mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-10-31 10:26:48 +08:00 
			
		
		
		
	 5f7385c8a3
			
		
	
	5f7385c8a3
	
	
	
		
			
			* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
		
			
				
	
	
		
			143 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			143 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // File: binary_search_tree.go
 | |
| // Created Time: 2022-11-26
 | |
| // Author: Reanon (793584285@qq.com)
 | |
| 
 | |
| package chapter_tree
 | |
| 
 | |
| import (
 | |
| 	. "github.com/krahets/hello-algo/pkg"
 | |
| )
 | |
| 
 | |
| type binarySearchTree struct {
 | |
| 	root *TreeNode
 | |
| }
 | |
| 
 | |
| func newBinarySearchTree() *binarySearchTree {
 | |
| 	bst := &binarySearchTree{}
 | |
| 	// 初始化空樹
 | |
| 	bst.root = nil
 | |
| 	return bst
 | |
| }
 | |
| 
 | |
| /* 獲取根節點 */
 | |
| func (bst *binarySearchTree) getRoot() *TreeNode {
 | |
| 	return bst.root
 | |
| }
 | |
| 
 | |
| /* 查詢節點 */
 | |
| func (bst *binarySearchTree) search(num int) *TreeNode {
 | |
| 	node := bst.root
 | |
| 	// 迴圈查詢,越過葉節點後跳出
 | |
| 	for node != nil {
 | |
| 		if node.Val.(int) < num {
 | |
| 			// 目標節點在 cur 的右子樹中
 | |
| 			node = node.Right
 | |
| 		} else if node.Val.(int) > num {
 | |
| 			// 目標節點在 cur 的左子樹中
 | |
| 			node = node.Left
 | |
| 		} else {
 | |
| 			// 找到目標節點,跳出迴圈
 | |
| 			break
 | |
| 		}
 | |
| 	}
 | |
| 	// 返回目標節點
 | |
| 	return node
 | |
| }
 | |
| 
 | |
| /* 插入節點 */
 | |
| func (bst *binarySearchTree) insert(num int) {
 | |
| 	cur := bst.root
 | |
| 	// 若樹為空,則初始化根節點
 | |
| 	if cur == nil {
 | |
| 		bst.root = NewTreeNode(num)
 | |
| 		return
 | |
| 	}
 | |
| 	// 待插入節點之前的節點位置
 | |
| 	var pre *TreeNode = nil
 | |
| 	// 迴圈查詢,越過葉節點後跳出
 | |
| 	for cur != nil {
 | |
| 		if cur.Val == num {
 | |
| 			return
 | |
| 		}
 | |
| 		pre = cur
 | |
| 		if cur.Val.(int) < num {
 | |
| 			cur = cur.Right
 | |
| 		} else {
 | |
| 			cur = cur.Left
 | |
| 		}
 | |
| 	}
 | |
| 	// 插入節點
 | |
| 	node := NewTreeNode(num)
 | |
| 	if pre.Val.(int) < num {
 | |
| 		pre.Right = node
 | |
| 	} else {
 | |
| 		pre.Left = node
 | |
| 	}
 | |
| }
 | |
| 
 | |
| /* 刪除節點 */
 | |
| func (bst *binarySearchTree) remove(num int) {
 | |
| 	cur := bst.root
 | |
| 	// 若樹為空,直接提前返回
 | |
| 	if cur == nil {
 | |
| 		return
 | |
| 	}
 | |
| 	// 待刪除節點之前的節點位置
 | |
| 	var pre *TreeNode = nil
 | |
| 	// 迴圈查詢,越過葉節點後跳出
 | |
| 	for cur != nil {
 | |
| 		if cur.Val == num {
 | |
| 			break
 | |
| 		}
 | |
| 		pre = cur
 | |
| 		if cur.Val.(int) < num {
 | |
| 			// 待刪除節點在右子樹中
 | |
| 			cur = cur.Right
 | |
| 		} else {
 | |
| 			// 待刪除節點在左子樹中
 | |
| 			cur = cur.Left
 | |
| 		}
 | |
| 	}
 | |
| 	// 若無待刪除節點,則直接返回
 | |
| 	if cur == nil {
 | |
| 		return
 | |
| 	}
 | |
| 	// 子節點數為 0 或 1
 | |
| 	if cur.Left == nil || cur.Right == nil {
 | |
| 		var child *TreeNode = nil
 | |
| 		// 取出待刪除節點的子節點
 | |
| 		if cur.Left != nil {
 | |
| 			child = cur.Left
 | |
| 		} else {
 | |
| 			child = cur.Right
 | |
| 		}
 | |
| 		// 刪除節點 cur
 | |
| 		if cur != bst.root {
 | |
| 			if pre.Left == cur {
 | |
| 				pre.Left = child
 | |
| 			} else {
 | |
| 				pre.Right = child
 | |
| 			}
 | |
| 		} else {
 | |
| 			// 若刪除節點為根節點,則重新指定根節點
 | |
| 			bst.root = child
 | |
| 		}
 | |
| 		// 子節點數為 2
 | |
| 	} else {
 | |
| 		// 獲取中序走訪中待刪除節點 cur 的下一個節點
 | |
| 		tmp := cur.Right
 | |
| 		for tmp.Left != nil {
 | |
| 			tmp = tmp.Left
 | |
| 		}
 | |
| 		// 遞迴刪除節點 tmp
 | |
| 		bst.remove(tmp.Val.(int))
 | |
| 		// 用 tmp 覆蓋 cur
 | |
| 		cur.Val = tmp.Val
 | |
| 	}
 | |
| }
 | |
| 
 | |
| /* 列印二元搜尋樹 */
 | |
| func (bst *binarySearchTree) print() {
 | |
| 	PrintTree(bst.root)
 | |
| }
 |