In this tutorial, We will learn a c++ implementation to Print All Nodes that don’t have Sibling.
A child node is said to have a sibling if the other node has the same parent as the child node. That means the nodes are siblings if they have same parent.
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 | #include <bits/stdc++.h> using namespace std; // tree node is defined class tree{ public: int data; tree *left; tree *right; }; void printSibling(tree* root) { //Declare queue using STL queue<tree*> q; //enqueue the root q.push(root); vector<int> store; tree* temp; //do the level order traversal & check for siblings while(!q.empty()){ //dequeue temp=q.front(); q.pop(); //if the current node has only one child //definitely the child has no sibling //store the child node value if(temp->left==NULL && temp->right!=NULL){ store.push_back(temp->right->data); } if(temp->left!=NULL && temp->right==NULL){ store.push_back(temp->left->data); } // do level order traversing if(temp->right) q.push(temp->right); if(temp->left) q.push(temp->left); } //if no node found without having sibling //vector size is zero //print -1 if(store.size()==0){ printf("-1, no such node\n"); return; } //sort the vector to print sorted node value sort(store.begin(),store.end()); //printing for(auto it=store.begin();it!=store.end();it++) printf("%d ",*it); } tree* newnode(int data) // creating new node { tree* node = (tree*)malloc(sizeof(tree)); node->data = data; node->left = NULL; node->right = NULL; return(node); } int main() { //**same tree is builted as shown in example** cout<<"same tree is built as shown in example\n"; tree *root=newnode(2); root->left= newnode(7); root->right= newnode(5); root->right->right=newnode(9); root->right->right->left=newnode(4); root->left->left=newnode(2); root->left->right=newnode(6); root->left->right->left=newnode(5); root->left->right->right=newnode(11); cout<<"printing the nodes that don't have sibling...\n"<<endl; printSibling(root); return 0; } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.