Introduction to Degenerate Binary Tree
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 to use 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 of the Right-skewed Tree:
C++
#include <iostream>
using namespace std;
class Node {
public :
int data;
Node* left;
Node* right;
Node( int val)
{
data = val;
left = NULL;
right = NULL;
}
};
Node* insert(Node* root, int data)
{
if (root == NULL) {
root = new Node(data);
}
else {
root->right = insert(root->right, data);
}
return root;
}
void printTree(Node* node)
{
if (node != NULL) {
cout << node->data << endl;
printTree(node->right);
}
}
int main()
{
Node* root = NULL;
root = insert(root, 1);
insert(root, 2);
insert(root, 3);
insert(root, 4);
insert(root, 5);
printTree(root);
return 0;
}
|
Java
class Node {
int data;
Node left;
Node right;
Node( int val)
{
data = val;
left = null ;
right = null ;
}
}
public class Main {
public static Node insert(Node root, int data)
{
if (root == null ) {
root = new Node(data);
}
else {
root.right = insert(root.right, data);
}
return root;
}
public static void printTree(Node node)
{
if (node != null ) {
System.out.println(node.data);
printTree(node.right);
}
}
public static void main(String[] args)
{
Node root = null ;
root = insert(root, 1 );
insert(root, 2 );
insert(root, 3 );
insert(root, 4 );
insert(root, 5 );
printTree(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .left = None
self .right = None
self .data = data
def insert(root, data):
if root is None :
root = Node(data)
else :
root.right = insert(root.right, data)
return root
def printTree(node):
if node is not None :
print (node.data)
printTree(node.right)
root = None
root = insert(root, 1 )
insert(root, 2 )
insert(root, 3 )
insert(root, 4 )
insert(root, 5 )
printTree(root)
|
C#
using System;
class Node {
public int data;
public Node left;
public Node right;
public Node( int val)
{
data = val;
left = null ;
right = null ;
}
}
class Program {
static Node Insert(Node root, int data)
{
if (root == null ) {
root = new Node(data);
}
else {
root.right = Insert(root.right, data);
}
return root;
}
static void PrintTree(Node node)
{
if (node != null ) {
Console.WriteLine(node.data);
PrintTree(node.right);
}
}
static void Main( string [] args)
{
Node root = null ;
root = Insert(root, 1);
Insert(root, 2);
Insert(root, 3);
Insert(root, 4);
Insert(root, 5);
PrintTree(root);
Console.ReadKey();
}
}
|
Javascript
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
function insert(root, data) {
if (root === null ) {
root = new Node(data);
} else {
root.right = insert(root.right, data);
}
return root;
}
function printTree(node) {
if (node !== null ) {
console.log(node.data);
printTree(node.right);
}
}
let root = null ;
root = insert(root, 1);
insert(root, 2);
insert(root, 3);
insert(root, 4);
insert(root, 5);
printTree(root);
|
Output :
Below is the implementation of the Left-skewed Tree:
C++14
#include <iostream>
class Node {
public :
int data;
Node* left;
Node* right;
Node( int data) {
this ->data = data;
this ->left = nullptr;
this ->right = nullptr;
}
};
Node* insert(Node* root, int data) {
if (root == nullptr) {
root = new Node(data);
}
else {
root->left = insert(root->left, data);
}
return root;
}
void printTree(Node* node) {
if (node != nullptr) {
std::cout << node->data << std::endl;
printTree(node->left);
}
}
int main() {
Node* root = nullptr;
root = insert(root, 1);
insert(root, 2);
insert(root, 3);
insert(root, 4);
insert(root, 5);
printTree(root);
return 0;
}
|
Java
class Node {
int data;
Node left;
Node right;
Node( int data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
public class BinaryTree {
static Node insert(Node root, int data) {
if (root == null ) {
root = new Node(data);
} else {
root.left = insert(root.left, data);
}
return root;
}
static void printTree(Node node) {
if (node != null ) {
System.out.println(node.data);
printTree(node.left);
}
}
public static void main(String[] args) {
Node root = null ;
root = insert(root, 1 );
insert(root, 2 );
insert(root, 3 );
insert(root, 4 );
insert(root, 5 );
printTree(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .left = None
self .right = None
self .data = data
def insert(root, data):
if root is None :
root = Node(data)
else :
root.left = insert(root.left, data)
return root
def printTree(node):
if node is not None :
print (node.data)
printTree(node.left)
root = None
root = insert(root, 1 )
insert(root, 2 )
insert(root, 3 )
insert(root, 4 )
insert(root, 5 )
printTree(root)
|
C#
using System;
class Node
{
public int Data;
public Node Left;
public Node Right;
public Node( int data)
{
this .Data = data;
this .Left = null ;
this .Right = null ;
}
}
class BinaryTree
{
static Node Insert(Node root, int data)
{
if (root == null )
{
root = new Node(data);
}
else
{
root.Left = Insert(root.Left, data);
}
return root;
}
static void PrintTree(Node node)
{
if (node != null )
{
Console.WriteLine(node.Data);
PrintTree(node.Left);
}
}
static void Main()
{
Node root = null ;
root = Insert(root, 1);
Insert(root, 2);
Insert(root, 3);
Insert(root, 4);
Insert(root, 5);
PrintTree(root);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
function insert(root, data) {
if (root === null ) {
root = new Node(data);
} else {
root.left = insert(root.left, data);
}
return root;
}
function printTree(node) {
if (node !== null ) {
console.log(node.data);
printTree(node.left);
}
}
function main() {
let root = null ;
root = insert(root, 1);
root = insert(root, 2);
root = insert(root, 3);
root = insert(root, 4);
root = insert(root, 5);
printTree(root);
}
main();
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
30 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...