12

I understand the structure of binary trees and how to traverse them. However, I am struggling to realize their actual uses, purposes in programs and programming. When I think about 'real life' examples of hierarchical data they almost certainly have more than 2 children. For example, in a family tree, a mother may often have more than two children.

Are 'binary trees' really only useful to store linearly related data due to the faster processing times over arrays and lists? Alternatively, do they serve a specific purpose in storing hierarchical data? If so, what examples are there of the application of binary trees. What data is such that a node has at most 2 children?

durron597
  • 7,610
  • 10
  • 39
  • 67
sw123456
  • 249

3 Answers3

26

No, binary trees are not for storing hierarchical data in the sense you're thinking of. The primary use case for n-ary trees, where n is a fixed number, is fast search capability, not a semantic hierarchy.

Remember the old game where one person thinks of a number between 1 and 100, and the other has to guess it in as few guesses as possible, and if you guess wrong the person thinking of the number has to tell you if you're too high or too low? It gets boring after a while because you quickly figure out that you should always start at 50, then go to 25 or 75, and keep dividing the range to be searched in half with each new guess after that, and eventually you can guess any number in at most 7 guesses, guaranteed.

It may not make for a fun game, but that property is what makes binary (and other n-ary) trees useful: you can use them to search a very large data set in a very small amount of time.

Mason Wheeler
  • 83,213
3

Any tree structure, where a node can have unlimited numbers of children, can be implemented using a binary tree.

For each node in your tree, replace it with a node with a right and left pointer. The left pointer goes to the first of the node's children. The right node goes to the next sibling of the node. All the children of a given node are in a linked list joined by their right pointers, with the head of the list pointed to by the left pointer of their parent.

Your complicated, n-ary tree has become a simple, binary tree.

I am sure this is in Knuth, Vol. 1 somewhere.

Justsalt
  • 141
2

Binary trees why use them?

In programming you work a lot with collections of same type data.

The two basic ways of storing this data are : linked lists and arrays.

They both come with up and downsides: In a linked list it's easy to add elements at any position or remove elements. But access to a specific element is harder, because you have to go through the list until you're at the element you want.

  • It doesn't search efficiently but inserting and deleting is easy.

With an array access to a specific element is easy, but it's harder to insert or delete an element because inserting means: extend array by one, shift all elements before the insert position 1 to the right and insert the element.

  • It searches efficiently (if sorted) but inserting and deleting is hard.

So both the linked list and the array have downsides.

Binary trees are made to tackle both problems of the array and the linked list:

  1. Easy insert and delete
  2. Easy searching

So binary tree are made for when you have lots of data which changes regularly.

Pieter B
  • 13,310