Exemplo n.º 1
0
Arquivo: AVL.c Projeto: Arch23/AVL
int branchHeight(Node *N){
   if(N == NULL){
      return(0);
   }else{
      int right = branchHeight(N->r),left = branchHeight(N->l);
      if(right > left){
         return(++right);
      }else{
         return(++left);
      }
   }
}
Exemplo n.º 2
0
void SearchTree::Insert(TreeNode *&curNode, char * key)
{
	//wrapped in try catch jic
	try {
		if (curNode == NULL)
		{
			curNode = new TreeNode(key);
		}
		//Compares ASCII values?
		else if (&key < &curNode->data) {
			//Recursively goes through
			Insert(curNode->leftNode, key);

			//checks for th e max range
			if (branchHeight(curNode->leftNode) - branchHeight(curNode->rightNode) == 2) {
				TreeNode * temp = curNode->leftNode;
				if (&key < &temp->data) {
					rotateLeft(curNode);
				}
				else {
					rotateRight(curNode->leftNode);
					rotateLeft(curNode);
				}

			}
		}
		else if(&key > &curNode->data) {
			Insert(curNode->rightNode, key);

				//checks for the max range
			if (branchHeight(curNode->rightNode) - branchHeight(curNode->leftNode) == 2) {
				TreeNode * temp = curNode->rightNode;
				if (key < temp->data) {
					rotateRight(curNode);

				}
				else {
					rotateLeft(curNode->rightNode);
					rotateRight(curNode);
				}

			}

		}
	}
	catch (...){}
	
}
Exemplo n.º 3
0
int SearchTree::branchHeight(TreeNode *&curNode) {
	if (curNode != NULL) {
		int leftBranch;
		int rightBranch;
		leftBranch = branchHeight(curNode->leftNode);
		rightBranch = branchHeight(curNode->rightNode);

		if (leftBranch > rightBranch)
			return leftBranch + 1;
		else
			return rightBranch + 1;
	}
	else {
		return 0;
	}
}
Exemplo n.º 4
0
void SearchTree::printRoot(TreeNode *&curNode, int nest, bool isLeft) {
	string line;
	if (curNode != NULL) {
		string data;
		string symbol;
		for (int i = 0; i < nest; i++) {
			line += "   ";
		}

		if (isLeft) { 
			symbol = "L";
		} else {
			symbol = "R";
		}

		data = curNode->data;
		cout << line << symbol << "---" << data << " (" << branchHeight(curNode) << ")" << endl;
		printRoot(curNode->leftNode, nest+1, true);
		printRoot(curNode->rightNode, nest+1, false);
	}
}
Exemplo n.º 5
0
Arquivo: AVL.c Projeto: Arch23/AVL
int balanceFactor(Node *N){
   return(branchHeight(N->l)-branchHeight(N->r));
}