// File: ambersun1234[P]AVL_Tree/src/avl.h
// Function: AVLTree<int>::remove(node<int>*, int)
node< itemType >* AVLTree< itemType >::remove( node< itemType > *root , itemType target ) {
	if ( root == nullptr ) return nullptr;

	if ( root->getData() == target ) { // target found
		if ( root->isLeaf() ) { // no children
			delete root;
			root = nullptr;
			return root;
		}
		else {
			itemType returnValue;
			node< itemType > *temp = this->findSmallestNodeHelper( root , returnValue );

			if ( temp == nullptr ) { // no smallest node found , but may have right children
				return root->getRightChild();
			}
			else { // smallest children found
				root->setLeftChild( temp );
				root->setData( returnValue );
				if ( root->unbalanced() ) this->doRotation( root );
				return root;
			}
		}
	}

	else if ( root->getData() > target ) root->setLeftChild( this->remove( root->getLeftChild() , target ) );
	else if ( root->getData() < target ) root->setRightChild( this->remove( root->getRightChild() , target ) );

	if ( root->unbalanced() ) this->doRotation( root );
	root->setHeight( root->getMaxHeight() );
	return root;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x354f
movq $0x0, -0x8(%rbp)
jmp 0x36cb
movq -0x18(%rbp), %rdi
callq 0x2ba0
cmpl -0x1c(%rbp), %eax
jne 0x3611
movq -0x18(%rbp), %rdi
callq 0x36e0
testb $0x1, %al
jne 0x3570
jmp 0x35a1
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0x358c
movq -0x38(%rbp), %rdi
movl $0x20, %esi
callq 0x20f0
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x36cb
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x20(%rbp), %rdx
callq 0x3720
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x35cf
movq -0x18(%rbp), %rdi
callq 0x2f60
movq %rax, -0x8(%rbp)
jmp 0x36cb
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x30d0
movq -0x18(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x3800
movq -0x18(%rbp), %rdi
callq 0x3110
testb $0x1, %al
jne 0x35f7
jmp 0x3604
movq -0x30(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x3160
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x36cb
movq -0x18(%rbp), %rdi
callq 0x2ba0
cmpl -0x1c(%rbp), %eax
jle 0x364d
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2f40
movq -0x30(%rbp), %rdi
movq %rax, %rsi
movl -0x1c(%rbp), %edx
callq 0x3520
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x30d0
jmp 0x3689
movq -0x18(%rbp), %rdi
callq 0x2ba0
cmpl -0x1c(%rbp), %eax
jge 0x3687
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2f60
movq -0x30(%rbp), %rdi
movq %rax, %rsi
movl -0x1c(%rbp), %edx
callq 0x3520
movq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x30f0
jmp 0x3689
jmp 0x368b
movq -0x18(%rbp), %rdi
callq 0x3110
testb $0x1, %al
jne 0x369a
jmp 0x36a7
movq -0x30(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x3160
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3330
movq -0x50(%rbp), %rdi
movl %eax, %esi
callq 0x3310
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop