11 November
11 November
11 November
#include<iostream>
struct node{
int data;
};
n->data = data;
n->left = NULL;
n->right = NULL;
return n;
if(root!=NULL){
std::cout<<" "<<root->data;
preOrder(root->left);
preOrder(root->right);
int main(){
p->right = p2;
p1->left = p3;
p1->right = p4;
preOrder(p);
return 0;
POSTODER
#include<iostream>
struct node{
int data;
};
n->data = data;
n->left = NULL;
n->right = NULL;
return n;
if(root!=NULL){
std::cout<<" "<<root->data;
preOrder(root->left);
preOrder(root->right);
int main(){
p->left = p1;
p->right = p2;
p1->left = p3;
p1->right = p4;
preOrder(p);
return 0;
DEPTH
#include <iostream>
struct Node {
int key;
};
temp->key = key;
int depth = 0;
queue<Node*> q;
q.push(root);
q.push(NULL);
while (!q.empty()) {
q.pop();
if (temp == NULL) {
depth++;
if (temp != NULL) {
if (temp->left) {
q.push(temp->left);
if (temp->right) {
q.push(temp->right);
else if (!q.empty()) {
q.push(NULL);
return depth;
}
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
HEIGHT
#include <iostream>
class node {
public:
int data;
node* left;
node* right;
};
if (node == NULL)
return 0;
else {
else
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return (Node);
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
return 0;
}
GENERAL ORDER
#include<iostream>
struct node{
int data;
};
node->data=data;
node->left=NULL;
node->right=NULL;
return(node);
int main()
cout<<"\t\t\t"<<rootnode->data<<endl;
cout<<"\t\t\t"<<"/"<<"\\"<<endl;
rootnode->left=newnode(20);
rootnode->right=newnode(30);
cout<<"\t\t\t"<<rootnode->left->data<<" "<<rootnode->right->data;
cout<<endl;
rootnode->left->left=newnode(40);
cout<<"\t\t\t"<<"/"<<endl;
cout<<"\t\t\t"<<rootnode->left->left->data;
return 0;