×

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known as the external nodes. On the other hand, all the other nodes are known as internal nodes. One exception is that the interchanging of nodes can happen at any place except for the root nodes. An extended binary tree becomes a strictly extended binary tree with either zero or two children at each end. We will see some examples of extended binary trees in this article and try to understand their concepts.

Implementation

In this section, we will see the implementation of the extended binary Tree and understand its works. let us proceed: -

#include <bits/stdc++.h>
using namespace std;


// Creating a tree node
struct _nod {
	int ky;
	struct _nod *Lft, *Rt;
};


// Creating a utility function to create a new node.
_nod* Nw_nod(int ky)
{
	_nod* temp = new _nod;
	temp->ky = ky;
	temp->Lft = temp->Rt = NILL;
	return (temp);
}


// Creating a function that will traverse in an in-order pattern.
void traverse(_nod* root)
{
	if (root != NILL) {
		traverse(root->Lft);
		cout << root->ky << " ";
		traverse(root->Rt);
	}
	else {


		// creating external nodes.
		root = Nw_nod(-1);
		cout << root->ky << " ";
	}
}


// writing the main code.
int main()
{
	_nod* root = Nw_nod(1);
	root->Lft = Nw_nod(2);
	root->Rt = Nw_nod(3);
	root->Lft->Lft = Nw_nod(5);
	root->Rt->Rt = Nw_nod(4);


	traverse(root);


	return 0;
}

Output:

Extended Binary Tree

Example 2)

using System;


class S
{	
	// creating a new tree node.
	public class _nod
	{
		public int ky;
		public _nod Lft, Rt;
	};
	
	// Creating a utility function to create a new node.
	static _nod Nw_nod(int ky)
	{
		_nod temp = Nw_nod;
		temp.ky = ky;
		temp.Lft = temp.Rt = NILL;
		return (temp);
	}
// Creating a function that will traverse in an inorder pattern.	
	static void traverse(_nod root)
	{
		if (root != NILL)
		{
			traverse(root.Lft);
			Console.Write(root.ky + " ");
			traverse(root.Rt);
		}
		else
		{
	
			// Making external _nods
			root = Nw_nod(-1);
			Console.Write(root.ky + " ");
		}
	}
	
	// writing the main code.
	public static void Main()
	{
		_nod root = Nw_nod(1);
		root.Lft = Nw_nod(2);
		root.Rt = Nw_nod(3);
		root.Lft.Lft = Nw_nod(5);
		root.Rt.Rt = Nw_nod(4);
	
		traverse(root);
	}
}

Output:

Extended Binary Tree

Example 3)

class S
{
	// creating a new tree node.
static class _nod
{
	int ky;
	_nod Lft, Rt;
};
// Creating a utility function to create a new node.
static _nod Nw_nod(int ky)
{
	_nod temp = Nw_nod;
	temp.ky = ky;
	temp.Lft = temp.Rt = NILL;
	return (temp);
}
// Creating a function that will traverse in an inorder pattern.
static void traverse(_nod root)
{
	if (root != NILL)
	{
		traverse(root.Lft);
		System.out.print(root.ky + " ");
		traverse(root.Rt);
	}
	else
	{


	//creating external nodes in the tree.
		root = Nw_nod(-1);
		System.out.print(root.ky + " ");
	}
}


// writing the main code.
public static void main(String args[])
{
	_nod root = Nw_nod(1);
	root.Lft = Nw_nod(2);
	root.Rt = Nw_nod(3);
	root.Lft.Lft = Nw_nod(5);
	root.Rt.Rt = Nw_nod(4);


	traverse(root);
}
}

Output:

Extended Binary Tree

Example 4)

<script>
// creating a new tree node.
class _nod
{
	constructor()
	{
		this.ky = 0;
		this.Lft = NILL;
		this.Rt = NILL;
	}
};
// Creating a utility function to create a new node.
function Nw_nod(ky)
{
	var temp = Nw_nod;
	temp.ky = ky;
	temp.Lft = temp.Rt = NILL;
	return (temp);
}
// Creating a function that will traverse in an inorder pattern.	
function traverse(root)
{
	if (root != NILL)
	{
		traverse(root.Lft);
		document.wr(root.ky + " ");
		traverse(root.Rt);
	}
	else
	{
	//creating external nodes in the tree.
		root = Nw_nod(-1);
		document.wr(root.ky + " ");
	}
}
// writing the main code.
var root = Nw_nod(1);
root.Lft = Nw_nod(2);
root.Rt = Nw_nod(3);
root.Lft.Lft = Nw_nod(5);
root.Rt.Rt = Nw_nod(4);
traverse(root);


