// There are branches in trees, and tree B is not a subtree of tree A
//                  8                8
//              /       \           / \
//             8         7         9   2
//           /   \
//          9     3
//               / \
//              4   7
void Test2()
{
    BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(7);
    BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(9);
    BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNodeA6 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNodeA7 = CreateBinaryTreeNode(7);

    ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

    BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
    BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(2);

    ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);

    Test("Test2", pNodeA1, pNodeB1, false);

    DestroyTree(pNodeA1);
    DestroyTree(pNodeB1);
}
示例#2
0
int main(void) {
	unsigned char pkt[200];
	req r;
	ssize_t len;
	unsigned int i;
	FILE *in;
	uint32_t seed;

	signal(SIGALRM, tooslow);
	alarm(TIMEOUT);

	// set up the head of the SNMP MIB tree
	InitTree();
	SetCount = 0;
	ErrCount = 0;

	// seed the prng
	if ((in = fopen("/dev/urandom", "r")) == NULL) {
		exit(-1);
	}
	if (fread(&seed, 1, 4, in) != 4) {
		exit(-1);
	}
	fclose(in);
	srand(seed);

	// init the MIB with some objects
	PopulateMIB();

	while (1) {
		if (ErrCount > MAX_ERRORS) {
			DestroyTree(MIB);
			exit(-1);
		}
		bzero(pkt, 200);
		bzero(&r, sizeof(req));

		if ((len = ReceivePacket(pkt, 200)) == 0) {
			DestroyTree(MIB);
			exit(-1);
		}

		// reset the timer
		alarm(TIMEOUT);
		
		// parse the packet and handle the particular request
		if (ParseSnmpPkt(pkt, len, &r)) {
			if (r.type == GET_REQUEST) {
				HandleGetRequest(&r);
			} else if (r.type == GET_NEXT_REQUEST) {
				HandleGetNextRequest(&r);
			} else if (r.type == SET_REQUEST) {
				HandleSetRequest(&r);
			}
		} else {
			// error parsing packet
			ErrCount++;
		}
	}
}
// Nodes in tree A only have right children, and tree B is not a subtree of tree A
//       8                   8
//        \                   \ 
//         8                   9   
//          \                 / \
//           9               3   2
//            \      
//             2        
//              \
//               5
void Test6()
{
    BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(9);
    BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(5);

    ConnectTreeNodes(pNodeA1, NULL, pNodeA2);
    ConnectTreeNodes(pNodeA2, NULL, pNodeA3);
    ConnectTreeNodes(pNodeA3, NULL, pNodeA4);
    ConnectTreeNodes(pNodeA4, NULL, pNodeA5);

    BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
    BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNodeB4 = CreateBinaryTreeNode(2);

    ConnectTreeNodes(pNodeB1, NULL, pNodeB2);
    ConnectTreeNodes(pNodeB2, pNodeB3, pNodeB4);

    Test("Test6", pNodeA1, pNodeB1, false);

    DestroyTree(pNodeA1);
    DestroyTree(pNodeB1);
}
Status DeleteChild(CSTree &T, CSTree p, int i)
{
	CSTree b, q;
	int j;

	if (T) {
		if (i == 1) {
			b = p->firstchild;
			p->firstchild = b->nextsibling;
			b->nextsibling = NULL;
			DestroyTree(b);
		} else { q = p->firstchild;
			 j = 2;
			 while (q && j < i) {
				 q = q->nextsibling;
				 j++;
			 }
			 if (j == i) {
				 b = q->nextsibling;
				 q->nextsibling = b->nextsibling;
				 b->nextsibling = NULL;
				 DestroyTree(b);
			 } else {
				 return ERROR;
			 } }
		return OK;
	} else {
		return ERROR;
	}
}
// Nodes in trees only have left children, and tree B is a subtree of tree A
//                8                  8
//              /                   / 
//             8                   9   
//           /                    /
//          9                    2
//         /      
//        2        
//       /
//      5
void Test3()
{
    BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(9);
    BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(5);

    ConnectTreeNodes(pNodeA1, pNodeA2, NULL);
    ConnectTreeNodes(pNodeA2, pNodeA3, NULL);
    ConnectTreeNodes(pNodeA3, pNodeA4, NULL);
    ConnectTreeNodes(pNodeA4, pNodeA5, NULL);

    BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
    BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(2);

    ConnectTreeNodes(pNodeB1, pNodeB2, NULL);
    ConnectTreeNodes(pNodeB2, pNodeB3, NULL);

    Test("Test3", pNodeA1, pNodeB1, true);

    DestroyTree(pNodeA1);
    DestroyTree(pNodeB1);
}
示例#6
0
/**
 * 删除一棵树,并释放相应的结点空间
 * 注意,不要忘了加&,否则T无法释放
 */
