-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicalANDBinaryTree.c
87 lines (70 loc) · 2.18 KB
/
LogicalANDBinaryTree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Convert a Binary Tree to a Logical AND Binary Tree
#include<stdio.h>
#include<stdlib.h>
// #define MAX_QUEUE_SIZE 200
typedef struct node node;
/**
* Creating a structure for a node
**/
struct node {
int data;
node *left;
node *right;
};
/**
* Helper function to create new node in the tree
**/
node* newNode(int value) {
node* newNode = (node*)malloc(sizeof(node));
newNode->data=value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
/**
* Inorder traversal recursive function for a tree
**/
void printInorder(node *root) {
if(root==NULL) return;
printInorder(root->left);
printf("%d ", root->data);
printInorder(root->right);
}
/**
* recursive function for converting a normal Ninary tree to Logical AND Binary Tree
**/
void convert2LogicalANDTree(node *root) {
if(root==NULL) return;
// if(root->left==NULL || root->right==NULL) return;
// if(root->left!=NULL) convert2LogicalANDTree(root->left);
// if(root->right!=NULL) convert2LogicalANDTree(root->right);
convert2LogicalANDTree(root->left);
convert2LogicalANDTree(root->right);
if(root->left!=NULL && root->right!=NULL) root->data = root->left->data & root->right->data;
// if(root->left!=NULL && root->right!=NULL) return convert2LogicalANDTree(root->left) && convert2LogicalANDTree(root->right);
}
/**
* Main Driving Function
**/
void main() {
node* a = newNode(1);
a->left = newNode(1);
a->right = newNode(0);
a->left->left = newNode(0);
a->left->right = newNode(1);
a->right->left = newNode(1);
a->right->right= newNode(1);
// 1 0
// / \ / \
// 1 0 ---> 0 1
// / \ / \ / \ / \
// 0 1 1 1 0 1 1 1
//
//
// printf("\nHalf node count: %d",getHalfNodes(a));
printf("\nBefre: ");
printInorder(a);
convert2LogicalANDTree(a);
printf("\nAfter: ");
printInorder(a);
}