Every non-leaf node has just one child in a binary tree known as a Degenerate Binary tree. The tree effectively transforms into a linked list as a result, with each node linking to its single child.
When a straightforward and effective data structure is required, degenerate binary trees, a special case of binary trees, may be employed. For instance, a degenerate binary tree can be used to create a stack data structure since each node simply needs to store a reference to the next node.
Degenerate binary trees are less effective than balanced binary trees at searching, inserting, and deleting data, though. This is due to the fact that degenerate binary trees may become unbalanced, resulting in performance that is comparable to a linked list in the worst case.
Degenerate binary trees are rarely employed by themselves as a data structure in general. Rather, they are frequently utilized as a component of other data structures like linked lists, stacks, and queues. Degenerate binary trees can also be utilized as a particular instance to take into account in algorithms that traverse or search over binary trees.
Degenerate Binary tree is of two types:
Left-skewed Tree: If all the nodes in the degenerate tree have only a left child.
Right-skewed Tree: If all the nodes in the degenerate tree have only a right child.
Examples:
Input: 1, 2, 3, 4, 5 Output:
Representation of right skewed tree
Explanation: Each parent node in this tree only has one child node. In essence, the tree is a linear chain of nodes.
Approach: To solve the problem follow the below idea:
Idea is touse handle degenerate binary trees relies on the issue at hand. Degenerate trees may typically be transformed into linked lists or arrays, which can make some operations easier.
Steps involved in the implementation of code:
Initializing structure of Tree.
Inserting all data on the right side of a current node.
Printing node by considering only the right node of each node.
Below is the implementation ofthe Right-skewed Tree:
C++
// C++ implementation of above approach#include<iostream>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intval){data=val;left=NULL;right=NULL;}};// Function to insert nodes on the right side of// current nodeNode*insert(Node*root,intdata){if(root==NULL){root=newNode(data);}else{root->right=insert(root->right,data);}returnroot;}// Function to print treevoidprintTree(Node*node){if(node!=NULL){cout<<node->data<<endl;printTree(node->right);}}// Driver codeintmain(){Node*root=NULL;root=insert(root,1);insert(root,2);insert(root,3);insert(root,4);insert(root,5);// Function callprintTree(root);return0;}// This code is contributed by Prajwal Kandekar
Java
classNode{intdata;Nodeleft;Noderight;Node(intval){data=val;left=null;right=null;}}publicclassMain{// Function to insert nodes on the right side of// current nodepublicstaticNodeinsert(Noderoot,intdata){if(root==null){root=newNode(data);}else{root.right=insert(root.right,data);}returnroot;}// Function to print treepublicstaticvoidprintTree(Nodenode){if(node!=null){System.out.println(node.data);printTree(node.right);}}// Driver codepublicstaticvoidmain(String[]args){Noderoot=null;root=insert(root,1);insert(root,2);insert(root,3);insert(root,4);insert(root,5);// Function callprintTree(root);}}
Python3
# Python implementation of above approachclassNode:def__init__(self,data):self.left=Noneself.right=Noneself.data=data# Function to insert nodes on the right side of# current nodedefinsert(root,data):ifrootisNone:root=Node(data)else:root.right=insert(root.right,data)returnroot# Function to print treedefprintTree(node):ifnodeisnotNone:print(node.data)printTree(node.right)# Driver coderoot=Noneroot=insert(root,1)insert(root,2)insert(root,3)insert(root,4)insert(root,5)# Function callprintTree(root)
C#
// C# implementation of above approachusingSystem;classNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intval){data=val;left=null;right=null;}}// Function to insert nodes on the right side of// current nodeclassProgram{staticNodeInsert(Noderoot,intdata){if(root==null){root=newNode(data);}else{root.right=Insert(root.right,data);}returnroot;}// Function to print treestaticvoidPrintTree(Nodenode){if(node!=null){Console.WriteLine(node.data);PrintTree(node.right);}}// Driver codestaticvoidMain(string[]args){Noderoot=null;root=Insert(root,1);Insert(root,2);Insert(root,3);Insert(root,4);Insert(root,5);// Function callPrintTree(root);Console.ReadKey();}}
JavaScript
classNode{constructor(val){this.data=val;this.left=null;this.right=null;}}// Function to insert nodes on the right side of// current nodefunctioninsert(root,data){if(root===null){root=newNode(data);}else{root.right=insert(root.right,data);}returnroot;}// Function to print treefunctionprintTree(node){if(node!==null){console.log(node.data);printTree(node.right);}}// Driver codeletroot=null;root=insert(root,1);insert(root,2);insert(root,3);insert(root,4);insert(root,5);// Function callprintTree(root);
Output
1
2
3
4
5
Output :
Below is the implementation of the Left-skewed Tree:
C++14
// C++ implementation of above approach#include<iostream>// Node classclassNode{public:intdata;Node*left;Node*right;Node(intdata){this->data=data;this->left=nullptr;this->right=nullptr;}};// Function to insert nodes on the left side of // current nodeNode*insert(Node*root,intdata){if(root==nullptr){root=newNode(data);}else{root->left=insert(root->left,data);}returnroot;}// Function to print treevoidprintTree(Node*node){if(node!=nullptr){std::cout<<node->data<<std::endl;printTree(node->left);}}intmain(){Node*root=nullptr;root=insert(root,1);insert(root,2);insert(root,3);insert(root,4);insert(root,5);// Function callprintTree(root);return0;}// This code is contributed by Vaibhav nandan
Java
// Node classclassNode{intdata;Nodeleft;Noderight;// Constructor to initialize a new nodeNode(intdata){this.data=data;this.left=null;this.right=null;}}publicclassBinaryTree{// Function to insert nodes on the left side of the current nodestaticNodeinsert(Noderoot,intdata){// If the root is null, create a new node with the given dataif(root==null){root=newNode(data);}else{// If the root is not null, recursively insert on the left sideroot.left=insert(root.left,data);}returnroot;}// Function to print the tree using inorder traversalstaticvoidprintTree(Nodenode){// Base case: if the node is not nullif(node!=null){// Print the data of the current nodeSystem.out.println(node.data);// Recursively print the left subtreeprintTree(node.left);}}// Main methodpublicstaticvoidmain(String[]args){// Create a root nodeNoderoot=null;// Insert nodes into the treeroot=insert(root,1);insert(root,2);insert(root,3);insert(root,4);insert(root,5);// Function call to print the treeprintTree(root);}}
Python3
# Python implementation of above approachclassNode:def__init__(self,data):self.left=Noneself.right=Noneself.data=data# Function to insert nodes on the left side of# current nodedefinsert(root,data):ifrootisNone:root=Node(data)else:root.left=insert(root.left,data)returnroot# Function to print treedefprintTree(node):ifnodeisnotNone:print(node.data)printTree(node.left)# Driver coderoot=Noneroot=insert(root,1)insert(root,2)insert(root,3)insert(root,4)insert(root,5)# Function callprintTree(root)
C#
usingSystem;// Node classclassNode{publicintData;publicNodeLeft;publicNodeRight;publicNode(intdata){this.Data=data;this.Left=null;this.Right=null;}}classBinaryTree{// Function to insert nodes on the left side of the current nodestaticNodeInsert(Noderoot,intdata){if(root==null){root=newNode(data);}else{root.Left=Insert(root.Left,data);}returnroot;}// Function to print the tree (pre-order traversal)staticvoidPrintTree(Nodenode){if(node!=null){Console.WriteLine(node.Data);PrintTree(node.Left);}}staticvoidMain(){Noderoot=null;root=Insert(root,1);Insert(root,2);Insert(root,3);Insert(root,4);Insert(root,5);// Function call to print the treePrintTree(root);}}
JavaScript
classNode{constructor(data){this.data=data;this.left=null;this.right=null;}}// Function to insert nodes on the left side of the current nodefunctioninsert(root,data){if(root===null){root=newNode(data);}else{root.left=insert(root.left,data);}returnroot;}// Function to print the tree in-orderfunctionprintTree(node){if(node!==null){console.log(node.data);printTree(node.left);}}// Main functionfunctionmain(){letroot=null;root=insert(root,1);root=insert(root,2);root=insert(root,3);root=insert(root,4);root=insert(root,5);// Function call to print the treeprintTree(root);}// Run the main functionmain();