Building Java Programs - University of Washington

嚜濁uilding Java Programs

Binary Trees

reading: 17.1 每 17.3

Trees

? tree: A directed, acyclic structure of linked nodes.

每 directed : Has one-way links between nodes.

每 acyclic : No path wraps back around to the same node twice.

每 binary tree: One where each node has at most two

children.

root

? Recursive definition: A tree is either:

每 empty (null), or

每 a root node that contains:

?

?

?

1

data,

a left subtree, and

a right subtree.



(The left and/or right

subtree could be empty.)

2

4

3

5

6

7

2

Trees in computer science

? folders/files on a computer

? family genealogy; organizational charts

? AI: decision trees

? compilers: parse tree

每 a = (b + c) * d;

? cell phone T9

=

a

*

+

b

d

c

3

Terminology

? node: an object containing a data value and left/right

children

? root: topmost node of a tree

? leaf: a node that has no children

? branch: any internal node; neither the root nor a leaf

root

? parent: a node that refers to this one

height = 3

? child: a node that this node refers to

level 1

? sibling: a node with a common parent

? subtree: the smaller tree of nodes on

level 2

the left or right of the current node

? height: length of the longest path

from the root to any node

level 3

? level or depth: length of the path

from a root to a given node

4

1

2

3

5

6

7

4

A tree node for integers

? A basic tree node object stores data, refers to left/right

? Multiple nodes can be linked together into a larger tree

left

data right

42

left

data right

59

left

data right

27

left

data right

86

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download