自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

尋找二叉樹的下一個節(jié)點

開發(fā) 前端
已知一個包含父節(jié)點引用的二叉樹和其中的一個節(jié)點,如何找出這個節(jié)點中序遍歷序列的下一個節(jié)點?

本文轉載自微信公眾號「神奇的程序員k」,作者神奇的程序員K。轉載本文請聯(lián)系神奇的程序員k公眾號。  

前言

已知一個包含父節(jié)點引用的二叉樹和其中的一個節(jié)點,如何找出這個節(jié)點中序遍歷序列的下一個節(jié)點?

本文就跟大家分享下這個問題的解決方案與實現(xiàn)代碼,歡迎各位感興趣的開發(fā)者閱讀本文。

問題分析

正如前言所述,我們的已知條件如下:

  • 包含父節(jié)點引用的二叉樹
  • 要查找的節(jié)點

我們要解決的問題:

  • 找出要查找節(jié)點中序遍歷序列的下一個節(jié)點

接下來,我們通過舉例來推導下一個節(jié)點的規(guī)律,我們先來畫一顆二叉搜索樹,如下所示:

  1.  
  2.    / \ 
  3.   6   13 
  4.  / \  / \ 
  5. 3  7 9  15 

例如,我們尋找6的下一個節(jié)點,根據(jù)中序遍歷的規(guī)則我們可知它的下一個節(jié)點是7

  • 8的下一個節(jié)點是9
  • 3的下一個節(jié)點是6
  • 7的下一個節(jié)點是8

通過上述例子,我們可以分析出下述信息:

  • 要查找的節(jié)點存在右子樹,那么它的下一個節(jié)點就是其右子樹中的最左子節(jié)點
  • 要查找的節(jié)點不存右子樹:
    • 當前節(jié)點屬于父節(jié)點的左子節(jié)點,那么它的下一個節(jié)點就是其父節(jié)點本身
    • 當前節(jié)點屬于父節(jié)點的右子節(jié)點,那么就需要沿著父節(jié)點的指針一直向上遍歷,直至找到一個是它父節(jié)點的左子節(jié)點的節(jié)點

上述規(guī)律可能有點繞,大家可以將規(guī)律代入問題中多驗證幾次,就能理解了。

實現(xiàn)思路

  • 二叉樹中插入節(jié)點時保存其父節(jié)點的引用
  • 調用二叉樹的搜索節(jié)點方法,找到要查找的節(jié)點信息
  • 判斷找到的節(jié)點是否存在右子樹
  • 如果存在,則遍歷它的左子樹至葉節(jié)點,將其返回。
  • 如果不存在,則遍歷它的父節(jié)點至根節(jié)點,直至找到一個節(jié)點與它父節(jié)點的左子節(jié)點相等的節(jié)點,將其返回。

實現(xiàn)代碼

接下來,我們將上述思路轉換為代碼,本文代碼中用到的二叉樹相關實現(xiàn)請移步我的另一篇文章:TypeScript實現(xiàn)二叉搜索樹

搜索要查找的節(jié)點

我們需要找到要查找節(jié)點在二叉樹中的節(jié)點信息,才能繼續(xù)實現(xiàn)后續(xù)步驟,搜索節(jié)點的代碼如下:

  1. import { Node } from "./Node.ts"
  2.  
  3. export default class BinarySearchTree<T> { 
  4.     protected root: Node<T> | undefined; 
  5.  
  6.     constructor(protected compareFn: ICompareFunction<T> = defaultCompare) { 
  7.         this.root = undefined; 
  8.     } 
  9.    
  10.     // 搜索特定值 
  11.     search(key: T): boolean | Node<T> { 
  12.         return this.searchNode(<Node<T>>this.root, key); 
  13.     } 
  14.  
  15.     // 搜索節(jié)點 
  16.     private searchNode(node: Node<T>, key: T): boolean | Node<T> { 
  17.         if (node == null) { 
  18.             return false
  19.         } 
  20.  
  21.         if (this.compareFn(key, node.key) === Compare.LESS_THAN) { 
  22.             // 要查找的key在節(jié)點的左側 
  23.             return this.searchNode(<Node<T>>node.leftkey); 
  24.         } else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) { 
  25.             // 要查找的key在節(jié)點的右側 
  26.             return this.searchNode(<Node<T>>node.rightkey); 
  27.         } else { 
  28.             // 節(jié)點已找到 
  29.             return node; 
  30.         } 
  31.     } 

