// File: /workspace/github_cpp_makefile_star2_O1/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 0x2f6b
movq %rdi, %rbx
movq 0x7235(%rip), %r14      # 0x9fd0
leaq 0x42db(%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 0x4048
movq 0x30(%r14), %rax
movq 0x40(%r14), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2e1d
movq (%rbx), %rcx
movq %rcx, (%rax)
addq $0x8, -0x50(%rbp)
jmp 0x2e29
leaq -0x80(%rbp), %rdi
movq %rbx, %rsi
callq 0x41d0
movq -0x70(%rbp), %rax
cmpq %rax, -0x50(%rbp)
je 0x2f1c
leaq -0x30(%rbp), %r12
movq (%rax), %r13
movq -0x60(%rbp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2e51
addq $0x8, %rax
jmp 0x2e7e
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 0x2f0e
cmpl $0xb, 0x8c(%r13)
jg 0x2f0e
cmpl $0x32, 0x20(%r13)
jl 0x2f0e
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2f9a
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 0x2ed5
movq %rax, (%rcx)
addq $0x8, -0x50(%rbp)
jmp 0x2ee4
movq %r14, %rdi
leaq -0x88(%rbp), %rsi
callq 0x41d0
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2f03
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
addq $0x8, -0x50(%rbp)
jmp 0x2f0e
movq %r14, %rdi
movq %r12, %rsi
callq 0x41d0
movq -0x70(%rbp), %rax
cmpq %rax, -0x50(%rbp)
jne 0x2e3b
movq 0x70ad(%rip), %rbx      # 0x9fd0
leaq 0x4165(%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 0x707b(%rip), %rdi      # 0x9fd0
callq 0x2040
movq %rax, %rdi
callq 0x2150
leaq -0x80(%rbp), %rdi
callq 0x3c74
testq %r15, %r15
setne %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2f86
jmp 0x2f86
jmp 0x2f86
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x3c74
movq %rbx, %rdi
callq 0x2350