EU
  
You are currently viewing the European Union version of the site.
Would you like to switch to your local site?
14 MIN READ TIME
PYTHON

Working with binary tree data structures

Mihalis Tsoukalos explains how to construct and use binary trees for faster searches and easier relationships (of the data sort, mind).

OUR EXPERT

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

QUICK TIP

MAKING SENSE OF HASH TABLES

QUICK TIP

LINKED LISTS

OUR EXPERT

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.

Linked lists are popular in computer science because of their simplicity, versatility and ease of use.

A hash table is a data structure that stores one or more key and value pairs. It can store keys of any type and uses a hash function to compute an index into an array of buckets or slots, from which a value can be found. Ideally, the hash function will assign each key to a unique bucket. Unfortunately, this rarely happens. Therefore, the most crucial feature of the hash function is that it should produce a uniform distribution of the hash values.

There’s no point in sorting a binary tree because trees are sorted by default in their own way. This special sorting depends on the way new nodes are inserted in the binary tree. What’s important is, when possible, to have our binary trees balanced to speed up their operations.

Strictly speaking, a linked list is a structure with a finite set of elements. Each element uses at least two memory locations: one for the data and the other for a pointer that links the current element to the next one in the sequence of elements that construct the linked list. This is also known as a singly (or single) linked list.

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

Unlock this article and much more with
You can enjoy:
Enjoy this edition in full
Instant access to 600+ titles
Thousands of back issues
No contract or commitment
Try for €1.09
SUBSCRIBE NOW
30 day trial, then just €11,99 / month. Cancel anytime. New subscribers only.


Learn more
Pocketmags Plus
Pocketmags Plus

This article is from...


View Issues
Linux Format
April 2022
VIEW IN STORE

Other Articles in this Issue


WELCOME
MEET THE TEAM
This issue we’re compiling the Linux kernel from source. We wondered if our experts compile anything from source, or does even the thought of this cause them to run screaming into the night?
Kernel of truth
Compiling the kernel from source was always a
REGULARS AT A GLANCE
Linux malware grows by 35%
Malware aimed directly at Linux systems increased drastically last year
Major Linux exploit found
Almost every distro is affected by a major vulnerability – make sure that your systems are patched
US government wants your messages
And the UK government wants to confirm IDs online
IT’S COOL
Michael Meeks is general manager at Collabora Productivity
OLD AND UNTRUSTED
Keith Edmunds is MD of Tiger Computing Ltd,
Framework open sources its firmware
Users can change how the modular laptop manages its hardware
FreeCAD Project Association formed
New legal non-profit association set up in Belgium
LibreOffice 7.3 Community now out
New features in the latest version of the office suite
Distro watch
What’s down the side of the free software sofa?
TRUST ME?
Matt Yonkovit Head of open source strategy and
MEASURE THE PULSE
Jon Masters has been involved with Linux for
KERNEL WATCH
Jon Masters summarises the latest happenings in the Linux kernel, because someone has to…
Answers
Got a burning question about open source or the kernel? Whatever your level, email it to lxf.answers@futurenet.com
Mailserver
WRITE TO US Do you have a burning
Hot Picks
THE BEST NEW OPEN SOURCE SOFTWARE ON THE PLANET
REVIEWS
Intel Core i5 12400
It’s the new Q6600, Dave James’ greatest compliment ever!
ArchLabs 2022.02.12
Mayank Sharma has a soft spot for new Arch-based distros, and this time around he’s found one that tickles his fancy
UBOS Linux
Mechanisms for simplifying the installation of popular web tools are like snake oil. Or so thought Mayank Sharma, until he came across UBOS…
Slackware 15.0
He’s no masochist… until there’s a new Slackware release, when Mayank Sharma puts himself through all kinds of pains to relive the good ol’ days
Valve Steam Deck
Our PCGamer friends got a hands-on preview hardware of the Steam Deck before the big launch at the end of February. Here’s what they learnt…
Ubuntu alternatives
WE COMPARE TONS OF STUFF SO YOU DON’T HAVE TO!
The Verdict
Ubuntu alternatives
BUILD THE KERNEL
The kernel is what makes Linux tick. Jonni Bidwell is happy to get his hands dirty and help you tune up those ticks…
Grasp the kernel basics
Just what is a kernel and why is it telling my computer what to do?
Compiling a kernel
Get straight to business and build your own Ubuntu-esque kernel
Kernel minification
Perfection is reached not when there’s nothing left to add, but rather when there’s nothing left to take away…
Popular patches
Forget trawling through configs – use a pre-rolled patchset to set the rules
Pi USER
The maker’s Coolest Projects goes Global!
The once US-centric event is opening its doors to makers from around the world. Yes, that means you!
FLIRC Pi Zero Case
Les Pounder keeps his cool with a case for his beloved Raspberry Pi Zero 2 W. Will it beat the heat of a benchmark?
Assemble a Micro Dot pHAT news ticker
Les Pounder builds his own homage to the famous Times Square news ticker, this time scaled for ants!
Create your own Chromecast device
Mats Tage Axelsson takes you on a tour of the Linux tools that will enable you to stream media between your laptop and other devices
IN-DEPTH
LINUX FROM SCRATCH
Like to get your hands dirty? Aaron Peters is your man, as we get deep into building Linux
TUTORIALS
Rapid fuzzy finder
Shashank Sharma isn’t one for magic, but he’s not averse to using the Accio spell or its computing incarnation, fuzzy finder, to find things quickly
Store and search your research notes
Always on the look-out for organisational tools, Nick Peers reveals how to bring all your research materials and notes into a single convenient space
May the forth be with the Jupiter Ace
Les Pounder nips back to the early 80s and pays homage to a home computer that sold less than 6,000 units, despite its go-faster stripes!
Make your home as smart as possible
Matt Holder, who’s a bit of a clever-clogs himself, investigates the usage of Home Assistant to make your home as smart as it can be
Publishing your own slick ebooks
Seeking to chalk up a best seller, Michael Reed goes further into the intricacies of publishing a book from his Ubuntu desktop
Create your own virtual classroom
David Rutland moodles along with classroom management software on the LXF virtual private server and starts his own online course
TOP OF THE FOSS
CHARITABLE CHARACTERS
The Emmabuntüs collective enlightens Jonni Bidwell as to its kind and open source efforts
CODING ACADEMY
How to use Mojolicious for web scraping
Mark Gardner reveals how you can retrieve and parse HTML and XML from websites with a few lines of Perl and the Mojolicious framework
Interact with your 3D game environment
Enhance your gaming world by adding collision detection and custom objects, as Andrew Smith throws barrels at you
Chat
X
Pocketmags Support