Exemple #1
0
Fichier : AVL.c Projet : 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);
      }
   }
}
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 (...){}
	
}
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;
	}
}
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);
	}
}
Exemple #5
0
Fichier : AVL.c Projet : Arch23/AVL
int balanceFactor(Node *N){
   return(branchHeight(N->l)-branchHeight(N->r));
}