// File: liujan[P]randomforest/Tree.cpp
// Function: Tree::buildTree()
bool Tree::buildTree(){
    
    if (root == NULL)
        return false;
    
    cout << "building tree ..." << endl;
    
    double p = 0.7;  //当一个节点里有p的样本为同一类型，则该节点不再进行分裂
    queue<Node *> q; //用于辅助非递归建树
    q.push(root);
    int sum = 0; //记录一共分裂了多少次
    while (!q.empty()) {
        Node * current = q.front();
        q.pop();
        if (current == NULL)
            continue;
        
        for (int i = 0; i < numOfTypes; i++){ //当一个节点里有p的样本为同一类型，则该节点不再进行分裂
            if ((current->num[i] * 1.0 / current->sum) > p)
                continue;
        }
        if (current->depth >= 8) //当当前节点的高度大于5时，当前节点不再进行分裂
            continue;
        
        if (current->sum < 50) //当一个节点里样本数小于50时，该节点不再进行分裂
            continue;
        
        //cout <<"depth = " <<  current->depth << " bin = " << sum << endl;
        pair<Node *, Node *> child = this->selectBestPart(current);
        Node *left = child.first;
        Node *right = child.second;
        
        current->left = left;
        current->right = right;
        
        q.push(left);
        q.push(right);
        sum++;
    }
    
    cout << "building tree completed" << endl;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, (%rax)
jne 0x4c49
movb $0x0, -0x1(%rbp)
jmp 0x4e75
movq 0x14388(%rip), %rdi     # 0x18fd8
leaq 0xb426(%rip), %rsi      # 0x1007d
callq 0x21c0
movq %rax, %rdi
movq 0x14362(%rip), %rsi     # 0x18fc8
callq 0x2200
movabsq $0x3fe6666666666666, %rax # imm = 0x3FE6666666666666
movq %rax, -0x18(%rbp)
leaq -0x68(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x6100
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x6130
jmp 0x4c9e
movl $0x0, -0x78(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x6160
movb %al, -0xb9(%rbp)
jmp 0x4cb6
movb -0xb9(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x4cc7
jmp 0x4e37
leaq -0x68(%rbp), %rdi
callq 0x6180
movq %rax, -0xc8(%rbp)
jmp 0x4cd9
movq -0xc8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x61a0
jmp 0x4cf2
cmpq $0x0, -0x80(%rbp)
jne 0x4d15
jmp 0x4ca5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x61c0
jmp 0x4e83
movl $0x0, -0x84(%rbp)
cmpl $0x1a, -0x84(%rbp)
jge 0x4d6e
movq -0x80(%rbp), %rax
movslq -0x84(%rbp), %rcx
cvtsi2sdl 0x24(%rax,%rcx,4), %xmm0
movsd 0xb2d7(%rip), %xmm1     # 0x10018
mulsd %xmm1, %xmm0
movq -0x80(%rbp), %rax
cvtsi2sdl 0x20(%rax), %xmm1
divsd %xmm1, %xmm0
ucomisd -0x18(%rbp), %xmm0
jbe 0x4d5b
jmp 0x4d5d
jmp 0x4d5d
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x4d1f
movq -0x80(%rbp), %rax
cmpl $0x8, 0x8c(%rax)
jl 0x4d80
jmp 0x4ca5
movq -0x80(%rbp), %rax
cmpl $0x32, 0x20(%rax)
jge 0x4d8f
jmp 0x4ca5
movq -0xb0(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x4e90
movq %rdx, -0xd8(%rbp)
movq %rax, -0xd0(%rbp)
jmp 0x4daf
movq -0xd8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movq %rcx, -0x98(%rbp)
movq %rax, -0x90(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0xa8(%rbp), %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x18(%rax)
leaq -0x68(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x6130
jmp 0x4e17
leaq -0x68(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x6130
jmp 0x4e29
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x4ca5
movq 0x1419a(%rip), %rdi     # 0x18fd8
leaq 0xb24a(%rip), %rsi      # 0x1008f
callq 0x21c0
movq %rax, -0xe0(%rbp)
jmp 0x4e53
movq -0xe0(%rbp), %rdi
movq 0x14167(%rip), %rsi     # 0x18fc8
callq 0x2200
jmp 0x4e68
movb $0x1, -0x1(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x61c0
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xe0, %rsp
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x2330
nopl (%rax)