畢業也快2年了,畢業以後就沒有認真寫過博客了,都是那裏學習一下,這裏弄一下。學習了也不作筆記,事後就忘記了。我對這種狀態打從內心是討厭的。javascript
進入正題,今天的筆記是記錄javascript二叉樹的學習,是很是基本的知識加上一些本身的認識。適合想了解javascript二叉樹的萌新,大神請繞路走,固然若是發現有什麼理解上的錯誤,還望不吝賜教。html
===================漂亮的分割線===================java
首先說下我對二叉樹的理解:node
1,二叉樹是由一系列有規則的節點構成數組
2,二叉樹每個節點能夠當作爲對象,必有key(節點值),能夠有left(節點),right(節點)這三個屬性學習
3,每一個節點的left節點的key一定小於當前key值this
4,每一個節點的right節點的key一定大於當前的key值spa
那麼假如咱們要把一個數組構成二叉樹,須要哪些代碼?很簡單,記住上面的幾點,咱們來一步一步把數組變成二叉樹形式code
一,htm
1,二叉樹是由一系列有規則的節點構成,
2,二叉樹每個節點能夠當作爲對象,必有key(節點值),能夠有left(節點),right(節點)這三個屬性
思考: 數組的每個值將變成二叉樹的節點(先無論這個節點怎麼排序),那麼咱們在循環數組的時候,是否是都要處理下?
答: 我定義下面一個類,到時候就能夠依次實例化爲節點了。看下好很差理解...
//節點類 class Node { constructor(key) { this.left = null this.right = null this.key = key } }
進一步解釋: 當循環數組的時候,就能夠把每個數組項,實例化爲節點,相似這樣new Node(數組Item)
二,
3,每一個節點的left節點的key一定小於當前key值
4,每一個節點的right節點的key一定大於當前的key值
這2點是生成二叉樹的規則。那咱們是否是應該有一個二叉樹類呢?
//二叉樹類 class BinaryTree{ }
思考: 二叉樹類要哪些東西呢?補充類看下是否是應該這些
//二叉樹類 class BinaryTree { constructor() { this.root = null //二叉樹根節點 } insert (key) { //插入節點的方法 } }
解釋: 每個二叉樹實例應該都有一個根節點,而後暴露一個能夠插入節點的方法(先無論插入規則,後續講解)
接下來就是重點了: 如何實現插入邏輯能實現上面3,4兩點規則呢?
第一點:咱們要肯定一個根節點,而後在根節點基礎上實現二分法
完善insert方法以下:
class BinaryTree { constructor() { this.root = null } insert (key) { const newNode = new Node(key) // 這裏也能夠在數組那裏執行,但這裏能夠避免屢次實例操做 if (this.root === null) { this.root = newNode } else { this.inOrderTraversNode(this.root, newNode) } } inOrderTraversNode (node, newNode) { } }
解釋:當調用insert方法的時候都先肯定root的值,而後在根節點爲入口進行二分。inOrderTraversNode方法就負責二分法。
思考: inOrderTraversNode方法怎麼樣實現二分法?
答: 該方法爲一個遞歸方法。功能爲:有2個參數,一個是當前已經存在的節點,後一個是要插入的新節點,就是把新節點插入到當前節點的正確的(左節點或者右節點)位置。
好比:當根節點肯定以後,調用該方法插入一個新左節點,此時造成了2個節點的二叉樹。此時若又一個左節點須要插入,咱們就能能夠遞歸調用該方法實現了。
//二叉樹類 class BinaryTree { constructor() { this.root = null } insert (key) { const newNode = new Node(key) if (this.root === null) { this.root = newNode } else { this.inOrderTraversNode(this.root, newNode) } } inOrderTraversNode (node, newNode) { if (newNode.key < node.key) { // 左插 if (node.left === null) { node.left = newNode }else{ this.inOrderTraversNode(node.left, newNode) } } else { // 右插 if (node.right === null) { node.right = newNode } else { this.inOrderTraversNode(node.right, newNode) } } } }
全部代碼在這裏,能夠好好理解下
const node = [8, 1, 3, 10, 12, 6, 13, 15, 18, 7] //節點類 class Node { constructor(key) { this.left = null this.right = null this.key = key } } //二叉樹類 class BinaryTree { constructor() { this.root = null } insert (key) { const newNode = new Node(key) if (this.root === null) { this.root = newNode } else { this.inOrderTraversNode(this.root, newNode) } } inOrderTraversNode (node, newNode) { if (newNode.key < node.key) { // 左插 if (node.left === null) { node.left = newNode }else{ this.inOrderTraversNode(node.left, newNode) } } else { // 右插 if (node.right === null) { node.right = newNode } else { this.inOrderTraversNode(node.right, newNode) } } } } const binaryTree = new BinaryTree() node.map(function(item) { binaryTree.insert(item) }) console.log(binaryTree.root)
原文出處:https://www.cnblogs.com/panhe-xue/p/10915486.html