15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

384<br />

Taking Action on Trees<br />

Figure 5-10:<br />

In a B-tree,<br />

all leaf<br />

nodes<br />

appear at<br />

the same<br />

level.<br />

In an ordinary tree, leaf nodes can appear<br />

on different levels.<br />

In a B-tree, all leaf nodes appear on the<br />

same level.<br />

When you add or subtract data, the binary tree constantly adjusts to keep all<br />

leaf nodes at the same level. Keeping all leaf nodes at the same level ensures<br />

that searching <strong>for</strong> some data (stored farther down a tree) won’t take a long<br />

time compared to searching <strong>for</strong> other data (stored closer to the root node of<br />

the tree).<br />

A variation of a B-tree is a B+ tree. The main difference is that a B-tree can<br />

store data in all its nodes. Because some nodes appear closer to the root<br />

node than others, this makes searching <strong>for</strong> data closer to the root node fast<br />

but searching <strong>for</strong> data farther from the root node slower in comparison.<br />

A B+ tree only stores data in its leaf nodes and connects all leaf nodes<br />

together as a linked list. Because all leaf nodes are the same distance from<br />

the root node, this makes searching <strong>for</strong> any type of data equal.<br />

Microsoft Windows and Linux are two operating systems that use B+ trees<br />

<strong>for</strong> keeping track of files on a disk.<br />

Taking Action on Trees<br />

Trees are flexible data structures because they organize data and allow fast<br />

retrieval of that data. Some of the different actions you can per<strong>for</strong>m on trees<br />

include<br />

✦ Searching <strong>for</strong> a specific item<br />

✦ Adding a new node or a sub-tree<br />

✦ Deleting data or a sub-tree

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!