Пример #1
0
bool IsSubTree(SearchTree treea, SearchTree treeb) {
	bool result = false;
	if (treea && treeb) {
		if (treea->element == treeb->element)
			result = SubTreeCore(treea, treeb);
		if (!result)
			result = IsSubTree(treea->left, treeb);
		if (!result)
			result = IsSubTree(treea->right, treeb);
	}
	return result;
}
Пример #2
0
void testProblems() {
	BinTree r, s;
	freopen("input/BinTree1.txt", "r", stdin);
	r = Create_BinTree();
	s = Create_BinTree();
	printf("%d\n", IsSubTree(r, s));
	freopen("input/BinTree2.txt", "r", stdin);
	int res = 0;
	r = Create_BinTree();
	MaxPathSum(r, res);
	printf("%d\n", res);
	printf("%d\n", MinDepth(r));
	printf("%d\n", MaxDepth(r));
	printf("%d\n", IsAvlTree(r));
	printf("%d\n", IsSymmetricTree(r));
	freopen("input/BinTree4.txt", "r", stdin);
	r = Create_BinTree();
	PreOrder(r);
	printf("\n");
	MirrorTree(r);
	PreOrder(r);
	printf("\n");
	freopen("input/BinTree3.txt", "r", stdin);
	r = Create_BinTree();
	BinNode *p = LastCommonParent(r, 10, 13);
	if (p) {
		printf("%d\n", ToInt(p));
	}
    int node = findSucc(r,6);
    printf("%d\n", node);
}
Пример #3
0
int main()
{
	#if 1
	AddNode(&pRoot,4);
	AddNode(&pRoot,6);
	AddNode(&pRoot,2);
	AddNode(&pRoot,3);
	AddNode(&pRoot,5);
	AddNode(&pRoot,7);
	AddNode(&pRoot,1);
	
	//SubTree

	pRootSub = getbtNode(2);
	pRootSub->left = getbtNode(1);
	pRootSub->right = getbtNode(3);//true subtree case
	//pRootSub->right = getbtNode(4);//fasle subtree case
	#else//for Sum Tree 
	pRoot = getbtNode(30);
	pRoot->left = getbtNode(10);
	pRoot->right = getbtNode(5);
	pRoot->left->left = getbtNode(6);
	pRoot->left->right = getbtNode(4);
	pRoot->right->right = getbtNode(3);
	pRoot->right->left = getbtNode(2);
		
	#endif
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<"Inorder Traversal ::"<<endl;
	Inorder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<"Preorder Traversal ::"<<endl;
	
	Preorder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<"Postorder Traversal ::"<<endl;
	Postorder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<"Level Order Traversal ::"<<endl;
	LOrder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<"Size of the Tree ::"<<endl;
	cout<<GetTreeSize(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<"Height of the Tree ::"<<endl;
	cout<<GetTreeHeight(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	#if 0
	DeleteTree(&pRoot);
	
	AddNode(&pRoot,1);
	
	cout<<"Inorder Traversal after Deletion::"<<endl;
	Inorder(pRoot);
	cout<<endl;
	#endif
		
	MirrorTree(pRoot);	
	cout<<"Inorder Traversal after MirrorTree operation::"<<endl;
	Inorder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	MirrorTree(pRoot);
	cout<<"Inorder Traversal after MirrorTree operation for second time::"<<endl;
	Inorder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<"Max Element of the Tree ::"<<endl;
	cout<<MaxTreeElement(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	int path[7];
	cout<<"Root To Leaf Paths ::"<<endl;
	RootToLeafPath(pRoot,path,0);
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<"Number of Leaf Nodes::"<<NumberOfLeafNodes(pRoot)<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<"Ancestors of 7 are ::"<<endl;
	GetAncestors(pRoot,7);
	cout<<endl;

	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<"CheckSumTree ::"<<IsSumTree(pRoot)<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<endl<<"Lowest Common Ancestor of 1 and 3 ::"<<LCA(pRoot,1,3)->data<<endl;
	cout<<endl<<"Lowest Common Ancestor of 1 and 7 ::"<<LCA(pRoot,1,7)->data<<endl;
	cout<<endl<<"Lowest Common Ancestor of 4 and 6 ::"<<LCA(pRoot,4,6)->data<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<endl<<"Tree has Path Sum 9 ::"<<hasPathSum(pRoot,9)<<endl;
	cout<<endl<<"Tree has Path Sum 100 ::"<<hasPathSum(pRoot,100)<<endl;
	cout<<endl<<"Tree has Path Sum 15 ::"<<hasPathSum(pRoot,15)<<endl;
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<endl<<"Subtree ::"<<IsSubTree(pRoot,pRootSub)<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	return 0;
}