Full Binary Trees
A Full Binary Tree is a specific type of binary tree where every node has either 0 or 2 children. It is a highly structured tree and is an important concept in computer science. Full binary trees are used in various algorithms and applications, including heap-based data structures and expression evaluation.
A Full Binary Tree (also known as a Proper Binary Tree or 2-Tree) is a binary tree where every node, except the leaves, has exactly two children. This means:
In comparison to other binary trees:
Here’s a visual representation of a full binary tree:
1
/ \
2 3
/ \ / \
4 5 6 7
In the tree above:
In a full binary tree, every non-leaf node has exactly two children. Insertion is generally performed by adding a new node as a child of the first available parent node. This method ensures the binary tree maintains its full structure.
However, since the full binary tree requires each internal node to have two children, ensuring the tree stays full while inserting can involve more complex balancing mechanisms (like in AVL trees).
For insertion in a perfectly filled binary tree, a node will be inserted at the first available position on the last level.
Traversal is a common operation where we visit all nodes of the tree in a specific order. The main types of tree traversal are:
Deletion in a full binary tree involves removing the leaf node or the deepest node in the tree. If a non-leaf node is deleted, its children are promoted to maintain the tree's structure.
Below is an example of implementing a Full Binary Tree in Python. The tree class supports insertion and traversal.
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.value = key
class FullBinaryTree:
def __init__(self):
self.root = None
def insert(self, root, key):
if root is None:
return Node(key)
# First, insert the new node in the left or right subtree
queue = [root]
while queue:
temp = queue.pop(0)
# If left child is None, insert the node there
if not temp.left:
temp.left = Node(key)
return root
else:
queue.append(temp.left)
# If right child is None, insert the node there
if not temp.right:
temp.right = Node(key)
return root
else:
queue.append(temp.right)
def inorder_traversal(self, root):
if root:
self.inorder_traversal(root.left) # Visit left subtree
print(root.value, end=" ") # Visit root
self.inorder_traversal(root.right) # Visit right subtree
def preorder_traversal(self, root):
if root:
print(root.value, end=" ") # Visit root
self.preorder_traversal(root.left) # Visit left subtree
self.preorder_traversal(root.right) # Visit right subtree
# Example Usage
full_tree = FullBinaryTree()
full_tree.root = full_tree.insert(full_tree.root, 1)
full_tree.insert(full_tree.root, 2)
full_tree.insert(full_tree.root, 3)
full_tree.insert(full_tree.root, 4)
full_tree.insert(full_tree.root, 5)
full_tree.insert(full_tree.root, 6)
full_tree.insert(full_tree.root, 7)
print("Inorder Traversal of Full Binary Tree:")
full_tree.inorder_traversal(full_tree.root)
print("\nPreorder Traversal of Full Binary Tree:")
full_tree.preorder_traversal(full_tree.root)
Inorder Traversal of Full Binary Tree:
4 2 5 1 6 3 7
Preorder Traversal of Full Binary Tree:
1 2 4 5 3 6 7