Open In App

Extended Binary Tree

Improve
Improve
Like Article
Like
Save
Share
Report

Extended binary tree is a type of binary tree in which all the null sub tree of the original tree are replaced with special nodes called external nodes whereas other nodes are called internal nodes 
 

Here the circles represent the internal nodes and the boxes represent the external nodes.
Properties of External binary tree 
 

  1. The nodes from the original tree are internal nodes and the special nodes are external nodes.
  2. All external nodes are leaf nodes and the internal nodes are non-leaf nodes.
  3. Every internal node has exactly two children and every external node is a leaf. It displays the result which is a complete binary tree.

Uses:

1. It is useful for representation in algebraic expressions. 

Below is an example of making an extended binary tree in C++ by making all the external nodes as ‘-1’ 
 

C++




// C++ program to make an extended binary tree
#include <bits/stdc++.h>
using namespace std;
 
// A Tree node
struct Node {
    int key;
    struct Node *left, *right;
};
 
// Utility function to
// create a new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return (temp);
}
 
// Function for inorder traversal
void traverse(Node* root)
{
    if (root != NULL) {
        traverse(root->left);
        cout << root->key << " ";
        traverse(root->right);
    }
    else {
 
        // Making external nodes
        root = newNode(-1);
        cout << root->key << " ";
    }
}
 
// Driver code
int main()
{
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(5);
    root->right->right = newNode(4);
 
    traverse(root);
 
    return 0;
}


Java




// Java program to make an extended binary tree
class GFG
{
     
// A Tree node
static class Node
{
    int key;
    Node left, right;
};
 
// Utility function to create a new node
static Node newNode(int key)
{
    Node temp = new Node();
    temp.key = key;
    temp.left = temp.right = null;
    return (temp);
}
 
// Function for inorder traversal
static void traverse(Node root)
{
    if (root != null)
    {
        traverse(root.left);
        System.out.print(root.key + " ");
        traverse(root.right);
    }
    else
    {
 
        // Making external nodes
        root = newNode(-1);
        System.out.print(root.key + " ");
    }
}
 
// Driver code
public static void main(String args[])
{
    Node root = newNode(1);
    root.left = newNode(2);
    root.right = newNode(3);
    root.left.left = newNode(5);
    root.right.right = newNode(4);
 
    traverse(root);
}
}
 
// This code is contributed by Arnab Kundu


C#




// C# program to make an extended binary tree
using System;
 
class GFG
{
         
    // A Tree node
    public class Node
    {
        public int key;
        public Node left, right;
    };
     
    // Utility function to create a new node
    static Node newNode(int key)
    {
        Node temp = new Node();
        temp.key = key;
        temp.left = temp.right = null;
        return (temp);
    }
     
    // Function for inorder traversal
    static void traverse(Node root)
    {
        if (root != null)
        {
            traverse(root.left);
            Console.Write(root.key + " ");
            traverse(root.right);
        }
        else
        {
     
            // Making external nodes
            root = newNode(-1);
            Console.Write(root.key + " ");
        }
    }
     
    // Driver code
    public static void Main()
    {
        Node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
        root.left.left = newNode(5);
        root.right.right = newNode(4);
     
        traverse(root);
    }
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
 
// Javascript program to make an extended binary tree
     
// A Tree node
class Node
{
    constructor()
    {
        this.key = 0;
        this.left = null;
        this.right = null;
    }
};
 
// Utility function to create a new node
function newNode(key)
{
    var temp = new Node();
    temp.key = key;
    temp.left = temp.right = null;
    return (temp);
}
 
// Function for inorder traversal
function traverse(root)
{
    if (root != null)
    {
        traverse(root.left);
        document.write(root.key + " ");
        traverse(root.right);
    }
    else
    {
 
        // Making external nodes
        root = newNode(-1);
        document.write(root.key + " ");
    }
}
 
// Driver code
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(5);
root.right.right = newNode(4);
traverse(root);
 
</script>


Python3




# Python 3 program to make an extended binary tree
 
# A Tree node
class Node :
    def __init__(self):
        self.key=-1
        self.left=self.right=None
 
 
# Utility function to
# create a new node
def newNode(key):
    temp = Node()
    temp.key = key
    temp.left = temp.right = None
    return temp
 
 
# Function for inorder traversal
def traverse(root):
    if (root != None) :
        traverse(root.left)
        print(root.key,end=" ")
        traverse(root.right)
     
    else :
 
        # Making external nodes
        root = newNode(-1)
        print(root.key,end=" ")
     
 
 
# Driver code
if __name__ == '__main__':
    root = newNode(1)
    root.left = newNode(2)
    root.right = newNode(3)
    root.left.left = newNode(5)
    root.right.right = newNode(4)
 
    traverse(root)
    print()
# This code was added by Amartya Ghosh


Output: 

-1 5 -1 2 -1 1 -1 3 -1 4 -1

 

Time Complexity: O(N). 
Auxiliary Space: O(N).  
Application of extended binary tree: 
 

  1. Calculate weighted path length: It is used to calculate total path length in case of weighted tree.

  1. Here, the sum of total weights is already calculated and stored in the external nodes and thus makes it very easier to calculate the total path length of a tree with given weights. The same technique can be used to update routing tables in a network.
  2. To convert binary tree in Complete binary tree: The above-given tree having removed all the external nodes, is not a complete binary tree. To introduce any tree as complete tree, external nodes are added onto it. Heap is a great example of a complete binary tree and thus each binary tree can be expressed as heap if external nodes are added to it.


Last Updated : 09 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads