16.BinaryTrees-11

# 16.BinaryTrees-11 - The Idea of a Tree One of the problems...

This preview shows pages 1–4. Sign up to view the full content.

The Idea of a Tree One of the problems we had with linked lists was that it took a long time to get to an arbitrary node of a linked list. It would be nice if we could have a linked structure were nodes were more accessible. If you think of a tree with branches, and each point where branches intersect as a node, including leaves, you find a structure with a huge number of nodes, but one where the path to any one particular node is not too long. An upside down picture of that sort of tree might look like this:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Binary Trees Now, what we could do with that picture above is make each of the branches of the tree a pointer to a node, and each node contain a data value along with several pointers to other nodes. To simplify things, we will limit the number of pointers to other nodes to be 2 or less, and also not allow any “circular” references. (These are when you follow a set of pointers from node to node and eventually get back to the node you started from.) Any linked structure that fits these requirements is a binary tree. Here is a picture of a binary tree:
Structure to Declare a Node in a Binary Tree Pictorially, a node of a binary tree looks very similar to a node in a linked list, except instead of having 1 field as a pointer field, we should have 2 fields as pointer fields. Hence, we could declare a node in a binary tree as follows:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

16.BinaryTrees-11 - The Idea of a Tree One of the problems...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online