Professional Documents
Culture Documents
h>
#include <stdlib.h>
struct node {
int data;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
while(1) {
parent = current;
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
//not found
if(current == NULL) {
return NULL;
}
}
return current;
}
LEFT VIEW:
RIGHT VIEW:
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
TOP VIEW:
1
/ \
2 3
/ \ / \
4 5 6 7
Top view of the above binary tree is
4 2 1 3 7
1
/ \
2 3
\
4
\
5
\
6
Top view of the above binary tree is
2 1 3 6
ANOTHER APPROCH:
SUM TREE:
A SumTree is a Binary Tree where the value of a node is equal to the sum of
the nodes present in its left subtree and right subtree. An empty tree is
SumTree and the sum of an empty tree can be considered as 0. A leaf node is
also considered as SumTree.
INTERNAL NODE:
void printNonLeafNodes(Node* root) {
queue<Node*> treeNodes;
treeNodes.push(root);
while (!treeNodes.empty()) {
Node* curr = treeNodes.front();
treeNodes.pop();
bool isInternal = 0;
if (curr->left) {
isInternal = 1;
treeNodes.push(curr->left);
}
if (curr->right) {
isInternal = 1;
treeNodes.push(curr->right);
}
if (isInternal)
cout<<curr->data<<"\t";
}
}
FULL NODE:
DEGREE:
#include <stdio.h>
int main()
int btree[MAX];
int degtree[MAX];
btree[i] = -1;
degtree[i] = 0;
int n;
int leaf_node = 0;
scanf("%d", &n);
scanf("%d", &btree[i]);
if(btree[2*p] != -1)
degtree[p]++;
if(btree[2*p + 1] != -1)
degtree[p]++;
}
for(int p=1; p<=n; p++)
leaf_node++;
if(btree[p] != -1)
KTH NODE:
COUNT:
1.int countnodes(struct node *root)
2.{
3. if(root != NULL)
4. {
5. countnodes(root->left);
6. count++;
7. countnodes(root->right);
8. }
9. return count;
10. }
int cur = 1, mx = 0;
int node;
struct Node* previous = NULL;
// Find the inorder traversal of the BST
void inorder(struct Node* root)
{
// If root is NULL then return
if (root == NULL) {
return;
}
inorder(root->left);
if (previous != NULL) {
// If the previous value is equal to the current value
// then increase the count
if (root->val == previous->val) {
cur++;
}
// Else initialize the count by 1
else {
cur = 1;
}
}
// If current count is greater than the max count
// then update the mx value
if (cur > mx) {
mx = cur;
node = root->val;
}
// Make the current Node as previous
previous = root;
inorder(root->right);
}
// Utility function
int findnode(struct Node* root)
{
inorder(root);
return node;
}
DUPLICATE: