示例#1
0
int main() {
	BTNode *bt, *p;
	int n;
	scanf("%d", &n);
	initBT(&bt, n);

	printf("************preorder***********\n");
	preorder(bt);
	printf("\n");

	printf("************inorder************\n");
	inorder(bt);
	printf("\n");

	printf("*************postorder*********\n");
	postorder(bt);
	printf("\n");

	printf("*************level*************\n");
	level(bt);
	printf("\n");

	printf("maxNode: %d\n", maxNode(bt));
	printf("countNodes: %d\n", countNodes(bt));
	printf("getDepth: %d\n", getDepth(bt));
	p = buildBT(pre, in, 0, pre_index - 1, 0, in_index - 1);

	printf("*********************************\n");
	level(p);
	printf("\n");
	return 0;
}
示例#2
0
文件: avl.cpp 项目: possientis/Prog
// checks BST invariant
static bool checkBSTNode(const AVLNode *node, Comparator comp){

  if(node == nullptr) return true;  // empty tree satisfies invariant

  if(!checkBSTNode(node->left(), comp)) return false; // failure left

  if(!checkBSTNode(node->right(), comp)) return false;// failure right

  if(node->left() != nullptr){  // left child exists

    const void* key = maxNode(node->left())->key(); // maximal key in left child

    if(comp(key,node->key()) >= 0){ // left maximal key is not smaller

      return false; // binary search tree property is violated

    }

  }

  if(node->right() != nullptr){ // right child exists

    const void* key = minNode(node->right())->key(); // minimal key in right child

    if(comp(node->key(),key) >= 0){ // right minimal key is not greater

      return false; // binary search tree property is violated

    }

  }

  return true;  // all tests were successful

}
示例#3
0
int minNode(char * board)		//player2
{

	char evaluation = -INF;
	if (is_winner(board, PLAYER_2))
		evaluation = -1;
	else if (is_full(board))
		evaluation = 0;
	else
	{
		//µ±Ç°½áµã¹ÀÖµ
		char temp;
		for (int i = 0; i < BOARD; i++)
		{
			if (board[i] == 0)
			{
				board[i] = PLAYER_1;
				temp = maxNode(board);
				if (temp > evaluation)
					evaluation = temp;	//×îС¹ÀÖµ
				board[i] = 0;		//»Ö¸´µ±Ç°×´Ì¬
			}
		}
	}
#if DEBUG
	printBoard(board);
	printf("%d\n", evaluation);
#endif
	return evaluation;
}
示例#4
0
文件: binarytree.c 项目: bobjoris/IPA
int maxNode(BinaryTree* tree)
{
	int res;
	if(rightChild(tree)!=NULL)
	{
		res=maxNode(rightChild(tree));//la valeur maximale n'st pas encore atteinte, donc on fait un appel recursif
	}
	else
	{
		res=nodeValue(tree);//on a atteint la valeur max	
	}
	return res;
}
示例#5
0
文件: avl.cpp 项目: possientis/Prog
const void* AVL::max(const void* *key_p) const{

  assert(key_p != nullptr);

  AVLNode *temp = maxNode(d_top_p);  // node with maximal key

  if(temp == nullptr) return nullptr; // tree is empty

  *key_p = temp->key();  // returning maximal key

  return temp->val(); // returning corresponding value pointer

}
示例#6
0
文件: afr.cpp 项目: BOPOHOB/NPO
double AFR::damping(const FrequencyMagnitude& maxValue) const
{
#ifndef SQRT2
    const double rimAmplitude(abs(maxValue.amplitude) / sqrt(2.0));
#else
    const double rimAmplitude(abs(maxValue.amplitude) / SQRT2);
#endif
    if (this->empty()) {
        return std::numeric_limits<double>::quiet_NaN();
    }
    const double& maxFreq = maxValue.frequency;
    RealRange freq;
    typedef CRange<const_iterator> DampingRange;
    const_iterator maxNode(begin());
    while (maxNode->frequency < maxValue.frequency && maxNode < end()) {
        ++maxNode;
    }
    DampingRange range(maxNode);
    while (range.getMin() > begin() && abs(range.getMin()->amplitude) >= rimAmplitude) {
        --range.first;
    }
    while (range.getMax() < end() && abs(range.getMax()->amplitude) >= rimAmplitude) {
        ++range.second;
    }
    if (range.getMax() == end()) {
        --range.second;
    }
    if (range.getMin() >= range.getMax() ||
            range.getMax() <= begin() || range.getMax() >= end() ||
            range.getMin() < begin() || (range.getMin() + 1) >= end()) {
        return std::numeric_limits<double>::quiet_NaN();
    }
    //The frequency is interpolated between the two points. The expression is derived from the similarity of triangles
    //nahu'ya?
    double deltaFreq = (range.getMin() + 1)->frequency - range.getMin()->frequency;
    double deltaAmplitudeSmall = rimAmplitude - abs(range.getMin()->amplitude);
    double deltaAmplitude = abs((range.getMin() + 1)->amplitude) - abs(range.getMin()->amplitude);
    freq.setMin(range.getMin()->frequency + deltaAmplitudeSmall / deltaAmplitude * deltaFreq);
    deltaFreq = range.getMax()->frequency - (range.getMax() - 1)->frequency;
    deltaAmplitudeSmall = rimAmplitude - abs(range.getMax()->amplitude);
    deltaAmplitude = abs((range.getMax() - 1)->amplitude) - abs(range.getMax()->amplitude);
    freq.setMax(range.getMax()->frequency - deltaAmplitudeSmall / deltaAmplitude * deltaFreq);
    Q_ASSERT(freq.range() > 0);

    return freq.range() / maxFreq;
}
示例#7
0
文件: avl.cpp 项目: possientis/Prog
static AVLNode *predNode(AVLNode *node, const void* key, Comparator comp){

  if(node == nullptr) return nullptr; // key has no predecessor in tree

  if(comp(node->key(),key) < 0){ // key is to the right
    AVLNode *temp = predNode(node->right(),key,comp);  // looking right
    if(temp == nullptr)
    {  // there is no predecessor of key in right child
      return node;  // current node has predecessor key
    }
    else
    {
      return temp;  // predecessor of key in right child is predecessor key
    }
  }

  if(comp(key,node->key()) < 0){ // key is to the left, if predecessor exists ..
    return predNode(node->left(),key,comp); // ... it must be on the left
  }

  // both < and > have failed, key is equal to key of current node
  if (node->left() == nullptr)
  {  // left child does not exist
    AVLNode *parent_p = node->parent();  // predecessor possibly parent
    if(parent_p == nullptr){  // if no parent then no predecessor
      return nullptr;
    }
    if(comp(parent_p->key(), key) < 0)
    {  // parent key smaller, it is predecessor
      return parent_p;
    }
    else
    {  // parent key cannot be equal, hence it is greater and no predecessor
      return nullptr;
    }
  } // left child does not exist
  else
  { // left child does exist
    return maxNode(node->left());  // predecessor is max of left child
  }
}
示例#8
0
文件: binarytree.c 项目: bobjoris/IPA
BinaryTree* deleteNode2(int val, BinaryTree *tree)
{
	BinaryTree *res=NULL;
	res = malloc(sizeof(BinaryTree));
	if(haveNode(val,tree))//condition : le noeud est present
	{	
		if(nodeValue(tree)==val)// on se deplace dans le branches de l'arbre
		{
			// on supprime le noeud val dans l'arbre ou a été échangé val et le noeud de valeur maximale dans le sous arbre gauche
			res=deleteNode1(val,switchNode(val,maxNode(leftChild(tree)),tree));	
		}
		else// appel recursif
		{
			res=createNodeWithChilds(deleteNode2(val,leftChild(tree)),nodeValue(tree),deleteNode2(val,rightChild(tree)));
		}
	}
	else
	{
		res=tree;
	}
	return res;
}
示例#9
0
文件: avl.cpp 项目: possientis/Prog
static AVLNode *maxNode(AVLNode *node){

  if(node == nullptr) return nullptr;
  if(node->right() == nullptr) return node; // no right child, node is max
  return maxNode(node->right());  // maximal key in right child
}
示例#10
0
int TicTacToeAI(char * stateBoard, int player)		//play1:max player2:min
{
	//Minmax
	char probablyWin[BOARD];
	char probablyWinEvaluation[BOARD];
	int win[BOARD];
	int index;
	int probablyWin_cnt = 0;
	int win_cnt = 0;
	char evaluation;
	if (player == PLAYER_1)
	{
		evaluation = -INF;		//µ±Ç°½áµã¹ÀÖµ
		char temp;
		for (int i = 0; i < BOARD; i++)
		{
			if (stateBoard[i] == 0)
			{
				stateBoard[i] = PLAYER_1;
				temp = maxNode(stateBoard);
				if (temp >= evaluation)
				{
					index = i;			//ΨһµÄÂä×Ó·½·¨
					probablyWin[probablyWin_cnt] = i;
					probablyWinEvaluation[probablyWin_cnt++] = temp;
					evaluation = temp;	//×îС¹ÀÖµ
				}
				stateBoard[i] = 0;		//»Ö¸´µ±Ç°×´Ì¬
			}
		}
#if DEBUG
		printBoard(stateBoard);
		printf("%d\n", evaluation);
#endif
	}
	else if (player == PLAYER_2)
	{
		evaluation = INF;		//µ±Ç°½áµã¹ÀÖµ
		char temp;
		for (int i = 0; i < BOARD; i++)
		{
			if (stateBoard[i] == 0)
			{
				stateBoard[i] = PLAYER_2;
				temp = minNode(stateBoard);
				if (temp <= evaluation)
				{
					index = i;		//ΨһµÄÂä×Ó·½·¨
					probablyWin[probablyWin_cnt] = i;
					probablyWinEvaluation[probablyWin_cnt++] = temp;
					evaluation = temp;	//×îС¹ÀÖµ
				}
				stateBoard[i] = 0;		//»Ö¸´µ±Ç°×´Ì¬
			}
		}
#if DEBUG
		printBoard(stateBoard);
		printf("%d\n", evaluation);
#endif
	}
	for (int i = 0; i < probablyWin_cnt;i++)		//find the places which hava a max evaluation
	if (probablyWinEvaluation[i] == evaluation)
			win[win_cnt++] = probablyWin[i];
	srand((unsigned)time(NULL));
	int random=rand()%win_cnt;
	return win[random];		//randomly choose one place where have a probability to win
}