Linux Format

Working with binary tree data structures

OUR EXPERT

Mihalis Tsoukalos is a systems engineer and a technical writer. You can reach him at www.mtsoukalos.eu and @mactsouk.

The main benefit of using a binary tree or a tree in general is that you can quickly find out if an element is present or not, compared to performing a linear search. Trees are also good at modelling relationships and hierarchical data. Let’s begin by discussing the basics of binary trees before we start implementing binary trees in Python.

Get to grips with the basic concepts

Strictly speaking, a tree is a directed acyclic graph that satisfies the following three principles. First, it has a root node that’s the entry point to the tree. Second, every vertex, except the root, has only one entry point. Third, a path connects the root with each vertex. A directed graph features edges that have a direction associated with them. A directed acyclic graph is a directed graph that lacks cycles.

As already stated, the root of a tree is the first node of the tree. Each node can be connected to one or more nodes depending on the tree type. If each node leads to one and only one node, then the tree becomes a linked list. A leaf node is a node without any children. Leaves are also called external nodes, whereas a node with at least one child is called an internal node.

A binary tree is a specific type of tree where underneath each node there exist at most two more nodes. ‘At most’ means that it can be connected to one, two or no other node. The depth of a tree,

You’re reading a preview, subscribe to read more.

More from Linux Format

Linux Format1 min read
Ultimate Desktop Upgrade!
LXF316 will be on sale Tuesday 28th May 2024 Word processors that can help craft that novel you’ve always been talking about and organise large projects. Revive the old roleplaying system for a digital age as we recreate our own play-by-mail gaming
Linux Format4 min read
Linux
The #1 open source mag Future Publishing Limited, Quay House, The Ambury, Bath, BA1 1UA Email contact@linuxformat.com EDITORIAL Editor-in-chief Neil Mohr Art editor Fraser McDermott Production editor Katharine Davies Group editor-in-chief Graham Bar
Linux Format2 min read
Platform Support And Editions
Kali has a huge variety of install options, and these include images for ARM-based computers and pre-made virtual K machine images for most of the popular virtualisers. It’s even possible to install a version of Kali on to an Android-powered device.

Related Books & Audiobooks