Clicky

Go back to the programming category
Go back to previous page
Binary Tree
What is a Binary Tree and Why is it Important?

What is a Binary Tree and Why is it Important?

A Binary Tree is a type of tree in which each node has at most two children, referred to as the left and right child. The structure of a binary tree makes it efficient at organizing, storing data and for searching specific data within the tree.

How does a Binary Tree Work?

A binary tree works in the same way a normal tree does, it has a root node and left and right childs, each child can have it´s own child and so on...the only difference between them is that a binary tree can only have two children at most, while the tree can have more.

Advantages of Binary Trees

One of the main advantages of binary trees is that they can be efficient for searching data. Because each node has a unique parent and two children at most, it becomes possible to design algorithms that can take advantage of that to search in the tree effectively. This makes binary trees a very common structure on several different applications.

Another advantage of binary trees is that they can be easily balanced. Balancing a binary tree means changing the nodes so that the height of the left and right subtrees are roughly equal in case they are not already. This property also allows for creating algorithms that make it easier to search for the nodes in the tree.

Types of Binary Trees

There are several different types of binary trees. Some of the most common ones are:

  • Full Binary Tree: A full binary tree is a tree in which every node has either zero or two children.
  • Complete Binary Tree: A complete binary tree is a tree in which every level, except possibly the last, is completely filled and all nodes are as far left as possible.
  • Perfect Binary Tree: A perfect binary tree is a tree in which all leaves are at the same depth and all interior nodes have two children.
  • Degenerate Tree: A degenerate tree is a tree in which every internal node has only one child.

Applications of Binary Trees

Binary trees are used in:

  • Data Storage: Binary trees are an efficient method for organizing and storing data. They are often used in database systems to store and retrieve data.
  • File Systems: Binary trees are also used in file systems to organize and store files and directories.
  • Heaps: Heaps are used in several different algorithms and applications.

Python Binary Tree Implementation

class Node:
    def __init__(self, data):
        self.left = None
        self.right = None
        self.data = data

class BinaryTree:
    def __init__(self):
        self.root = None

    def insert(self, data):
        if self.root is None:
            self.root = Node(data)
        else:
            self._insert(data, self.root)

    def _insert(self, data, node):
        if data < node.data:
            if node.left is None:
                node.left = Node(data)
            else:
                self._insert(data, node.left)
        else:
            if node.right is None:
                node.right = Node(data)
            else:
                self._insert(data, node.right)

    def find(self, data):
        if self.root:
            return self._find(data, self.root)
        else:
            return None

    def _find(self, data, node):
        if data == node.data:
            return node
        elif data < node.data and node.left:
            return self._find(data, node.left)
        elif data > node.data and node.right:
            return self._find(data, node.right)

    def delete_value(self, data):
        if self.root:
            self.root = self._delete_value(data, self.root)

    def _delete_value(self, data, node):
        if data < node.data:
            node.left = self._delete_value(data, node.left)
        elif data > node.data:
            node.right = self._delete_value(data, node.right)
        else:
            if node.left is None and node.right is None:
                return None
            elif node.left is None:
                return node.right
            elif node.right is None:
                return node.left
            else:
                successor = self.find_min(node.right)
                node.data = successor.data
                node.right = self._delete_value(successor.data, node.right)
        return node

    def find_min(self, node):
        current = node
        while current.left is not None:
            current = current.left
        return current

Conclusion

The main difference between a binary tree and a normal tree is that they can only have up to 2 child nodes, which makes them very effective for several different applications such as data storage, file systems and heaps.