void DestroyTree(BiTree &T)
{
	if (T) {
		DestroyTree(T->lchild);	//先删除左右子树
		DestroyTree(T->rchild);
		free(T);
		T = NULL;
	}
}
void DestroyTree(CSTree &T)
{
	if (T) {
		DestroyTree(T->firstchild);
		DestroyTree(T->nextsibling);
		free(T);
		T = NULL;
	}
}
示例#8
0
void BinaryTree::DestroyTree ( node* leaf )
{
	if ( leaf != NULL )
	{
		DestroyTree ( leaf->left );
		DestroyTree ( leaf->right );
		delete leaf;
	}
}
示例#9
0
static void
DestroyTree( BinTree T )
{
    if( T != NULL )
    {
        DestroyTree( T->LeftChild );
        DestroyTree( T->NextSibling );
        free( T );
    }
}
示例#10
0
/* This frees the memory for a binary tree of type LEAF* using recursion
   the parameter is the head of the tree. 
*/
void DestroyTree(LEAF* head) {
   if (head == NULL) {
      return;
   }
   else {
      DestroyTree(head->lchild);
      DestroyTree(head->rchild);
      free(head);
   }
}
void DestroyTree(BSTreeNode* root)
{
	if(root)
	{
		DestroyTree(root->m_pLeft);
		DestroyTree(root->m_pRight);
		delete root;
		root = 0;
	}
}
示例#12
0
void BinarySearchTree::DestroyTree( Node *node )
{
	if ( node != nullptr )
	{
		DestroyTree( node->left );
		DestroyTree( node->right );

		delete node;
		node = nullptr;
	}
}
示例#13
0
// Release the resources
void BinaryTreeMethod::DestroyTree(TreeNode **p_Root)
{
	if(!p_Root || !(*p_Root))
	{
		return;
	}
	DestroyTree(&((*p_Root)->p_left));
	DestroyTree(&((*p_Root)->p_right));
	delete *p_Root;
	p_Root = 0;
}
示例#14
0
 void DestroyTree(CSTree *T)
 { /* 初始条件: 树T存在。操作结果: 销毁树T */
   if(*T)
   {
     if((*T)->firstchild) /* T有长子 */
       DestroyTree(&(*T)->firstchild); /* 销毁T的长子为根结点的子树 */
     if((*T)->nextsibling) /* T有下一个兄弟 */
       DestroyTree(&(*T)->nextsibling); /* 销毁T的下一个兄弟为根结点的子树 */
     free(*T); /* 释放根结点 */
     *T=NULL;
   }
 }
示例#15
0
 static void DestroyTree(CSTree &T)
 { // 初始条件: 树T存在。操作结果: 销毁树T
   if(T)
   {
     if(T->firstchild) // T有长子
       DestroyTree(T->firstchild); // 销毁T的长子为根结点的子树
     if(T->nextsibling) // T有下一个兄弟
       DestroyTree(T->nextsibling); // 销毁T的下一个兄弟为根结点的子树
     free(T); // 释放根结点
     T=NULL;
   }
 }
