// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq (%rdi), %r15
testq %r15, %r15
je 0x2ef0
movq %rdi, %rbx
movq 0x72cd(%rip), %r14      # 0x9fd8
leaq 0x436b(%rip), %rsi      # 0x707d
movl $0x11, %edx
movq %r14, %rdi
callq 0x21e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %r14
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
movq %r14, %rdi
xorl %esi, %esi
callq 0x3fa6
movq 0x30(%r14), %rax
movq 0x40(%r14), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2d94
movq (%rbx), %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, -0x50(%rbp)
jmp 0x2da4
leaq -0x80(%rbp), %rdi
movq %rbx, %rsi
callq 0x412e
movq -0x50(%rbp), %rcx
movq -0x70(%rbp), %rax
cmpq %rax, %rcx
je 0x2ea1
leaq -0x30(%rbp), %r12
movq (%rax), %r13
movq -0x60(%rbp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2dcb
addq $0x8, %rax
jmp 0x2df8
movq -0x68(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x21b0
movq -0x58(%rbp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, -0x58(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
leaq 0x200(%rax), %rcx
movq %rcx, -0x60(%rbp)
movq %rax, -0x70(%rbp)
testq %r13, %r13
je 0x2e97
cmpl $0x7, 0x8c(%r13)
jg 0x2e97
cmpl $0x32, 0x20(%r13)
jl 0x2e97
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2f20
movq %rax, -0x88(%rbp)
movq %rdx, -0x30(%rbp)
movq %rax, 0x10(%r13)
movq %rdx, 0x18(%r13)
movq -0x50(%rbp), %rcx
movq -0x40(%rbp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x2e5a
movq %rax, (%rcx)
movq -0x50(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x50(%rbp)
jmp 0x2e6d
movq %r14, %rdi
leaq -0x88(%rbp), %rsi
callq 0x412e
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2e88
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
addq $0x8, -0x50(%rbp)
jmp 0x2e93
movq %r14, %rdi
movq %r12, %rsi
callq 0x412e
movq -0x70(%rbp), %rax
cmpq %rax, -0x50(%rbp)
jne 0x2db5
movq 0x7130(%rip), %rbx      # 0x9fd8
leaq 0x41e0(%rip), %rsi      # 0x708f
movl $0x17, %edx
movq %rbx, %rdi
callq 0x21e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x70fe(%rip), %rdi      # 0x9fd8
callq 0x2030
movq %rax, %rdi
callq 0x2120
leaq -0x80(%rbp), %rdi
callq 0x3c04
testq %r15, %r15
setne %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2f0b
jmp 0x2f0b
jmp 0x2f0b
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x3c04
movq %rbx, %rdi
callq 0x2290
nop