From 1e2fe9bcd6d8b9bf3525cc7997396cd84dadc076 Mon Sep 17 00:00:00 2001 From: rajtirole <91474946+rajtirole@users.noreply.github.com> Date: Wed, 27 Oct 2021 11:36:31 +0530 Subject: [PATCH] Preetyprinting.cpp Make pretty printing binary tree program in cpp #hactoberfest2021 --- Preety printing binary tree | 180 ++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 Preety printing binary tree diff --git a/Preety printing binary tree b/Preety printing binary tree new file mode 100644 index 0000000..31646c0 --- /dev/null +++ b/Preety printing binary tree @@ -0,0 +1,180 @@ +#include +#include +#include +using namespace std; + +struct node{ +int data; +struct node*left; +struct node*right; +}; +//------------------------------------------------New Node +struct node*newNode(int data){ +struct node* node = new struct node; +node->data = data; +node->left = NULL; +node->right = NULL; +return node; +} +//--------------------------------------------------Insertion +struct node* insert(struct node*node,int data){ +if(node == NULL){ + return newNode(data); +} + +else{ + if(data data){ + node->left = insert(node->left,data); + } + else{ + node->right = insert(node->right,data); + } + + return node; + } +} +//------------------------------------------------------Print Inorder +void printInorder(struct node*node){ +if(node==NULL) + return; +printInorder(node->left); +cout<data<<" "; +printInorder(node->right); + +} +//-----------------------------------------------Count Nodes +int getCount(struct node*node){ +if(node==NULL) + return 0; +else + return getCount(node->left)+getCount(node->right)+1; +} +//----------------------------------------------------getMaxHeight +int getMaxHeight(struct node*root){ +if(root==NULL) + return 0; + +int leftHeight = getMaxHeight(root->left); +int rightHeight = getMaxHeight(root->right); + +return max(leftHeight,rightHeight)+1; +} + +//-------------------------------------------------Print Level Order +void printLevelorder(struct node*root){ +queue q; + +q.push(root); +q.push(NULL); + +while(!q.empty()){ + + + struct node* n = q.front(); + q.pop(); + if(q.empty()){ + cout<data<<" "; + if(n->left!=NULL) + q.push(n->left); + if(n->right!=NULL) + q.push(n->right); + } + } +} +void printSpaces(int spaces){ + for(int i=0;i q; + + q.push(root); + q.push(NULL); + + while(!q.empty()){ + + + struct node* n = q.front(); + q.pop(); + if(q.empty()){ + cout<data!=-1){ + printSpaces(spaces); + cout<data; + printSpaces(spaces); + } + /*else{ + printSpaces(spaces); + cout<<" "; + printSpaces(spaces); + }*/ + if(n->left!=NULL) + q.push(n->left); + else{ + struct node*dummy = new struct node; + dummy->data = -1; + dummy->left = dummy->right = NULL; + q.push(dummy); + } + if(n->right!=NULL){ + q.push(n->right); + } + else{ + struct node*dummy = new struct node; + dummy->data = -1; + dummy->left = dummy->right = NULL; + q.push(dummy); + } + } + } +} +void printPreorder(struct node*root){ + +} + +int main(){ +struct node* root = NULL; +root = insert(root,5); +root=insert(root,3); +root=insert(root,2); +root=insert(root,4); +root=insert(root,7); +root=insert(root,6); +root=insert(root,8); +root=insert(root,9); +printLevelorder(root); + +prettyPrint(root); +int h=0; +//cout<<"Diameter :"<