Chapter13 - Chapter 13 Trees The diagram below gives the...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 13 – Trees The diagram below gives the general idea of a tree. Trees are like linked lists in that we start somewhere and then follow links. The difference is that each node now tells us where to find multiple other nodes. Trees have their own vocabulary. Nodes pointing to other nodes are called “parents”. A node which points to another node is the “parent” of that node. A node can have only one parent. A node can be both a child and a parent Nodes with no children are “leaf” nodes. There can (and typically will) be many leaf nodes in a tree. The node with no parent is the “root” node There can only be one such node. root data data data data data data the root “pointer” tells us where to find the “root node” each node contains “pointers” to two (in this case) or more further nodes as with linked lists, some special value is used to mean “no more”
Background image of page 1

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

View Full DocumentRight Arrow Icon
Trees are normally drawn with the root node at the top and leaf nodes at the bottom. The fact that this is the reverse of what one sees in the real world does not seem to bother anybody. A “binary tree” is a tree in which each node contains pointers to two other nodes (a tree with a branching factor of two). In such trees, each parent has up to two children. In the case of binary trees, we speak of nodes as having “left” and “right” children. Each node contains some data, a pointer (possibly null) to its left child, and a pointer (possibly null) to its right child. p p,c p,c c c c c leaves roots leaf nodes root node p = node is a parent c = node is a child p, c = node is both left data right points to left child points to right child
Background image of page 2
Binary trees may be sorted. In a sorted binary tree, part (or all) of the data stored in each node is used as a “sort key” and nodes are so arranged that 1/. All of the nodes in every parent node’s “left subtree” have lower keys than the parent node. 2/. All of the nodes in every parent node’s “right subtree” have key greater than the parent node. The “left subtree” of a node is the tree whose root is the node’s left child (i.e. the node’s left child, both of its children, all of the children of its children, and so on) A node’s “right subtree” is analogously defined. In the sorted binary tree shown below, the sort keys are character values. Other data may exist, but is not shown. Root Pointer R P S W Z M T E D F K L A J root node’s left subtree - all keys are < M root node’s right subtree - all keys are > M
Background image of page 3

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

View Full DocumentRight Arrow Icon
Our tree examples will assume a container class called “Tree”. All tree details are private to this class. Users of the class simply see a container which, for all they know, could be implemented using some other technology (i.e. arrays, linked lists). In the interest of keeping things simple, the class definition below does not provide for a destructor, a copy constructor, or overloading the assignment operator. A complete class definition would look after all of these matters. class Tree {
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/24/2010 for the course SYSC 2002 taught by Professor Unknown during the Fall '07 term at Carleton.

Page1 / 15

Chapter13 - Chapter 13 Trees The diagram below gives the...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online