Ejemplo n.º 1
0
Archivo: 39-2.c Proyecto: bbchung/offer
bool IsBalanced(Node *root, int *height)
{
    if (root == NULL)
        return true;

    int leftHeight = 0;
    int rightHeight = 0;
    bool leftBalance = true;
    bool rightBalance = true;
    if (root->left != NULL)
        leftBalance = IsBalanced(root->left, &leftHeight);

    if (root->right)
        rightBalance = IsBalanced(root->right, &rightHeight);

    *height = (leftHeight >= rightHeight) ? leftHeight + 1 : rightHeight + 1;

    if (!leftBalance || !rightBalance)
        return false;

    int diff = leftHeight - rightHeight;

    if (-1 <= diff && diff <= 1)
        return true;

    return false;
}
Ejemplo n.º 2
0
bool BinTree::IsBalanced(TreeNode *pHead) {
    if (pHead == NULL)
        return false;
    int left = depth(pHead ->m_pLeft);
    int right = depth(pHead ->m_pRight);
    int diff = right - left;
    if (diff > 1 || diff < -1)
        return false;
    return IsBalanced(pHead->m_pLeft) && IsBalanced(pHead->m_pRight);
}
Ejemplo n.º 3
0
 //判断某子树是否平衡二叉树,并更新树高
 bool IsBalanced(TreeNode* node, int &depth) {
     if (node == NULL)
         return true;
     int left = 0, right = 0;
     if (IsBalanced(node->left, left) && IsBalanced(node->right, right)) {
         int diff = left - right;
         if (abs(diff) > 1)
             return false;
         depth = (left > right ? left : right) + 1;
         return true;
     }
     return false;
 }
Ejemplo n.º 4
0
bool IsBalanced(Node *t, int &height)
{
	if (t == NULL)
	{
		height = 0;
		return true;
	}

	int h1 = 0, h2 = 0;
	bool stage = IsBalanced(t->left, h1) && IsBalanced(t->right, h2);
	height = (h1 >= h2 ? h1 : h2) + 1;
	return stage && (h1 - h2 >= -1 && h1 - h2 <= 1);
}
//ÅжÏÊÇ·ñΪƽºâ¶þ²æÊ÷
 bool IsBalanced(BinaryTreeNode* pRoot){
	 if(pRoot == NULL)
		 return true;
	 
	 int left = TreeDepth(pRoot->left);
	 int right = TreeDepth(pRoot->right);
	 int diff = left - right;
	 if(diff > 1 || diff < -1)
		 return false;
	 
	 return IsBalanced(pRoot->left) && IsBalanced(pRoot->right);
	 
	 
 }
Ejemplo n.º 6
0
void Test1(BinarySearchTree<int> &tree) {
    tree.Print();
    if (!IsBalanced(tree.GetRoot()))
        printf("Not balanced\n");
    else
        printf("Balanced\n");
}
int MyBST<T>::IsBalanced(typename BinarySearchTree<T>::Node * n)
{
    if (n == nullptr)
        return 0;
    
    int left_depth = IsBalanced(n->left);
    if (left_depth == -1)
        return -1;
    int right_depth = IsBalanced(n->right);
    if (right_depth == -1)
        return -1;
    
    int depth_diff = left_depth - right_depth;
    if (depth_diff > 1 || depth_diff < -1)
        return -1;

    return std::max(left_depth, right_depth) + 1;
}
int main()
{
	BTree pTree = create_tree();
	if(IsBalanced(pTree))
		printf("Balanced\n");
	else
		printf("Not Balanced\n");

	return 0;
}
Ejemplo n.º 9
0
	bool IsBalanced(TreeNode* root, int& depth) {
		if (!root) {
			depth = 0;
			return true;
		}

		int left, right;

		if (IsBalanced(root->left, left) && IsBalanced(root->right, right)) {
			int diff = left - right;
			if (std::abs(diff) <= 1) {
				depth = 1 + (left > right ? left : right);
				return true;
			}
			else
				return false;
		}
		return false;
	}