保存父節(jié)點引用

此處的二叉樹與我們實現(xiàn)的二叉樹稍有不同,插入節(jié)點時需要保存父節(jié)點的引用,實現(xiàn)代碼如下:

  1. export default class BinarySearchTree<T> { 
  2.     // 插入方法 
  3.     insert(key: T): void { 
  4.         if (this.root == null) { 
  5.             // 如果根節(jié)點不存在則直接新建一個節(jié)點 
  6.             this.root = new Node(key); 
  7.         } else { 
  8.             // 在根節(jié)點中找合適的位置插入子節(jié)點 
  9.             this.insertNode(this.root, key); 
  10.         } 
  11.     } 
  12.    
  13.     // 節(jié)點插入 
  14.     protected insertNode(node: Node<T>, key: T): void { 
  15.         // 新節(jié)點的鍵小于當前節(jié)點的鍵,則將新節(jié)點插入當前節(jié)點的左邊 
  16.         // 新節(jié)點的鍵大于當前節(jié)點的鍵,則將新節(jié)點插入當前節(jié)點的右邊 
  17.         if (this.compareFn(key, node.key) === Compare.LESS_THAN) { 
  18.             if (node.left == null) { 
  19.                 // 當前節(jié)點的左子樹為null直接插入 
  20.                 node.left = new Node(key, node); 
  21.             } else { 
  22.                 // 從當前節(jié)點(左子樹)向下遞歸,找到null位置將其插入 
  23.                 this.insertNode(node.leftkey); 
  24.             } 
  25.         } else { 
  26.             if (node.right == null) { 
  27.                 // 當前節(jié)點的右子樹為null直接插入 
  28.                 node.right = new Node(key, node); 
  29.             } else { 
  30.                 // 從當前節(jié)點(右子樹)向下遞歸,找到null位置將其插入 
  31.                 this.insertNode(node.rightkey); 
  32.             } 
  33.         } 
  34.     } 
  35.  
  36. /** 
  37.  * 二叉樹的輔助類: 用于存儲二叉樹的每個節(jié)點 
  38.  */ 
  39. export class Node<K> { 
  40.     public left: Node<K> | undefined; 
  41.     public right: Node<K> | undefined; 
  42.     public parent: Node<K> | undefined; 
  43.     constructor(public key: K, parent?: Node<K>) { 
  44.         this.left = undefined; 
  45.         this.right = undefined; 
  46.         console.log(key"的父節(jié)點", parent?.key); 
  47.         this.parent = parent; 
  48.     } 
  49.  
  50.     toString(): string { 
  51.         return `${this.key}`; 
  52.     } 

我們來測試下上述代碼,驗證下父節(jié)點引用是否成功:

  1. const tree = new BinarySearchTree(); 
  2. tree.insert(8); 
  3. tree.insert(6); 
  4. tree.insert(3); 
  5. tree.insert(7); 
  6. tree.insert(13); 
  7. tree.insert(9); 
  8. tree.insert(15); 

 

在保存父節(jié)點引用時折騰了好久也沒實現(xiàn),最后求助了我的朋友_Dreams😁。

尋找下一個節(jié)點

接下來,我們就可以根據(jù)節(jié)點的規(guī)律來實現(xiàn)這個算法了,實現(xiàn)代碼如下:

  1. export class TreeOperate<T> { 
  2.     /** 
  3.      * 尋找二叉樹的下一個節(jié)點 
  4.      * 規(guī)則: 
  5.      *  1. 輸入一個包含父節(jié)點引用的二叉樹和其中的一個節(jié)點 
  6.      *  2. 找出這個節(jié)點中序遍歷序列的下一個節(jié)點 
  7.      * 
  8.      * 例如: 
  9.      *       8 
  10.      *      / \ 
  11.      *     6   13 
  12.      *    / \  / \ 
  13.      *   3  7 9  15 
  14.      * 
  15.      * 6的下一個節(jié)點是7,8的下一個節(jié)點是9 
  16.      * 
  17.      * 通過分析,我們可以得到下述信息: 
  18.      *  1. 如果一個節(jié)點有右子樹,那么它的下一個節(jié)點就是其右子樹中的最左子節(jié)點 
  19.      *  2. 如果一個節(jié)點沒有右子樹: 
  20.      *  (1). 當前節(jié)點屬于父節(jié)點的左子節(jié)點,那么它的下一個節(jié)點就是其父節(jié)點本身 
  21.      *  (2). 當前節(jié)點屬于父節(jié)點的右子節(jié)點,沿著父節(jié)點的指針一直向上遍歷,直至找到一個是它父節(jié)點的左子節(jié)點的節(jié)點 
  22.      * 
  23.      */ 
  24.     findBinaryTreeNextNode(tree: BinarySearchTree<number>, node: number): null | Node<number> { 
  25.         // 搜索節(jié)點 
  26.         const result: Node<number> | boolean = tree.search(node); 
  27.         if (result == null) throw "節(jié)點不存在"
  28.         let currentNode = result as Node<number>; 
  29.         // 右子樹存在 
  30.         if (currentNode.right) { 
  31.             currentNode = currentNode.right
  32.             // 取右子樹的最左子節(jié)點 
  33.             while (currentNode.left) { 
  34.                 currentNode = currentNode.left
  35.             } 
  36.             return currentNode; 
  37.         } 
  38.  
  39.         // 右子樹不存在 
  40.         while (currentNode.parent) { 
  41.             // 當前節(jié)點等于它父節(jié)點的左子節(jié)點則條件成立 
  42.             if (currentNode === currentNode.parent.left) { 
  43.                 return currentNode.parent; 
  44.             } 
  45.             // 條件不成立,繼續(xù)獲取它的父節(jié)點 
  46.             currentNode = currentNode.parent; 
  47.         } 
  48.         return null
  49.     } 

我們通過一個例子來測試下上述代碼:

  1. // 構建二叉搜索樹 
  2. const tree = new BinarySearchTree(); 
  3. tree.insert(8); 
  4. tree.insert(6); 
  5. tree.insert(3); 
  6. tree.insert(7); 
  7. tree.insert(13); 
  8. tree.insert(9); 
  9. tree.insert(15); 
  10. // 尋找下一個節(jié)點 
  11. const nextNode = treeOperate.findBinaryTreeNextNode(tree, 7); 
  12. console.log("7的下一個節(jié)點", nextNode.toString()); 

 

 

 

 

代碼地址

文中完整代碼如下:

  • TreeOperate.ts
  • BinarySearchTree.ts

 

責任編輯:武曉燕 來源: 神奇的程序員k
相關推薦

2020-04-27 07:05:58

二叉樹左子樹右子樹

2021-05-06 17:46:30

二叉樹數(shù)據(jù)結構

2021-04-19 07:47:42

數(shù)據(jù)結構二叉樹Tree

2021-04-20 08:37:14

數(shù)據(jù)結構二叉樹

2021-07-16 08:57:31

迭代遍歷二叉樹

2021-03-17 08:19:22

二叉樹LeetCode

2013-07-15 16:35:55

二叉樹迭代器

2022-10-26 23:58:02

二叉樹數(shù)組算法

2021-11-29 10:40:58

二叉樹鏡像節(jié)點

2021-04-28 20:12:27

數(shù)據(jù)結構創(chuàng)建

2021-08-27 11:36:44

二叉樹回溯節(jié)點

2021-09-29 10:19:00

算法平衡二叉樹

2021-12-17 14:26:58

二叉樹節(jié)點數(shù)量

2020-09-23 18:25:40

算法二叉樹多叉樹

2022-11-06 19:43:10

二叉樹指針節(jié)點

2022-07-27 07:45:53

二叉樹鏡像函數(shù)

2018-03-15 08:31:57

二叉樹存儲結構

2021-12-05 18:25:12

二叉樹路徑節(jié)點

2021-09-15 07:56:32

二叉樹層次遍歷

2021-10-12 09:25:11

二叉樹樹形結構
點贊
收藏

51CTO技術棧公眾號