📜  扩展二叉树

📅  最后修改于: 2022-05-13 01:57:17.523000             🧑  作者: Mango

扩展二叉树

扩展二叉树是一种二叉树,其中原始树的所有空子树都替换为称为外部节点的特殊节点,而其他节点称为内部节点

这里的圆圈代表内部节点,方框代表外部节点。
外部二叉树的性质

  1. 原始树中的节点是内部节点,特殊节点是外部节点。
  2. 所有外部节点都是叶子节点,内部节点是非叶子节点。
  3. 每个内部节点都有两个孩子,每个外部节点都是一个叶子。它显示的结果是一个完整的二叉树

下面是在 C++ 中通过将所有外部节点设为“-1”来制作扩展二叉树的示例

C++
// C++ program to make an extended binary tree
#include 
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


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


输出:
-1 5 -1 2 -1 1 -1 3 -1 4 -1

时间复杂度:O(N)。
辅助空间:O(N)。
扩展二叉树的应用:

  1. 计算加权路径长度:用于计算加权树的总路径长度。

  1. 在这里,总权重的总和已经计算并存储在外部节点中,因此可以更容易地计算给定权重的树的总路径长度。相同的技术可用于更新网络中的路由表。
  2. 将二叉树转换为完全二叉树:上面给出的去除了所有外部节点的树不是完全二叉树。要将任何树作为完整树引入,需要在其上添加外部节点。堆是完全二叉树的一个很好的例子,因此如果向其中添加外部节点,每个二叉树都可以表示为堆。