Open In App

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.

Degenerate Binary tree is of two types:



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:

Below is the implementation of the Right-skewed Tree:




// C++ implementation of above approach
#include <iostream>
using namespace std;
 
class Node {
public:
    int data;
    Node* left;
    Node* right;
 
    Node(int val)
    {
        data = val;
        left = NULL;
        right = NULL;
    }
};
 
// Function to insert nodes on the right side of
// current node
Node* insert(Node* root, int data)
{
    if (root == NULL) {
        root = new Node(data);
    }
    else {
        root->right = insert(root->right, data);
    }
    return root;
}
 
// Function to print tree
void printTree(Node* node)
{
    if (node != NULL) {
        cout << node->data << endl;
        printTree(node->right);
    }
}
 
// Driver code
int main()
{
    Node* root = NULL;
    root = insert(root, 1);
    insert(root, 2);
    insert(root, 3);
    insert(root, 4);
    insert(root, 5);
 
    // Function call
    printTree(root);
 
    return 0;
}
 
// This code is contributed by Prajwal Kandekar




class Node {
    int data;
    Node left;
    Node right;
 
    Node(int val)
    {
        data = val;
        left = null;
        right = null;
    }
}
 
public class Main {
    // Function to insert nodes on the right side of
    // current node
    public static Node insert(Node root, int data)
    {
        if (root == null) {
            root = new Node(data);
        }
        else {
            root.right = insert(root.right, data);
        }
        return root;
    }
 
    // Function to print tree
    public static void printTree(Node node)
    {
        if (node != null) {
            System.out.println(node.data);
            printTree(node.right);
        }
    }
 
    // Driver code
    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);
 
        // Function call
        printTree(root);
    }
}




# Python implementation of above approach
class Node:
    def __init__(self, data):
        self.left = None
        self.right = None
        self.data = data
 
# Function to insert nodes on the right side of
# current node
 
 
def insert(root, data):
    if root is None:
        root = Node(data)
    else:
        root.right = insert(root.right, data)
    return root
 
# Function to print tree
 
 
def printTree(node):
    if node is not None:
        print(node.data)
        printTree(node.right)
 
 
# Driver code
root = None
root = insert(root, 1)
insert(root, 2)
insert(root, 3)
insert(root, 4)
insert(root, 5)
 
# Function call
printTree(root)




// C# implementation of above approach
using System;
 
class Node {
    public int data;
    public Node left;
    public Node right;
 
    public Node(int val)
    {
        data = val;
        left = null;
        right = null;
    }
}
 
// Function to insert nodes on the right side of
// current node
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;
    }
    // Function to print tree
    static void PrintTree(Node node)
    {
        if (node != null) {
            Console.WriteLine(node.data);
            PrintTree(node.right);
        }
    }
    // Driver code
    static void Main(string[] args)
    {
        Node root = null;
        root = Insert(root, 1);
        Insert(root, 2);
        Insert(root, 3);
        Insert(root, 4);
        Insert(root, 5);
 
        // Function call
        PrintTree(root);
 
        Console.ReadKey();
    }
}




class Node {
  constructor(val) {
    this.data = val;
    this.left = null;
    this.right = null;
  }
}
 
// Function to insert nodes on the right side of
// current node
function insert(root, data) {
  if (root === null) {
    root = new Node(data);
  } else {
    root.right = insert(root.right, data);
  }
  return root;
}
 
// Function to print tree
function printTree(node) {
  if (node !== null) {
    console.log(node.data);
    printTree(node.right);
  }
}
 
// Driver code
let root = null;
root = insert(root, 1);
insert(root, 2);
insert(root, 3);
insert(root, 4);
insert(root, 5);
 
// Function call
printTree(root);

Output
1
2
3
4
5







Output : 

 

Below is the implementation of the Left-skewed Tree:




// C++ implementation of above approach
#include <iostream>
 
// Node class
class Node {
public:
    int data;
    Node* left;
    Node* right;
 
    Node(int data) {
        this->data = data;
        this->left = nullptr;
        this->right = nullptr;
    }
};
 
// Function to insert nodes on the left side of
// current node
Node* insert(Node* root, int data) {
    if (root == nullptr) {
        root = new Node(data);
    }
    else {
        root->left = insert(root->left, data);
    }
    return root;
}
 
// Function to print tree
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);
 
    // Function call
    printTree(root);
 
    return 0;
}
 
// This code is contributed by Vaibhav nandan




// Node class
class Node {
    int data;
    Node left;
    Node right;
 
    // Constructor to initialize a new node
    Node(int data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
 
public class BinaryTree {
    // Function to insert nodes on the left side of the current node
    static Node insert(Node root, int data) {
        // If the root is null, create a new node with the given data
        if (root == null) {
            root = new Node(data);
        } else {
            // If the root is not null, recursively insert on the left side
            root.left = insert(root.left, data);
        }
        return root;
    }
 
    // Function to print the tree using inorder traversal
    static void printTree(Node node) {
        // Base case: if the node is not null
        if (node != null) {
            // Print the data of the current node
            System.out.println(node.data);
            // Recursively print the left subtree
            printTree(node.left);
        }
    }
 
    // Main method
    public static void main(String[] args) {
        // Create a root node
        Node root = null;
 
        // Insert nodes into the tree
        root = insert(root, 1);
        insert(root, 2);
        insert(root, 3);
        insert(root, 4);
        insert(root, 5);
 
        // Function call to print the tree
        printTree(root);
    }
}




# Python implementation of above approach
class Node:
    def __init__(self, data):
        self.left = None
        self.right = None
        self.data = data
 
# Function to insert nodes on the left side of
# current node
 
 
def insert(root, data):
    if root is None:
        root = Node(data)
    else:
        root.left = insert(root.left, data)
    return root
 
# Function to print tree
 
 
def printTree(node):
    if node is not None:
        print(node.data)
        printTree(node.left)
 
 
# Driver code
root = None
root = insert(root, 1)
insert(root, 2)
insert(root, 3)
insert(root, 4)
insert(root, 5)
 
# Function call
printTree(root)




using System;
 
// Node class
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
{
    // Function to insert nodes on the left side of the current node
    static Node Insert(Node root, int data)
    {
        if (root == null)
        {
            root = new Node(data);
        }
        else
        {
            root.Left = Insert(root.Left, data);
        }
        return root;
    }
 
    // Function to print the tree (pre-order traversal)
    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);
 
        // Function call to print the tree
        PrintTree(root);
    }
}




class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
 
// Function to insert nodes on the left side of the current node
function insert(root, data) {
    if (root === null) {
        root = new Node(data);
    } else {
        root.left = insert(root.left, data);
    }
    return root;
}
 
// Function to print the tree in-order
function printTree(node) {
    if (node !== null) {
        console.log(node.data);
        printTree(node.left);
    }
}
 
// Main function
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);
 
    // Function call to print the tree
    printTree(root);
}
 
// Run the main function
main();

Output
1
2
3
4
5







Time Complexity: O(N)
Auxiliary Space: O(N)


Article Tags :