/*
ºóÐøµÝ¹é±éÀúÅж϶þ²æÊ÷ÊÇ·ñƽºâ
*/
bool IsBalanced(BTree pTree,int *depth)
{
	if(pTree == NULL)
	{
		*depth = 0;
		return true;
	}

	int leftDepth,rightDepth;
	if(IsBalanced(pTree->pLchild,&leftDepth) && IsBalanced(pTree->pRchild,&rightDepth))
	{
		int diff = leftDepth-rightDepth;
		if(diff<=1 && diff>=-1)
		{
			*depth = (leftDepth>rightDepth ? leftDepth:rightDepth) + 1;
			return true;
		}
	}
	return false;
}
Ejemplo n.º 11
0
int _tmain(int argc, _TCHAR* argv[])
{

	Node *root = new Node();
	printf("%s\n", IsBalanced(root) ? "true" : "false");



	root->left = new Node();
	printf("%s\n", IsBalanced(root) ? "true" : "false");
	root->right = new Node();


	root->left->left = new Node();
	printf("%s\n", IsBalanced(root) ? "true" : "false");

	root->right->right = new Node();
	printf("%s\n", IsBalanced(root) ? "true" : "false");

	return 0;
}
Ejemplo n.º 12
0
bool IsBalanced(BinaryTreeNode* pRoot, int* pDepth)
{
    if(pRoot == NULL)
    {
        *pDepth = 0;
        return true;
    }
 
    int leftDepth, rightDepth;
    if(IsBalanced(pRoot->m_pLeft, &leftDepth) && IsBalanced(pRoot->m_pRight, &rightDepth))
    {
        int diff = leftDepth - rightDepth;
        if(diff <= 1 && diff >= -1)
        {
            *pDepth = 1 + (leftDepth > rightDepth ? leftDepth : rightDepth);
            return true;
        }
    }
 
    return false;
}
Ejemplo n.º 13
0
Archivo: 39-2.c Proyecto: bbchung/offer
int main()
{
    Node n1;
    n1.value = 1;
    n1.left = NULL;
    n1.right = NULL;

    Node n2;
    n2.value = 2;
    n2.left = NULL;
    n2.right = NULL;

    Node n3;
    n3.value = 3;
    n3.left = NULL;
    n3.right = NULL;

    Node n4;
    n4.value = 4;
    n4.left = NULL;
    n4.right = NULL;

    Node n5;
    n5.value = 5;
    n5.left = NULL;
    n5.right = NULL;

    Node n6;
    n6.value = 6;
    n6.left = NULL;
    n6.right = NULL;

    n1.left = &n2;
    n1.right = &n3;

    n2.left = &n4;
    n2.right = &n5;

    n5.left = &n6;

    int height = 0;
    bool balance = IsBalanced(&n1, &height);

    printf("%d\n", balance);
}
Ejemplo n.º 14
0
/*************************************************************************
* This function is the entry point of refinement
**************************************************************************/
void RefineKWay(CtrlType *ctrl, GraphType *orggraph, GraphType *graph, int nparts, float *tpwgts, float ubfactor)
{
  int i, nlevels, mustfree=0;
  GraphType *ptr;

  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->UncoarsenTmr));

  /* Compute the parameters of the coarsest graph */
  ComputeKWayPartitionParams(ctrl, graph, nparts);

  /* Take care any non-contiguity */
  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->AuxTmr1));
  if (ctrl->RType == RTYPE_KWAYRANDOM_MCONN) {
    EliminateComponents(ctrl, graph, nparts, tpwgts, 1.25);
    EliminateSubDomainEdges(ctrl, graph, nparts, tpwgts);
    EliminateComponents(ctrl, graph, nparts, tpwgts, 1.25);
  }
  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->AuxTmr1));

  /* Determine how many levels are there */
  for (ptr=graph, nlevels=0; ptr!=orggraph; ptr=ptr->finer, nlevels++); 

  for (i=0; ;i++) {
    /* PrintSubDomainGraph(graph, nparts, graph->where); */
    if (ctrl->RType == RTYPE_KWAYRANDOM_MCONN && (i == nlevels/2 || i == nlevels/2+1))
      EliminateSubDomainEdges(ctrl, graph, nparts, tpwgts);

    IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->RefTmr));

    if (2*i >= nlevels && !IsBalanced(graph->pwgts, nparts, tpwgts, 1.04*ubfactor)) {
      ComputeKWayBalanceBoundary(ctrl, graph, nparts);
      if (ctrl->RType == RTYPE_KWAYRANDOM_MCONN)
        Greedy_KWayEdgeBalanceMConn(ctrl, graph, nparts, tpwgts, ubfactor, 1); 
      else
        Greedy_KWayEdgeBalance(ctrl, graph, nparts, tpwgts, ubfactor, 1); 
      ComputeKWayBoundary(ctrl, graph, nparts);
    }

    switch (ctrl->RType) {
      case RTYPE_KWAYRANDOM:
        Random_KWayEdgeRefine(ctrl, graph, nparts, tpwgts, ubfactor, 10, 1); 
        break;
      case RTYPE_KWAYGREEDY:
        Greedy_KWayEdgeRefine(ctrl, graph, nparts, tpwgts, ubfactor, 10); 
        break;
      case RTYPE_KWAYRANDOM_MCONN:
        Random_KWayEdgeRefineMConn(ctrl, graph, nparts, tpwgts, ubfactor, 10, 1); 
        break;
    }
    IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->RefTmr));

    if (graph == orggraph)
      break;

    GKfree(&graph->gdata, LTERM);  /* Deallocate the graph related arrays */

    graph = graph->finer;

    IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->ProjectTmr));
    if (graph->vwgt == NULL) {
      graph->vwgt = idxsmalloc(graph->nvtxs, 1, "RefineKWay: graph->vwgt");
      graph->adjwgt = idxsmalloc(graph->nedges, 1, "RefineKWay: graph->adjwgt");
      mustfree = 1;
    }
    ProjectKWayPartition(ctrl, graph, nparts);
    IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->ProjectTmr));
  }

  if (!IsBalanced(graph->pwgts, nparts, tpwgts, ubfactor)) {
    ComputeKWayBalanceBoundary(ctrl, graph, nparts);
    if (ctrl->RType == RTYPE_KWAYRANDOM_MCONN) {
      Greedy_KWayEdgeBalanceMConn(ctrl, graph, nparts, tpwgts, ubfactor, 8); 
      Random_KWayEdgeRefineMConn(ctrl, graph, nparts, tpwgts, ubfactor, 10, 0); 
    }
    else {
      Greedy_KWayEdgeBalance(ctrl, graph, nparts, tpwgts, ubfactor, 8); 
      Random_KWayEdgeRefine(ctrl, graph, nparts, tpwgts, ubfactor, 10, 0); 
    }
  }

  /* Take care any trivial non-contiguity */
  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->AuxTmr2));
  EliminateComponents(ctrl, graph, nparts, tpwgts, ubfactor);
  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->AuxTmr2));

  if (mustfree) 
    GKfree(&graph->vwgt, &graph->adjwgt, LTERM);

  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->UncoarsenTmr));
}
Ejemplo n.º 15
0
/*************************************************************************
* This function is the entry point of refinement
**************************************************************************/
void RefineVolKWay(CtrlType *ctrl, GraphType *orggraph, GraphType *graph, int nparts,
                   float *tpwgts, float ubfactor)
{
  int i, nlevels;
  GraphType *ptr;

  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->UncoarsenTmr));

  /* Take care any non-contiguity */
  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->AuxTmr1));
  if (ctrl->RType == RTYPE_KWAYRANDOM_MCONN) {
    ComputeVolKWayPartitionParams(ctrl, graph, nparts);
    EliminateVolComponents(ctrl, graph, nparts, tpwgts, 1.25);
    EliminateVolSubDomainEdges(ctrl, graph, nparts, tpwgts);
    EliminateVolComponents(ctrl, graph, nparts, tpwgts, 1.25);
  }
  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->AuxTmr1));


  /* Determine how many levels are there */
  for (ptr=graph, nlevels=0; ptr!=orggraph; ptr=ptr->finer, nlevels++);

  /* Compute the parameters of the coarsest graph */
  ComputeVolKWayPartitionParams(ctrl, graph, nparts);

  for (i=0; ;i++) {
    /*PrintSubDomainGraph(graph, nparts, graph->where);*/
    MALLOC_CHECK(NULL);
    IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->RefTmr));

    if (2*i >= nlevels && !IsBalanced(graph->pwgts, nparts, tpwgts, 1.04*ubfactor)) {
      ComputeVolKWayBalanceBoundary(ctrl, graph, nparts);
      switch (ctrl->RType) {
        case RTYPE_KWAYRANDOM:
          Greedy_KWayVolBalance(ctrl, graph, nparts, tpwgts, ubfactor, 1);
          break;
        case RTYPE_KWAYRANDOM_MCONN:
          Greedy_KWayVolBalanceMConn(ctrl, graph, nparts, tpwgts, ubfactor, 1);
          break;
      }
      ComputeVolKWayBoundary(ctrl, graph, nparts);
    }

    switch (ctrl->RType) {
      case RTYPE_KWAYRANDOM:
        Random_KWayVolRefine(ctrl, graph, nparts, tpwgts, ubfactor, 10, 0);
        break;
      case RTYPE_KWAYRANDOM_MCONN:
        Random_KWayVolRefineMConn(ctrl, graph, nparts, tpwgts, ubfactor, 10, 0);
        break;
    }
    IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->RefTmr));

    if (graph == orggraph)
      break;

    GKfree(&graph->gdata, LTERM);  /* Deallocate the graph related arrays */

    graph = graph->finer;

    IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->ProjectTmr));
    ProjectVolKWayPartition(ctrl, graph, nparts);
    IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->ProjectTmr));
  }

  if (!IsBalanced(graph->pwgts, nparts, tpwgts, ubfactor)) {
    ComputeVolKWayBalanceBoundary(ctrl, graph, nparts);
    switch (ctrl->RType) {
      case RTYPE_KWAYRANDOM:
        Greedy_KWayVolBalance(ctrl, graph, nparts, tpwgts, ubfactor, 8);
        Random_KWayVolRefine(ctrl, graph, nparts, tpwgts, ubfactor, 10, 0);
        break;
      case RTYPE_KWAYRANDOM_MCONN:
        Greedy_KWayVolBalanceMConn(ctrl, graph, nparts, tpwgts, ubfactor, 8);
        Random_KWayVolRefineMConn(ctrl, graph, nparts, tpwgts, ubfactor, 10, 0);
        break;
    }
  }

  EliminateVolComponents(ctrl, graph, nparts, tpwgts, ubfactor);

  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->UncoarsenTmr));
}
Ejemplo n.º 16
0
	bool IsBalanced_Solution(TreeNode* pRoot) {
		int depth = 0;
		return IsBalanced(pRoot, depth);
	}
