二叉搜索樹的構(gòu)建

文章來源https://www.nczonline.net/blog/2009/06/09/computer-science-in-javascript-binary-search-tree-part-1/

基本界面

function BinarySearchTree() { this._root = null; } BinarySearchTree.prototype = //restore constructor constructor: BinarySearchTree, add: function (value){ }, contains: function(value){ }, remove: function(value){ }, size: function(){ }, toArray: function(){ }, toString: function(){ } };//這里size,toArray,toString是文章作者添加的新函數(shù)

contians

BinarySearchTree.prototype = { //more code contains: function(value){ var found = false, current = this._root //make sure there's a node to search while(!found && current){ //if the value is less than the current node's, go left if (value < current.value){ current = current.left; //if the value is greater than the current node's, go right } else if (value > current.value){ current = current.right; //values are equal, found it! } else { found = true; } } //only proceed if the node was found return found; }, //more code };

add

`BinarySearchTree.prototype = {
//more code
add: function(value){
//create a new item object, place data in
var node = {
value: value,

            left: null,
            right: null
        },
        //used to traverse the structure
        current;
    //special case: no items in the tree yet
    if (this._root === null){
        this._root = node;
    } else {
        current = this._root;
        while(true){
            //if the new value is less than this node's value, go left
            if (value < current.value){
                //if there's no left, then the new node belongs there
                if (current.left === null){
                    current.left = node;
                    break;
                } else {
                    current = current.left;
                }
            //if the new value is greater than this node's value, go right
            } else if (value > current.value){
                //if there's no right, then the new node belongs there
                if (current.right === null){
                    current.right = node;
                    break;
                } else {
                    current = current.right;
                }       
            //if the new value is equal to the current one, just ignore
            } else {
                break;
            }
        }
    }
},
//more code

};`

traverse遍歷遞歸

BinarySearchTree.prototype = { //more code traverse: function(process){ //helper function function inOrder(node){ if (node){ //traverse the left subtree if (node.left !== null){ inOrder(node.left); } //call the process method on this node process.call(this, node); //traverse the right subtree if (node.right !== null){ inOrder(node.right); } } } //start with the root inOrder(this._root); }, //more code };

size/toArray/toString

BinarySearchTree.prototype = { //more code size: function(){ var length = 0; this.traverse(function(node){ length++; }); return length; }, toArray: function(){ var result = []; this.traverse(function(node){ result.push(node.value); }); return result; }, toString: function(){ return this.toArray().toString(); }, //more code };

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末承璃,一起剝皮案震驚了整個(gè)濱河市列敲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌义起,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件芍碧,死亡現(xiàn)場(chǎng)離奇詭異栅迄,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)区宇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門娃殖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人议谷,你說我怎么就攤上這事炉爆。” “怎么了卧晓?”我有些...
    開封第一講書人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵芬首,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我逼裆,道長(zhǎng)郁稍,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任胜宇,我火速辦了婚禮耀怜,結(jié)果婚禮上恢着,老公的妹妹穿的比我還像新娘。我一直安慰自己财破,他們只是感情好然评,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著狈究,像睡著了一般碗淌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上抖锥,一...
    開封第一講書人閱讀 51,688評(píng)論 1 305
  • 那天亿眠,我揣著相機(jī)與錄音,去河邊找鬼磅废。 笑死纳像,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的拯勉。 我是一名探鬼主播竟趾,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼宫峦!你這毒婦竟也來了岔帽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤导绷,失蹤者是張志新(化名)和其女友劉穎犀勒,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體妥曲,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贾费,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了檐盟。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片褂萧。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖葵萎,靈堂內(nèi)的尸體忽然破棺而出导犹,到底是詐尸還是另有隱情,我是刑警寧澤陌宿,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布锡足,位于F島的核電站,受9級(jí)特大地震影響壳坪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜掰烟,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一爽蝴、第九天 我趴在偏房一處隱蔽的房頂上張望沐批。 院中可真熱鬧,春花似錦蝎亚、人聲如沸九孩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)躺彬。三九已至,卻和暖如春梅惯,著一層夾襖步出監(jiān)牢的瞬間宪拥,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來泰國(guó)打工铣减, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留她君,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓葫哗,卻偏偏與公主長(zhǎng)得像缔刹,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子劣针,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)校镐。 張土汪:刷leetcod...
    土汪閱讀 12,747評(píng)論 0 33
  • 花了半天的時(shí)間,把史蒂芬.柯維的《高效能人士的7個(gè)習(xí)慣》的前三章啃了下來捺典。不愧是經(jīng)典灭翔,字字珠璣。對(duì)于我這個(gè)困境中的...
    安寧姑娘閱讀 225評(píng)論 0 2
  • 你們的確
    25151ee6d9c9閱讀 173評(píng)論 0 1
  • 父親離開我們已十年有余了辣苏。 父親在世時(shí)肝箱,我與父親的感情一般,主要是他在我心里“形象”不太好稀蟋。母親由于長(zhǎng)年有病煌张,家中...
    飛翔的狐閱讀 251評(píng)論 1 2