// File: 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
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, (%rax)
jne 0xc659
movb $0x0, -0x1(%rbp)
jmp 0xc885
movq 0x1c970(%rip), %rdi     # 0x28fd0
leaq 0x10a16(%rip), %rsi     # 0x1d07d
callq 0x9660
movq %rax, %rdi
movq 0x1c932(%rip), %rsi     # 0x28fa8
callq 0x96b0
movabsq $0x3fe6666666666666, %rax # imm = 0x3FE6666666666666
movq %rax, -0x18(%rbp)
leaq -0x68(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0xdb10
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0xdb40
jmp 0xc6ae
movl $0x0, -0x78(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xdb70
movb %al, -0xb9(%rbp)
jmp 0xc6c6
movb -0xb9(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xc6d7
jmp 0xc847
leaq -0x68(%rbp), %rdi
callq 0xdb90
movq %rax, -0xc8(%rbp)
jmp 0xc6e9
movq -0xc8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xdbb0
jmp 0xc702
cmpq $0x0, -0x80(%rbp)
jne 0xc725
jmp 0xc6b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xdbd0
jmp 0xc893
movl $0x0, -0x84(%rbp)
cmpl $0x1a, -0x84(%rbp)
jge 0xc77e
movq -0x80(%rbp), %rax
movslq -0x84(%rbp), %rcx
cvtsi2sdl 0x24(%rax,%rcx,4), %xmm0
movsd 0x108c7(%rip), %xmm1    # 0x1d018
mulsd %xmm1, %xmm0
movq -0x80(%rbp), %rax
cvtsi2sdl 0x20(%rax), %xmm1
divsd %xmm1, %xmm0
ucomisd -0x18(%rbp), %xmm0
jbe 0xc76b
jmp 0xc76d
jmp 0xc76d
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xc72f
movq -0x80(%rbp), %rax
cmpl $0xc, 0x8c(%rax)
jl 0xc790
jmp 0xc6b5
movq -0x80(%rbp), %rax
cmpl $0x32, 0x20(%rax)
jge 0xc79f
jmp 0xc6b5
movq -0xb0(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0xc8a0
movq %rdx, -0xd8(%rbp)
movq %rax, -0xd0(%rbp)
jmp 0xc7bf
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 0xdb40
jmp 0xc827
leaq -0x68(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0xdb40
jmp 0xc839
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0xc6b5
movq 0x1c782(%rip), %rdi     # 0x28fd0
leaq 0x1083a(%rip), %rsi     # 0x1d08f
callq 0x9660
movq %rax, -0xe0(%rbp)
jmp 0xc863
movq -0xe0(%rbp), %rdi
movq 0x1c737(%rip), %rsi     # 0x28fa8
callq 0x96b0
jmp 0xc878
movb $0x1, -0x1(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xdbd0
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xe0, %rsp
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x9c10
nopl (%rax)