15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

388<br />

Taking Action on Trees<br />

10<br />

8<br />

12<br />

4<br />

9<br />

11<br />

19<br />

10<br />

8<br />

Figure 5-13:<br />

After<br />

deleting<br />

data from<br />

a tree, you<br />

may need to<br />

rearrange<br />

the<br />

remaining<br />

data to keep<br />

the tree<br />

sorted.<br />

4 9 11 19<br />

10<br />

8<br />

11<br />

4 9<br />

19<br />

After pruning a sub-tree, there may be more than one way to rearrange the<br />

remaining nodes.<br />

Adding or grafting a sub-tree to an existing tree can cause problems if the<br />

sub-tree data contains different values than the original tree. In that case,<br />

you can’t just graft the sub-tree to the existing tree; you have to rearrange<br />

the data in both the tree and the grafted sub-tree to sort data once more, as<br />

shown in Figure 5-15.<br />

Tree data structures are most useful <strong>for</strong> storing and sorting data, such as <strong>for</strong><br />

databases. However, tree data structures are also handy <strong>for</strong> creating artificial<br />

intelligence in games, such as chess.

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

Saved successfully!

Ooh no, something went wrong!