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

时间:2016-05-24 16:34:32

标签: javascript algorithm data-structures

我是Javascript的新手。我想知道如何创建具有指定深度的完整二叉树。目前,我创建了一个名为generateTree的函数,它将变量depth作为参数。预期结果如下:

  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));`

2 个答案:

答案 0 :(得分:0)

我还没有研究整个代码,但可以肯定的是:

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;
    } 
}

答案 1 :(得分:0)

可以使用2个概念创建完整的二叉树/完整的二叉树

  • 找到节点
  • 使用2 * i + 1、2 * i + 2计算节点的左右子节点
$unit = SELECT * FROM allunit;

$enroll = SELECT * FROM enrollment;

$row_enroll = mysqli_fetch_assoc($enroll);

while($row = mysqli_fetch_assoc($unit)) {
    if($row['id']==$row_enroll['unit_id']){
        $button = 'Already enrolled';
    }else{
        $button = 'Enroll';
    }
?>

<tr>
     <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['unit_name']; ?></td>

           <td><?php echo $button; ?></td>
</tr>
<?php } ?>