11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

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

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

5Binary TreesThe list represent<strong>at</strong>ions of Chapter 4 have a fundamental limit<strong>at</strong>ion: Either searchor insert can be made efficient, but not both <strong>at</strong> the same time. Tree structurespermit both efficient access <strong>and</strong> upd<strong>at</strong>e to large collections of d<strong>at</strong>a. Binary trees inparticular are widely used <strong>and</strong> rel<strong>at</strong>ively easy to implement. But binary trees areuseful for many things besides searching. Just a few examples of applic<strong>at</strong>ions th<strong>at</strong>trees can speed up include prioritizing jobs, describing m<strong>at</strong>hem<strong>at</strong>ical expressions<strong>and</strong> the syntactic elements of computer programs, or organizing the inform<strong>at</strong>ionneeded to drive d<strong>at</strong>a compression algorithms.This chapter begins by presenting definitions <strong>and</strong> some key properties of binarytrees. Section 5.2 discusses how to process all nodes of the binary tree in anorganized manner. Section 5.3 presents various methods for implementing binarytrees <strong>and</strong> their nodes. Sections 5.4 through 5.6 present three examples of binarytrees used in specific applic<strong>at</strong>ions: the Binary Search Tree (BST) for implementingdictionaries, heaps for implementing priority queues, <strong>and</strong> Huffman coding trees fortext compression. The BST, heap, <strong>and</strong> Huffman coding tree each have distinctivestructural fe<strong>at</strong>ures th<strong>at</strong> affect their implement<strong>at</strong>ion <strong>and</strong> use.5.1 Definitions <strong>and</strong> PropertiesA binary tree is made up of a finite set of elements called nodes. This set eitheris empty or consists of a node called the root together with two binary trees, calledthe left <strong>and</strong> right subtrees, which are disjoint from each other <strong>and</strong> from the root.(Disjoint means th<strong>at</strong> they have no nodes in common.) The roots of these subtreesare children of the root. There is an edge from a node to each of its children, <strong>and</strong>a node is said to be the parent of its children.If n 1 , n 2 , ..., n k is a sequence of nodes in the tree such th<strong>at</strong> n i is the parent ofn i+1 for 1 ≤ i < k, then this sequence is called a p<strong>at</strong>h from n 1 to n k . The lengthof the p<strong>at</strong>h is k − 1. If there is a p<strong>at</strong>h from node R to node M, then R is an ancestorof M, <strong>and</strong> M is a descendant of R. Thus, all nodes in the tree are descendants of the145

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

Saved successfully!

Ooh no, something went wrong!