示例#1
0
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
struct TNode* root1=NULL;
int n;
scanf("%d",&n);

int i,data;

root=NewNode(1);
root->left=NewNode(2);
root->right=NewNode(3);
root->right->left=NewNode(6);
root->left->left=NewNode(4);
root->left->right=NewNode(5);
root->left->right->left=NewNode(7);
root->left->right->right=NewNode(8);


root1=NewNode(1);
root1->right=NewNode(2);
root1->left=NewNode(3);
root1->left->right=NewNode(6);
root1->right->left=NewNode(4);
root1->right->right=NewNode(5);
root1->right->right->right=NewNode(7);
root1->right->right->left=NewNode(8);



printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
printf("\n\n");
LevelOrderTraversal(root1,n);
getch();
int result=CheckForTreeIsomorphism(root,root1);
if(result){
printf("\nYes Isomorphic");
}else{
printf("\nNot isomorphic");
}
return 0;
}
示例#2
0
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
printf("\n\n");
PostOrderUsingTwoStack(root);
return 0;
}
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
int isLevelSetA=0;
int isLevelSetB=0;
int passed=0;
int setHeight=-1;
int result=CheckIfCousins(root,root->left->left,root->right->right);
if(result==1){
printf("\nCousins");
}else{
printf("\nNot cousins");
}
return 0;
}
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
printf("\nPress enter to see max size of BST in given binary tree\n\n");
int result=MaxBST(root);
getch();
printf("\nSize of MaxBST : %d",result);
return 0;
}
示例#5
0
void BTree::Print(OrderType Order) const
{
    if(Order ==InOrder)
    {
        std::cout << " InOrder" << std::endl;
        InOrderTraversal(root);
        std::cout << std::endl;
    }
    else if(Order == PostOrder) {
        std::cout << " PostOrder" << std::endl;
        PostOrderTraversal(root);
        std::cout << std::endl;

    }
    else if(Order == PreOrder) {
        std::cout << " PreOrder" << std::endl;
        PreOrderTraversal(root);
        std::cout << std::endl;

    }
    else if(Order == LevelOrder) {
        std::cout << " LevelOrder" << std::endl;
        LevelOrderTraversal(root);
        std::cout << std::endl;
    }
}
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
printf("\n\nEnter the value of sum to see any path of this tree sum up to that sum : ");
scanf("%d",&data);
int result=CheckRootToLeafPathEqualsSum(root,data);
if(result){
printf("\nPath exists...\n\n");
}else{
printf("\nPath does not exist\n\n");
}
return 0;
}
示例#7
0
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
struct TNode* head=NULL;
printf("\npress enter to extract leaf nodes\n");
ExtractLeafDLL(root,&head);
printf("\nLeaf nodes extracted\n");
getch();
printf("\nPress enter to traverse\n");
getch();
TraverseDLL(head);
getch();
return 0;
}
示例#8
0
文件: BT_SumTree.C 项目: sidpka/repo
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
int result=isSumTree(root);
if(result){
printf("\n\nSum Tree");
}else{
printf("\nNot a sum tree");
}
return 0;
}
int main(){

struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
printf("\nEnter the value of K : ");
int K;
scanf("%d",&K);
printf("\nPress enter to remove unwanted nodes\n\n");
getch();
/*
struct TNode** buffer=(struct TNode**)malloc(sizeof(struct TNode*)*n);
for(i=0;i<n;i++){
buffer[i]=(struct TNode*)malloc(sizeof(struct TNode));
}

RemoveBekarNodes(&root,buffer,K,0);
*/
int sum=0;
root=RemoveNodes(root,K,&sum);

printf("\nPress enter to traverse modified tree\n\n");
getch();
LevelOrderTraversal(root,n);
return 0;
}
示例#10
0
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert in Tree: ");
int n;
scanf("%d",&n);
int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}
printf("\nInserted... %d nodes \n",n);
getch();
/*
struct TNode* root1=NULL;
printf("\nEnter the number of elements you want to insert in SubTree: ");
int m;
scanf("%d",&m);
for(i=0;i<m;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root1,data,m);
}
printf("\nInserted... %d nodes \n",m);
getch();
*/
printf("\nPress enter to do level-order traversal of Tree : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
/*
printf("\nPress enter to do level-order traversal of SubTree : \n\n");
getch();
LevelOrderTraversal(root1,m);
getch();
*/
printf("\nPress ennter to see subtree existence ....");
getch();
struct TNode* tmpNode=(struct TNode*)malloc(sizeof(struct TNode));
tmpNode->data=10;
tmpNode->left=root->left->left;
tmpNode->right=root->left->right;
int result=CheckForSubTree(root,tmpNode);
if(result){
printf("\nIts a subtree\\n");
}else{
printf("\nNot a subtree\n\n");
}
return 0;
}
示例#11
0
文件: IdenticalT.C 项目: sidpka/repo
int main(){


struct TNode* root=NULL;
struct TNode* root1=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
printf("\nEnter data into first tree \n");
getch();
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();
printf("\nEnter data into first tree \n");
getch();
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root1,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();


printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();

printf("\nSee if identical\n");
getch();
int result=SeeIfIdentical(root,root1);
if(result==1){
printf("\nIdentical\n");
}else{
printf("\nNot identical");
}

//printf("\nSize of tree : %d",FindSize(root));
return 0;
}
示例#12
0
int main(){
printf("\nEnter the number of elements you want to insert : ");
int n;
int i,data;
scanf("%d",&n);
/*
struct TNode* root=NULL;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}
*/
   struct TNode *root        = newNode(1);
    root->left               = newNode(2);
    root->right              = newNode(3);
    root->right->left        = newNode(4);
    root->right->right       = newNode(5);
    root->right->left->left  = newNode(6);
    root->right->left->left->left  = newNode(7);
    root->right->left->left->right = newNode(8);
    root->right->right->right      = newNode(9);
    root->right->right->right->left = newNode(10);




printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();

struct TNode** buffer=(struct TNode**)malloc(sizeof(struct TNode*)*n);
for(i=0;i<n;i++){
buffer[i]=(struct TNode*)malloc(sizeof(struct TNode));
}
struct TNode* target=root->right->left;
ClosestLeafNode(root,target,buffer,0);
//printf("\nResult: %d",result);
return 0;
}
示例#13
0
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
printf("\nEnter to see if tree is complete binary tree : \n\n");
getch();
/*
struct TNode* tmpNode=(struct TNode*)malloc(sizeof(struct TNode));
tmpNode->data=10;
tmpNode->left=NULL;
tmpNode->right=NULL;
root->right->right=tmpNode;
*/
int result=CheckIfCompleteBinaryTree(root,n);
if(result){
printf("\nComplete binary tree\n\n");
}else{
printf("\nNot complete BT\n\n");
getch();
}
return 0;
}
示例#14
0
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
int isLeftSet=0;
int isRightSet=0;
struct TNode* tmpNode=(struct TNode*)malloc(sizeof(struct TNode));
tmpNode->data=10;
tmpNode->left=NULL;
tmpNode->right=NULL;
struct TNode* ancestor=GetLCA(root,root->left->left,tmpNode,&isLeftSet,&isRightSet);
if(isLeftSet && isRightSet){
printf("\nAncestor : %d\n",ancestor->data);
}else{
printf("\nNULL\n");
}

return 0;
}