// File: /workspace/github_cpp_makefile_star2_O3/liujan[P]MPI_multi_thread_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 >= maxDepth) //����ǰ�ڵ��ĸ߶ȴ���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 0x2fc0
movq %rdi, %rbx
movq 0x71f5(%rip), %r14      # 0x9fd0
leaq 0x429b(%rip), %rsi      # 0x707d
movl $0x11, %edx
movq %r14, %rdi
callq 0x2240
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2150
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 0x4076
movq 0x30(%r14), %rax
movq 0x40(%r14), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2e64
movq (%rbx), %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, -0x50(%rbp)
jmp 0x2e74
leaq -0x80(%rbp), %rdi
movq %rbx, %rsi
callq 0x41fe
movq -0x50(%rbp), %rcx
movq -0x70(%rbp), %rax
cmpq %rax, %rcx
je 0x2f71
leaq -0x30(%rbp), %r12
movq (%rax), %r13
movq -0x60(%rbp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2e9b
addq $0x8, %rax
jmp 0x2ec8
movq -0x68(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x2200
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 0x2f67
cmpl $0xb, 0x8c(%r13)
jg 0x2f67
cmpl $0x32, 0x20(%r13)
jl 0x2f67
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2ff0
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 0x2f2a
movq %rax, (%rcx)
movq -0x50(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x50(%rbp)
jmp 0x2f3d
movq %r14, %rdi
leaq -0x88(%rbp), %rsi
callq 0x41fe
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2f58
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
addq $0x8, -0x50(%rbp)
jmp 0x2f63
movq %r14, %rdi
movq %r12, %rsi
callq 0x41fe
movq -0x70(%rbp), %rax
cmpq %rax, -0x50(%rbp)
jne 0x2e85
movq 0x7058(%rip), %rbx      # 0x9fd0
leaq 0x4110(%rip), %rsi      # 0x708f
movl $0x17, %edx
movq %rbx, %rdi
callq 0x2240
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq 0x7026(%rip), %rdi      # 0x9fd0
callq 0x2040
movq %rax, %rdi
callq 0x2150
leaq -0x80(%rbp), %rdi
callq 0x3cd4
testq %r15, %r15
setne %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2fdb
jmp 0x2fdb
jmp 0x2fdb
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x3cd4
movq %rbx, %rdi
callq 0x2350
nop