×

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

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.

Perfect Binary Tree

Complete binary trees are an important and general topic in the concept – of tree data structures. Before discussing a complete binary tree, we need to know the concept of...

4 minutes read.

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Collision Resolution Techniques

Collision Resolution Techniques Collision in hashing In this, the hash function is used to compute the index of the array.The hash value is used to store the key in the hash table,...

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

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.

Introduction to 2D-Arrays

Two Dimensional Array Technical Definitions An array of arrays is a common definition for a two-dimensional array. A matrix is another name for a two-dimensional array. A matrix looks like a table...

3 minutes read.

Data structure: Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

Common Operations on various Data Structures

Data structures are ways to organise data in computer memory for quick and effective use. The storage of data uses a variety of data-structures. It is also possible to define...

7 minutes read.

Why is Binary Heap Preferred over BST for Priority Queue

A priority queue is a linear and ordered collection of elements in which each element has an attribute named priority and the priority attribute decides the order in which elements...

2 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

4 minutes read.

Doubly Linked List

Doubly Linked List Doubly linked list is another kind of Linked list. Doubly linked list contains two pointers for navigation. In this, we can traverse the list in both directions, either...

4 minutes read.

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the...

3 minutes read.

Length of longest palindrome in a linked list using O(1) extra space

Length of longest palindrome in a linked list using O(1) extra space In this problem, we need to find the length of the longest palindrome list that is present in given...

2 minutes read.

Lowest Common Ancestor in a Binary Tree

The lowest node in the tree that contains both n1 and n2 as descendants is the lowest common ancestor (LCA), and n1 and n2 are the nodes for which we...

11 minutes read.

Vertical Order Traversal of Binary Tree

Implementation #include <iostream> #include <vector> #include <map> using namespace std; // representing the primary model of a binary tree node. struct _nod { int ky; _nod *Lft, *Rt; }; // establishing a new function representing the new binary tree node. struct _nod*...

5 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 B tree?

What do you mean by B Tree in Data Structures? In the technological world, a B tree is simply a well-managed and coordinated tree and an integral part of the data...

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

Bitonical Sort

Arranging an unordered collecttion of things into asignificant order. •Comparision Based Model: Bubble Sort, Selection Sort -->Non-Comparison Based. Model: Bucket Sort or on the other hand a Count Sort Bitonic Sort: Bitonic sort Algorithm was made...

5 minutes read.