簡體   English   中英

如何在Javascript中創建具有指定深度的完整二叉樹

[英]How to Create a Full Binary Tree with Specified Depth in Javascript

我是 Javascript 新手。 我想知道如何創建具有指定深度的完整二叉樹。 目前,我創建了一個名為 generateTree 的函數,它以可變深度作為參數。 的預期結果應該如下。

  Depth = 3
     2
  3     6
 7 2   1 10

但是,它不會在控制台中顯示任何信息。

任何建議都會有所幫助。 謝謝你=)

/**
 * Definition of Node
**/
function Node(value, left, right) {
    this.value = value;
    this.left = left;
    this.right = right;
    this.show = show;
}

/**
 * Output data
**/
function show() {
    return this.value;
}

/**
 * Binary Tree constructor
**/
function BinaryTree() {
    this.root = null; 
    this.insert = insert; 
    this.inOrder = inOrder;
    this.generateTree = generateTree;
}

/**
 * Insert data
**/
function insert(value) { 
    var node = new Node(value, null, null); 

    if (this.root == null){ 
        this.root = node; 
    } else { 
        var current = this.root;
        var parent; 

        while(true) { 
            parent = current; 
            if(value <= current.value) { 
                current = current.left;

                if (current == null) { 
                    parent.left = node; 
                    break; 
                } 
            } else { 
                current = current.right; 

                if(current == null) { 
                    parent.right = node; 
                    break; 
                } 
            } 
        } 
    } 
}

/**
 * Inorder travesal
**/
function inOrder(node) { 
    if(!(node == null)) {
        inOrder(node.left); 
        console.log(node.show() + " "); 
        inOrder(node.right); 
    } 
}

// generate a tree with specified depth
function generateTree(depth) {
    var OPERATOR = ['+', '-', '*', '/'];
    var OPERAND = Math.floor(Math.random() * 100);

    if(depth > 1) {
        var operator = OPERATOR[Math.floor(Math.random() * OPERATOR.length)];
        var node = Node(operator, generateTree(depth - 1), generateTree(depth - 1));
        return node;
    } else {
        var node = new Node(OPERAND + 1);
        return node;
    }
}

/**
 * Main
**/
var tree = new BinaryTree();
inOrder(generateTree(3));`

我還沒有研究整個代碼,但可以肯定的是:

function Node(value, left, right) {
    this.value = value;
    this.left = left;
    this.right = right;
    this.show = show;
}

function show() {
    return this.value;
}

必須重構為

function Node(value, left, right) {
    this.value = value;
    this.left = left;
    this.right = right;
    this.show = show;

    function show() {
        return this.value;
    } 
}

完整二叉樹/完整二叉樹可以使用2個概念創建

  • 找到節點
  • 使用 2*i+1, 2*i+2 計算節點的左右子節點

    class Node {
        constructor(data, left, right) {
            this.data = data;
            this.left = left;
            this.right = right;
        }
    }
    class BinaryTree {
        constructor() {
            this.root = null
            this.storage = [];
        }

        find = (data, root) => {
            if (root) {
                this.find(data, root.left);
                if (root.data == data) {
                    this.storage.push(root);
                }
                this.find(data, root.right);
            }
        }

        insert = (data) => {
            if (!this.root) {
                this.root = new Node(data[0]);
            }
            for (let i = 0; i < data.length; i++) {
               this.find(data[i], this.root);
               let parent = this.storage.pop();
               parent.left = new Node(data[2 * i + 1]);
               parent.right = new Node(data[2 * i + 2]);
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM