×

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes are known as internal nodes.

In the extended binary tree, it is fixed that every internal node has two children. And it is also fixed that every external node is a leaf node. The result of the constructed binary tree is generally a Complete binary tree.

Extended Binary Tree

In the above diagram, the circle denotes the Internal nodes whereas the box denotes the external nodes.

Extended Binary Trees are useful for the algebraic expression representation.

External Binary Tree Properties:

  1. The special nodes are external nodes, whereas the nodes from the original tree are internal nodes.
  2. Internal nodes are non-leaf nodes, whereas all external nodes are leaf nodes.
  3. Every external node is a leaf, and every internal node has precisely two children. It gives the complete binary tree as the output.

Dealing with random values in the actual world is frequently impossible; the likelihood that you are dealing with non-random variables (such as sequential) results in largely skew trees, which brings us to the worst situation. So, using rotations, we balance the height of the binary tree.

Let’s see an example of making the extended binary tree.

Initially all the external nodes are marked as -1.

The JAVA implementation of constructing the extended binary tree.

// A JAVA Program for the construction of an Extended Binary Tree.
class ExtendedBinaryTreeDemo
{
     
// A Node is created for the binary tree.
static class Node
{
    int data;
    Node left, right;
};
 
// This method creates a new node in the binary tree.
static Node newNode (int data)
{
    Node temp_node = new Node ();
    Temp_node.data = data;
    Temp_node.left = temp_node.right = null;
    return (temp_node);
}
 
// method which demonstrates the Inorder traversal of a binary tree.
static void inordertraverse (Node root)
{
      //Checking if the root node is empty or not.
    if (root != null)
    {
        inordertraverse(root.left);
        System.out.print(root.data + " ");
        inordertraverse(root.right);
    }
    else
    {
         //If the given node is NULL, then external nodes are made.
        // The external nodes are indicated with -1.
        root = newNode(-1);
        System.out.print(root.data + " ");
    }
}
 


// The Driver program code for executing the above functions.


public static void main (String args [])
{
    Node root = newNode (10);
    root.left = newNode (20);
    root.right = newNode (30);
    root.left.left = newNode (50);
    root.right.right = newNode (40);
 
    inordertraverse (root);
}
}

OUTPUT:

-1 50 -1 2 -1 10 -1 30 -1 40 -1

The C++ implementation of constructing the extended binary tree.

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


// A Node is created for the binary tree.
 
struct Node {
    int data;
    struct Node *left, *right;
};
// This method creates a new node in the binary tree.
// This type of method is known as utility function.
 
Node* newNode (int data)
{
    Node* temp_Node = new Node;
    Temp_Node->data = data;
    Temp_Node->left = temp_Node->right = NULL;
    return (temp_Node);
}
 
// method which demonstrates the Inorder traversal of a binary tree.


void inordertraverse (Node* root)
{
    if (root != NULL) {
        inordertraverse(root->left);
        cout << root->data << " ";
        inordertraverse(root->right);
    }
    else {
 
        // Initialize the external nodes with -1 (Marker value).
        root = newNode(-1);
        cout << root->data << " ";
    }
}
 
// The Driver program code for executing the above functions.


int main ()
{
    Node* root = newNode (10);
    root->left = newNode (20);
    root->right = newNode (30);
    root->left->left = newNode (50);
    root->right->right = newNode (40);
 
    inordertraverse(root);
 
    return 0;
}

OUTPUT:

-1 50 -1 2 -1 10 -1 30 -1 40 -1

The Python implementation of constructing the extended binary tree.

# A program for constructing an Extended binary tree.

# A node for the given binary tree.
class Node:
    def __init__(self):
        self.data= -1
        self.left=self.right=None
 
 
#  The below fumction helps in creating a new node.
# This function is also called as utility function.
def newNode (data):
    temp_node = Node ()
    temp_node.data = data
    temp_node.left = temp_node.right = None
    return temp_node

# This Function produces the inorder traversal  for the given binary tree.

