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, 5Output: Representation of right skewed treeExplanation: 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); Output1 2 3 4 5Output : 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(); Output1 2 3 4 5Time Complexity: O(N)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Types of Asymptotic Notations in Complexity Analysis of Algorithms A avdhutgharat Follow Improve Article Tags : Tree DSA Practice Tags : Tree Similar Reads Basics & PrerequisitesTime Complexity and Space ComplexityMany times there are more than one ways to solve a problem with different algorithms and we need a way to compare multiple ways. Also, there are situations where we would like to know how much time and resources an algorithm might take when implemented. To measure performance of algorithms, we typic 13 min read Types of Asymptotic Notations in Complexity Analysis of AlgorithmsWe have discussed Asymptotic Analysis, and Worst, Average, and Best Cases of Algorithms. The main idea of asymptotic analysis is to have a measure of the efficiency of algorithms that don't depend on machine-specific constants and don't require algorithms to be implemented and time taken by programs 8 min read Data StructuresGetting Started with Array Data StructureArray is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. Basic terminologies of ArrayArray Index: In an array, elements are identified by their indexes. Array index starts fr 14 min read String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut 2 min read Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The 2 min read Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List: 2 min read Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first 2 min read Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems 2 min read Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most 4 min read Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of 3 min read Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this 15+ min read AlgorithmsSearching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input 2 min read Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ 3 min read Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get 3 min read Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net 3 min read Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of 3 min read Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit 4 min read AdvancedSegment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree 3 min read Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i 2 min read GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br 2 min read Interview PreparationInterview Corner: All Resources To Crack Any Tech InterviewThis article serves as your one-stop guide to interview preparation, designed to help you succeed across different experience levels and company expectations. Here is what you should expect in a Tech Interview, please remember the following points:Tech Interview Preparation does not have any fixed s 3 min read GfG160 - 160 Days of Problem SolvingAre you preparing for technical interviews and would like to be well-structured to improve your problem-solving skills? Well, we have good news for you! GeeksforGeeks proudly presents GfG160, a 160-day coding challenge starting on 15th November 2024. In this event, we will provide daily coding probl 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding PlatformGeeksforGeeks Practice is an online coding platform designed to help developers and students practice coding online and sharpen their programming skills with the following features. GfG 160: This consists of most popular interview problems organized topic wise and difficulty with with well written e 6 min read Problem of The Day - Develop the Habit of CodingDo you find it difficult to develop a habit of Coding? If yes, then we have a most effective solution for you - all you geeks need to do is solve one programming problem each day without any break, and BOOM, the results will surprise you! Let us tell you how:Suppose you commit to improve yourself an 5 min read Like