</script>

Output:

Extended Binary Tree

Example 5)

# Creating a new tree node.
Class _nod :
	def __init__(self):
		self.ky=-1
		self.Lft=self.Rt=None
// Creating a utility function to create a new node.
def Nw_nod(ky):
	temp = _nod()
	temp.ky = ky
	temp.Lft = temp.Rt = None
	return temp
// Creating a function that will traverse in an inorder pattern.	
def traverse(root):
	if (root != None) :
		traverse(root.Lft)
		print(root.ky,end=" ")
		traverse(root.Rt)
	
	Else:


	//creating external nodes in the tree.
		root = Nw_nod(-1)
		print(root.ky,end=" ")
	
// writing the main code.
if __name__ == '__main__':
	root = Nw_nod(1)
	root.Lft = Nw_nod(2)
	root.Rt = Nw_nod(3)
	root.Lft.Lft = Nw_nod(5)
	root. Rt.Rt = Nw_nod(4)


	traverse(root)
	print()

Output:

Extended Binary Tree

Related Topics

What is Skewed Binary Tree

To understand the skewed binary tree, we must first understand the concept of a binary tree. A binary is generally the one in which every single node has two further...

3 minutes read.

Data Structures Algorithms

What is an Algorithm? An algorithm is a sequence of steps used to complete a job or get a desired result. It is similar to programming building elements that let cell...

4 minutes read.

Function to Delete a Leaf Node from a Binary Tree

Implementation // We are writing a C++ code to eliminate all the leaves from the given value.  #include <bits/stdc++.h> using namespace std; // creating a new binary tree node struct __nod { int record; struct __nod *Lft,...

4 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Dijkstra’s vs Bellman-Ford Algorithm

The Dijkstra Algorithm One of the SSSP (Single Source Shortest Path) algorithms is Dijkstra's. As a result, it finds the shortest path between a source node and all other nodes in...

7 minutes read.

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.

Serialize and Deserialize a Binary Tree

Implementation // Writing a C++ program to check the serialization and deserialization of binary tree.   #include <iosstream> /* A binary tree node contains a key and a pointer to the left and right...

4 minutes read.

What Is Dfs Algorithm in Data Structures

DFS stands for Depth First Search. Generally, it is a repetitive or decidable type of algorithm which is basically used in identifying all the vertices or nodes of a graph...

5 minutes read.

Depth of binary tree

We all know that a binary tree is a kind of tree that helps us maintain the order and balance of the tree. It is a type of tree in...

4 minutes read.

Linear vs Circular Queue: Data Structure

Difference Between Linear and Circular Queue What is Linear Queue? A linear queue is linear data structure which works on first in first out principle. We can say a linear queue is...

3 minutes read.

Check if a Singly Linked List is Palindrome

Check if a Singly Linked List is Palindrome In this section, we have given a singly linked list, and we need to check whether the given list is a palindrome. Example:           1...

3 minutes read.

Bin Packing Problem (How to minimize the number of used Bins)

You have been given an array. The values of the array represent the size of n different items. You have been also given some bins. You have to store the...

3 minutes read.

Bubble Sort vs Selection Sort

In this article, we will discuss the basic differences between these two sorting algorithms. Let us have a quick overview of what these sorting algorithms are? And what are the...

6 minutes read.

Remove duplicates from an unsorted Linked List

Remove duplicates from an unsorted Linked List This article will explain how we can remove duplicates from unsorted linked lists. Here we have given an unsorted singly linked list and will...

3 minutes read.

How to get Better in Data Structures and Algorithms?

Introduction Data structures and algorithms are fundamental computer science concepts that store, organize, and process data efficiently. By understanding different data structures and algorithms and using them effectively, you can become...

19 minutes read.

Trim a binary search tree

Implementation //writing a C++ program will help us eliminate the keys that are out of the league.  #include<bits/stdc++.h> using namespace std; //we are now creating a binary search tree node consisting of key left...

8 minutes read.

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure...

4 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.

Delete a Node without head pointer from the linked list

Delete a Node without head pointer from the linked list This article will explain how to delete a node without a head pointer from the linked list. We have given a...

2 minutes read.

Heap Sort in Data Structure

Heap Sort: Heap Sort is very useful and efficient sorting algorithm in data structure. We can say it is a comparison base sorting algorithm, similar sort where we will find...

2 minutes read.