def inordertraverse (root):
    if (root != None):
        inordertraverse (root.left)
        print (root.data, end=" ")
        inordertraverse (root.right)
     
    else:
 
        # Initializing the external node with -1.
        root = newNode (-1)
        print (root.data, end=" ")

# Driver code for the execution of the above functions.

if __name__ == '__main__':
    root = newNode (10)
    root.left = newNode (20)
    root.right = newNode (30)
    root.left.left = newNode (50)
    root.right.right = newNode (40)
 
    inordertraverse (root)
    print ()

OUTPUT:

-1 50 -1 2 -1 10 -1 30 -1 40 -1

To construct the Extended Binary Tree, a time complexity of O(N) and space complexity of O(N) is required.

Extended Binary Tree Applications:

  • We can calculate the weighted path length easily. It calculates the total path length in case of a weighted tree.
Extended Binary Tree

The total path length can be easily calculated from the given extended binary tree.

Total Path Length is

P = 9*3 + 10*3 + 8*2 + 6*2 + 9*3 + 11*3

P = 145

It is much simpler to determine the overall path length of a tree with the supplied weights in this case since the entire weights' sum has already been determined and is kept in the external nodes. A network's routing tables can be updated using the same method.

  • Extended Binary tree is useful in converting the binary tree into a Complete Binary Tree. Even after all external nodes have been deleted, the tree in the example above is not a full binary tree. External nodes are added to any tree to make it a full tree. Each binary tree may be described as heap if extra nodes are added to it since heap is a fantastic illustration of a complete binary tree.

Related Topics

Binary Tree Uses

A binary tree is a tree data structure containing hubs with at most two children for instance a right and left child. The node at the top is insinuated as the...

3 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

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

Counts the number of times a given element occurs in a Linked List

Counts the number of times a given element occurs in a Linked List This article will explain how we can count the occurrences of a particular element in a list. Here,...

3 minutes read.

Linear Queue Data Structure in C

Data Structure There are many ways to store data in programming, that Queue has features that make it all the more special. We all know that data structure is a way...

9 minutes read.

Application of 2D array - Sparse Matrix

2D Arrays Application - Sparse Matrix A matrix is a two-dimensional data item consisting of m rows and n columns, with a total of m x n values. A sparse matrix...

7 minutes read.

What is a Height-Balanced Tree in Data Structure

A height-balanced tree is a type of binary tree. If the absolute difference between the heights of the left and right subtree is less than or equal to 1, then...

6 minutes read.

Operations on Queue in Data Structures

A queue is a linear structure where operations are done in a specific sequence. Queues are abstract data structures that are comparable to Stacks. A queue, unlike a stack, is...

8 minutes read.

Hashing and its Applications

Hashing Hashing refers to transforming plain text data in such a way that even if it is leaked for some reason, no one would be able to make sense of it....

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

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

6 minutes read.

Boruvkas algorithm

This algorithm is used for finding minimum spanning tree from a weighted graph. Like prim’s and kruskal’s algorithm it is also a greedy algorithm. Note:What is the minimum spanning tree?We know...

4 minutes read.

Singly Linked list

Singly Linked list A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed...

3 minutes read.

Symmetric binary tree

Implementation // writing a C++ program to check whether a given binary tree is symmetric or not. #include <bits/stdc++.h> using namespace std; // creating a binary tree node. struct __Nod { int ky; struct __Nod *Lft, *Rt; }; //...

4 minutes read.

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

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.

Bubble Sort in Data Structures

Bubble Sort in C++ The bubble sort algorithm analyses two adjacent elements and swaps them until they are no longer in the desired order. Each iteration moves each member of the array...

4 minutes read.

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array You have been given an array of numbers. You have to...

4 minutes read.

Sorting Algorithms in Data Structures

A sorting algorithm is used to organize the elements of an array or list. Sorting an array, for example. Unsorted array 572941 Sorted array 124579 We're sorting the array in ascending order right now. This procedure...

4 minutes read.

Spanning Tree

Spanning Tree: The spanning tree is a subset of the graph. It is a non-cyclic graph. If any node in the spanning tree is truncated, the entire graph fails. There are...

10 minutes read.