11 November

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

PREORDER

#include<iostream>

using namespace std;

struct node{

int data;

struct node* left;

struct node* right;

};

struct node* createNode(int data){

node *n=new node;

n->data = data;

n->left = NULL;

n->right = NULL;

return n;

void preOrder(struct node* root){

if(root!=NULL){

std::cout<<" "<<root->data;

preOrder(root->left);

preOrder(root->right);

int main(){

struct node *p = createNode(4);

struct node *p1 = createNode(1);

struct node *p2 = createNode(6);

struct node *p3 = createNode(5);

struct node *p4 = createNode(2);


p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

preOrder(p);

return 0;

POSTODER

#include<iostream>

using namespace std;

struct node{

int data;

struct node* left;

struct node* right;

};

struct node* createNode(int data){

node *n=new node;

n->data = data;

n->left = NULL;

n->right = NULL;

return n;

void preOrder(struct node* root){

if(root!=NULL){

std::cout<<" "<<root->data;

preOrder(root->left);
preOrder(root->right);

int main(){

struct node *p = createNode(4);

struct node *p1 = createNode(1);

struct node *p2 = createNode(6);

struct node *p3 = createNode(5);

struct node *p4 = createNode(2);

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

preOrder(p);

return 0;

DEPTH

#include <iostream>

using namespace std;

struct Node {

int key;

struct Node *left, *right;

};

Node* newNode(int key)

Node* temp = new Node;

temp->key = key;

temp->left = temp->right = NULL;


return (temp);

int height(struct Node* root)

int depth = 0;

queue<Node*> q;

q.push(root);

q.push(NULL);

while (!q.empty()) {

Node* temp = q.front();

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()

Node* root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

cout << "Height(Depth) of tree is: " << height(root);

HEIGHT

#include <iostream>

using namespace std;

class node {

public:

int data;

node* left;

node* right;

};

int maxDepth(node* node)

if (node == NULL)

return 0;

else {

int lDepth = maxDepth(node->left);

int rDepth = maxDepth(node->right);


if (lDepth > rDepth)

return (lDepth + 1);

else

return (rDepth + 1);

node* newNode(int data)

node* Node = new node();

Node->data = data;

Node->left = NULL;

Node->right = NULL;

return (Node);

int main()

node* root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

cout << "Height of tree is " << maxDepth(root);

return 0;

}
GENERAL ORDER

#include<iostream>

using namespace std;

struct node{

int data;

struct node *left;

struct node *right;

};

struct node* newnode(int data)

struct node* node=new struct node();

node->data=data;

node->left=NULL;

node->right=NULL;

return(node);

int main()

struct node *rootnode=newnode(10);

cout<<"general tree created is as follows="<<endl;

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;

You might also like