×

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

Difference between complete and full binary tree

As we all know that the  binary tree is a tree it contains one or two children at each other node. It contains two children's nodes in the Binary tree. The...

6 minutes read.

Red Black Tree vs AVL Tree: Data Structure

Difference Between Red Black Tree vs AVL Tree Red Black Tree: A red-black tree is referred as self-balancing binary search tree. In red-black, each node stores an extra bit that determines...

4 minutes read.

Given a Binary Tree Check the Zig-Zag Traversal

Implementation // The C++ implementation of the zig-zag traversal method in the O(n) time.  #include <iostream> #include <stack> using namespace std; // creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a...

4 minutes read.

Insertion Sort vs Bubble Sort

In this article, we will see the major differences between Insertion Sort and Bubble Sort. Before that, let’s have a quick overview of what these sorting algorithms are and what’s...

4 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Stack Data Structure

The stack is a non-primitive and linear data structure. It works on the principle of LIFO (Last In First Out). That is, the element that is added to the end...

3 minutes read.

Strings in Data Structures

Strings and functions in C A string is a collection of characters. We'll learn how to declare strings, operate with strings in C programming, and use pre-defined string handling routines. We'll look...

7 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Function to Create a Copy of Binary Search Tree

Implementation // creating a new hashmap in the language C++ that will help us clone a binary tree with arbitrary pointers.  #include<iostream> #include<unordered_map> using namespace std; /* A given binary tree has a record, a...

9 minutes read.

Bubble Sort vs Quick Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Quick Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Write Main Difference Between Tree and Graph in Data Structures

Graph: The graph has two sets, which are considered V and E. These vertices are also called nodes, and edges are referred to as arcs connecting any two nodes in a...

4 minutes read.

Finding the Minimum and Maximum Value of a Binary Tree

Implementation // Writing a C++ program that will help us find out the maximum and the minimum in a binary tree.  #include <bits/stdc++.h> #include <iostream> using namespace std; // creating a new class tree node. class...

5 minutes read.

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...

3 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.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.

Stack Using Array

Stack – A Stack is a linear abstract data type used to store elements. It is also called last in first out or first in last out data structure because...

6 minutes read.

What is an AVL Tree in Data Structure?

AVL tree stands for (Adelson, Velskii, & Landis Tree) Data structure Data management is called database management. A data model is a system used to store, manage, and optimize computer resources. Data...

4 minutes read.

Tim Sort

Tim Sort is a mixture stable arranging calculation that exploits normal examples in information, and uses a mix of an improved Merge sort and Binary Insertion sort alongside an interior...

6 minutes read.

Array Data Structure

Data Structure Array: The array is a non-primitive and linear data structure that is a group of similar data items. That is, it can store only one type of data....

6 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.