示例#16
0
void DestroyTree(BinaryTreeNode* pRoot) {
	
	if(pRoot != NULL) {
		BinaryTreeNode* pLeft = pRoot->m_pLeft;
		BinaryTreeNode* pRight = pRoot->m_pRight;

		delete pRoot;
		pRoot = NULL;

		DestroyTree(pLeft);
		DestroyTree(pRight);
	}
}
示例#17
0
void DestroyTree(BNode** tree)
{
    if (!tree || !*tree)
    {
        return;
    }

    BNode* pTree = *tree;

    DestroyTree(&(pTree->left));
    DestroyTree(&(pTree->right));

    delete pTree;
    *tree = nullptr;
}
BOOL TestDestroyTree()
{
    PTreeNode pTreeHead = NULL;
    PTreeNode  pNode = NULL ;
    PTreeNode  pChild = NULL ;
    int sum = 0 ;
    int j = 0 ;

    pTreeHead = new TreeNode ;
    NULLVALUE_CHECK(pTreeHead, TestDestroyTree) ;
    //InitTreeNode(pTreeHead) ;

    // 添加树节点
    int i = 0 ;
    for (; i < 3; ++i)
    {
        pNode = new TreeNode ;
        //InitTreeNode(pNode) ;
        pNode->dwAddress = sum++ ;
        
        pTreeHead->list.push_back(pNode) ;

        pChild = new TreeNode ;
        //InitTreeNode(pChild) ;
        pChild->dwAddress = sum++ ;

        pNode->list.push_back(pChild) ;
    }

    DestroyTree(pTreeHead) ;
    OutputDebugString(_T("TestDestroyTree pass !\r\n")) ;
    return TRUE ;
}
示例#19
0
bool ON_Mesh::DeleteFace( int meshfi )
{
  // Do NOT add a call Compact() in this function.
  // Compact() is slow and this function may be called
  // many times in sequence.  
  // It is the callers responsibility to call Compact()
  // when it is needed.
  bool rc = false;

  if ( meshfi >= 0 && meshfi < m_F.Count() )
  {
    if ( m_top.m_topf.Count() > 0 )
    {
      DestroyTopology();
    }
    DestroyPartition();
    DestroyTree();
    if ( m_FN.Count() == m_F.Count() )
    {
      m_FN.Remove(meshfi);
    }
    m_F.Remove(meshfi);

    // 6 Mar 2010 S. Baer
    // Invalidate the cached IsClosed flag. This forces the mesh to
    // recompute IsClosed the next time it is called
    SetClosed(-1);

    rc = true;
  }
  return rc;
}
示例#20
0
文件: ipa2_3.c 项目: LukeWalsh/ipa2_3
int main(int argc, char *argv[]) {
	if (CheckArgCount(argc) == 0) {
		return EXIT_FAILURE;
	}
	/* Open the input file */
	FILE* fptr = NULL;
	if ((fptr = OpenFile(argv[1], "rb")) == NULL) {
		return EXIT_FAILURE;
	}
   /* Generate the huffman tree from the header of the file */
	LEAF* head = NULL;
	int charactersInData;
	if ((head = GenerateTree(fptr, &charactersInData)) == NULL) {	/* After this call fptr is pointing to the data */
		return EXIT_FAILURE;
	}
	/* Open the output file */
	FILE* outfptr = NULL;
	if ((outfptr = OpenFile(argv[2], "w+")) == NULL) {
		return EXIT_FAILURE;
	}
	/* Print the data to the output file */
	DecodeUsingTree(fptr, outfptr, head, charactersInData);	/* When we make this call fptr is pointing directly to the data */
	
	fclose(fptr);
	fclose(outfptr);
// 	PostOrderWalk(head);
	DestroyTree(head);
	return EXIT_SUCCESS;
}
// ====================测试代码====================
//            8
//        6      10
//       5 7    9  11
void Test1()
{
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
    BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
    BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);

    ConnectTreeNodes(pNode8, pNode6, pNode10);
    ConnectTreeNodes(pNode6, pNode5, pNode7);
    ConnectTreeNodes(pNode10, pNode9, pNode11);

    printf("====Test1 Begins: ====\n");
    printf("Expected Result is:\n");
    printf("8 \n");
    printf("10 6 \n");
    printf("5 7 9 11 \n\n");

    printf("Actual Result is: \n");
    Print(pNode8);
    printf("\n");

    DestroyTree(pNode8);
}
// There is only one node in a tree
void Test4()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    Test("Test4", pNode1);

    DestroyTree(pNode1);
}
示例#23
0
//               5
//              /
//             4
//            /
//           3
//          /
//         2
//        /
//       1
void TestB()
{
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);

    ConnectTreeNodes(pNode5, pNode4, nullptr);
    ConnectTreeNodes(pNode4, pNode3, nullptr);
    ConnectTreeNodes(pNode3, pNode2, nullptr);
    ConnectTreeNodes(pNode2, pNode1, nullptr);

    Test("TestB0", pNode5, 0, true, -1);
    Test("TestB1", pNode5, 1, false, 1);
    Test("TestB2", pNode5, 2, false, 2);
    Test("TestB3", pNode5, 3, false, 3);
    Test("TestB4", pNode5, 4, false, 4);
    Test("TestB5", pNode5, 5, false, 5);
    Test("TestB6", pNode5, 6, true, -1);

    DestroyTree(pNode5);

    printf("\n\n");
}
示例#24
0
//            8
//        6      10
//       5 7    9  11
void TestA()
{
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
    BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
    BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);

    ConnectTreeNodes(pNode8, pNode6, pNode10);
    ConnectTreeNodes(pNode6, pNode5, pNode7);
    ConnectTreeNodes(pNode10, pNode9, pNode11);

    Test("TestA0", pNode8, 0, true, -1);
    Test("TestA1", pNode8, 1, false, 5);
    Test("TestA2", pNode8, 2, false, 6);
    Test("TestA3", pNode8, 3, false, 7);
    Test("TestA4", pNode8, 4, false, 8);
    Test("TestA5", pNode8, 5, false, 9);
    Test("TestA6", pNode8, 6, false, 10);
    Test("TestA7", pNode8, 7, false, 11);
    Test("TestA8", pNode8, 8, true, -1);

    DestroyTree(pNode8);

    printf("\n\n");
}
示例#25
0
// 树中只有1个结点
void Test6()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    Test("Test6", pNode1, true);

    DestroyTree(pNode1);
}
示例#26
0
// ====================测试代码====================
// 测试完全二叉树:除了叶子节点,其他节点都有两个子节点
//            8
//        6      10
//       5 7    9  11
void Test1()
{
    printf("=====Test1 starts:=====\n");
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
    BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
    BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);

    ConnectTreeNodes(pNode8, pNode6, pNode10);
    ConnectTreeNodes(pNode6, pNode5, pNode7);
    ConnectTreeNodes(pNode10, pNode9, pNode11);

    PrintTree(pNode8);

    printf("=====Test1: MirrorRecursively=====\n");
    MirrorRecursively(pNode8);
    PrintTree(pNode8);

    printf("=====Test1: MirrorIteratively=====\n");
    MirrorIteratively(pNode8);
    PrintTree(pNode8);

    DestroyTree(pNode8);
}
示例#27
0
// ----------------------------------------------------------------------------
void IKSolver::SetAlgorithm(IKSolver::Algorithm algorithm)
{
    algorithm_ = algorithm;

    /* We need to rebuild the tree so make sure that the scene is in the
     * initial pose when this occurs.*/
    if (node_ != nullptr)
        ApplyOriginalPoseToScene();

    // Initial flags for when there is no solver to destroy
    uint8_t initialFlags = 0;

    // Destroys the tree and the solver
    if (solver_ != nullptr)
    {
        initialFlags = solver_->flags;
        DestroyTree();
        ik_solver_destroy(solver_);
    }

    switch (algorithm_)
    {
        case ONE_BONE : solver_ = ik_solver_create(SOLVER_ONE_BONE); break;
        case TWO_BONE : solver_ = ik_solver_create(SOLVER_TWO_BONE); break;
        case FABRIK   : solver_ = ik_solver_create(SOLVER_FABRIK);   break;
        /*case MSD      : solver_ = ik_solver_create(SOLVER_MSD);      break;*/
    }

    solver_->flags = initialFlags;

    if (node_ != nullptr)
        RebuildTree();
}
示例#28
0
// 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点
//            8
//          7   
//        6 
//      5
//    4
void Test2()
{
    printf("=====Test2 starts:=====\n");
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);

    ConnectTreeNodes(pNode8, pNode7, NULL);
    ConnectTreeNodes(pNode7, pNode6, NULL);
    ConnectTreeNodes(pNode6, pNode5, NULL);
    ConnectTreeNodes(pNode5, pNode4, NULL);

    PrintTree(pNode8);

    printf("=====Test2: MirrorRecursively=====\n");
    MirrorRecursively(pNode8);
    PrintTree(pNode8);

    printf("=====Test2: MirrorIteratively=====\n");
    MirrorIteratively(pNode8);
    PrintTree(pNode8);

    DestroyTree(pNode8);
}
示例#29
0
int main(int argc, char **argv)
{
	CSTree T;
	TElemType e;
	TElemType e1;
	InitTree(&T);
	printf("构造空树后,树空否? %d(1:是 0:否) 树根为%c 树的深度为%d\n",\
			TreeEmpty(T), Root(T), TreeDepth(T));
	CreateTree(&T);
	printf("构造空树后,树空否? %d(1:是 0:否) 树根为%c 树的深度为%d\n",\
				TreeEmpty(T), Root(T), TreeDepth(T));
	printf("先根遍历树T:\n");
	PreOrderTraverse(T, vi);
	printf("\n请输入等修改的结点的值 新值:");
	scanf("%c%*c%c%*c", &e, &e1);
	Assign(&T, e, e1);
	printf("后根遍历修改后的树T:\n");
	PostOrderTraverse_recurssion1(T, vi);
	printf("\n");
	printf("PostOrderTraverse_recurssion1 complete!\n");
	PostOrderTraverse_recurssion2(T, vi);
	printf("\n");
	printf("PostOrderTraverse_recurssion2 complete!\n");
	printf("\n%c的双亲是%c, 长子是%c,下一个兄弟是%c\n", e1, Parent(T, e1),\
			LeftChild(T, e1), RightSibling(T, e1));
	printf("层序遍历树:\n");
	LevelOrderTraverse(T, vi);
	DestroyTree(&T);
	return EXIT_SUCCESS;
}
示例#30
0
// 1
//  \
//   2
//    \
//     3
//      \
//       4
//        \
//         5
void TestC()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

    ConnectTreeNodes(pNode1, nullptr, pNode2);
    ConnectTreeNodes(pNode2, nullptr, pNode3);
    ConnectTreeNodes(pNode3, nullptr, pNode4);
    ConnectTreeNodes(pNode4, nullptr, pNode5);

    Test("TestC0", pNode1, 0, true, -1);
    Test("TestC1", pNode1, 1, false, 1);
    Test("TestC2", pNode1, 2, false, 2);
    Test("TestC3", pNode1, 3, false, 3);
    Test("TestC4", pNode1, 4, false, 4);
    Test("TestC5", pNode1, 5, false, 5);
    Test("TestC6", pNode1, 6, true, -1);

    DestroyTree(pNode1);

    printf("\n\n");
}