/*
·â×°ÆðÀ´
*/
bool IsBalanced(BTree pTree)
{
	int depth = 0;
	return IsBalanced(pTree,&depth);
}
Ejemplo n.º 18
0
bool IsBalanced(Node *t)
{
	int height = 0;
	return IsBalanced(t, height);
}
Ejemplo n.º 19
0
bool IsBalanced_Solution2(BinaryTreeNode* pRoot)
{
    int depth = 0;
    return IsBalanced(pRoot, &depth);
}
void RefineKWay(ctrl_t *ctrl, graph_t *orggraph, graph_t *graph)
{
  idx_t i, nlevels, contig=ctrl->contig;
  graph_t *ptr;

  IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_startcputimer(ctrl->UncoarsenTmr));

  /* Determine how many levels are there */
  for (ptr=graph, nlevels=0; ptr!=orggraph; ptr=ptr->finer, nlevels++); 

  /* Compute the parameters of the coarsest graph */
  ComputeKWayPartitionParams(ctrl, graph);

  /* Try to minimize the sub-domain connectivity */
  if (ctrl->minconn) 
    EliminateSubDomainEdges(ctrl, graph);
  
  /* Deal with contiguity constraints at the beginning */
  if (contig && FindPartitionInducedComponents(graph, graph->where, NULL, NULL) > ctrl->nparts) { 
    EliminateComponents(ctrl, graph);

    ComputeKWayBoundary(ctrl, graph, BNDTYPE_BALANCE);
    Greedy_KWayOptimize(ctrl, graph, 5, 0, OMODE_BALANCE); 

    ComputeKWayBoundary(ctrl, graph, BNDTYPE_REFINE);
    Greedy_KWayOptimize(ctrl, graph, ctrl->niter, 0, OMODE_REFINE); 

    ctrl->contig = 0;
  }

  /* Refine each successively finer graph */
  for (i=0; ;i++) {
    if (ctrl->minconn && i == nlevels/2) 
      EliminateSubDomainEdges(ctrl, graph);

    IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_startcputimer(ctrl->RefTmr));

    if (2*i >= nlevels && !IsBalanced(ctrl, graph, .02)) {
      ComputeKWayBoundary(ctrl, graph, BNDTYPE_BALANCE);
      Greedy_KWayOptimize(ctrl, graph, 1, 0, OMODE_BALANCE); 
      ComputeKWayBoundary(ctrl, graph, BNDTYPE_REFINE);
    }

    Greedy_KWayOptimize(ctrl, graph, ctrl->niter, 5.0, OMODE_REFINE); 

    IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_stopcputimer(ctrl->RefTmr));

    /* Deal with contiguity constraints in the middle */
    if (contig && i == nlevels/2) {
      if (FindPartitionInducedComponents(graph, graph->where, NULL, NULL) > ctrl->nparts) {
        EliminateComponents(ctrl, graph);

        if (!IsBalanced(ctrl, graph, .02)) {
          ctrl->contig = 1;
          ComputeKWayBoundary(ctrl, graph, BNDTYPE_BALANCE);
          Greedy_KWayOptimize(ctrl, graph, 5, 0, OMODE_BALANCE); 
  
          ComputeKWayBoundary(ctrl, graph, BNDTYPE_REFINE);
          Greedy_KWayOptimize(ctrl, graph, ctrl->niter, 0, OMODE_REFINE); 
          ctrl->contig = 0;
        }
      }
    }

    if (graph == orggraph)
      break;

    graph = graph->finer;

    IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_startcputimer(ctrl->ProjectTmr));
    ASSERT(graph->vwgt != NULL);

    ProjectKWayPartition(ctrl, graph);
    IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_stopcputimer(ctrl->ProjectTmr));
  }

  /* Deal with contiguity requirement at the end */
  ctrl->contig = contig;
  if (contig && FindPartitionInducedComponents(graph, graph->where, NULL, NULL) > ctrl->nparts) 
    EliminateComponents(ctrl, graph);

  if (!IsBalanced(ctrl, graph, 0.0)) {
    ComputeKWayBoundary(ctrl, graph, BNDTYPE_BALANCE);
    Greedy_KWayOptimize(ctrl, graph, 10, 0, OMODE_BALANCE); 

    ComputeKWayBoundary(ctrl, graph, BNDTYPE_REFINE);
    Greedy_KWayOptimize(ctrl, graph, ctrl->niter, 0, OMODE_REFINE); 
  }

  if (ctrl->contig) 
    ASSERT(FindPartitionInducedComponents(graph, graph->where, NULL, NULL) == ctrl->nparts);

  IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_stopcputimer(ctrl->UncoarsenTmr));
}
bool MyBST<T>::IsBalanced()
{
    return IsBalanced(BinarySearchTree<T>::root) != -1;
}