Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Tree
  • Practice Tree
  • MCQs on Tree
  • Tutorial on Tree
  • Types of Trees
  • Basic operations
  • Tree Traversal
  • Binary Tree
  • Complete Binary Tree
  • Ternary Tree
  • Binary Search Tree
  • Red-Black Tree
  • AVL Tree
  • Full Binary Tree
  • B-Tree
  • Advantages & Disadvantages
Open In App
Next Article:
AA Trees | Set 1 (Introduction)
Next article icon

Introduction to Degenerate Binary Tree

Last Updated : 30 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// 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 
Java
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);     } } 
Python3
# 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#
// 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();     } } 
JavaScript
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++14
// 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 
Java
// 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);     } } 
Python3
# 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) 
C#
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);     } } 
JavaScript
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)


Next Article
AA Trees | Set 1 (Introduction)

A

avdhutgharat
Improve
Article Tags :
  • DSA
  • Tree
Practice Tags :
  • Tree

Similar Reads

  • Introduction to Binary Tree
    Binary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves. Representation of Binary TreeEach node in a Binar
    15+ min read
  • Introduction to Generic Trees (N-ary Trees)
    Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes. Every node stores address of its children and t
    5 min read
  • Introduction to Binary Search Tree
    Binary Search Tree is a data structure used in computer science for organizing and storing data in a sorted manner. Binary search tree follows all properties of binary tree and for every nodes, its left subtree contains values less than the node and the right subtree contains values greater than the
    3 min read
  • Introduction of B-Tree
    A B-Tree is a specialized m-way tree designed to optimize data access, especially on disk-based storage systems. In a B-Tree of order m, each node can have up to m children and m-1 keys, allowing it to efficiently manage large datasets.The value of m is decided based on disk block and key sizes.One
    8 min read
  • AA Trees | Set 1 (Introduction)
    AA trees are the variation of the red-black trees, a form of binary search tree. AA trees use the concept of levels to aid in balancing binary trees. The level of node (instead of colour) is used for balancing information. A link where child and parent's levels are same, is called a horizontal link,
    3 min read
  • Invert Binary Tree - Change to Mirror Tree
    Given a binary tree, the task is to convert the binary tree to its Mirror tree. Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged. Example 1: Explanation: In the inverted tree, every non-leaf node has its left and right child interc
    15 min read
  • Binary Tree Data Structure
    A Binary Tree Data Structure is a hierarchical data structure in which each node has at most two children, referred to as the left child and the right child. It is commonly used in computer science for efficient storage and retrieval of data, with various operations such as insertion, deletion, and
    3 min read
  • Binary Tree Representation
    Binary tree is a tree data structure (non-linear) in which each node can have at most two children which are referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves. A binary tree can be visualized as a hier
    4 min read
  • Introduction to Tree Data Structure
    Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree. Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) o
    15+ min read
  • Introduction to Link-Cut Tree
    Link Cut Trees (LCT) is a data structure that allows for efficient dynamic maintenance of trees. It is a type of self-adjusting data structure that allows for efficient manipulation of trees, such as link and cut operations, find-root, and access. The implementation of an LCT typically consists of a
    15 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences