Top Banner
CMSC 202, Version 5/02 1 Trees
42

CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

Jan 18, 2018

Download

Documents

Tracey Adams

CMSC 202, Version 5/02 3 A General Tree A B G K CD L HIJ EF
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

1

Trees

Page 2: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

2

Tree Basics1. A tree is a set of nodes.2. A tree may be empty (i.e., contain no

nodes).3. If not empty, there is a distinguished

node, r, called the root and zero or more non-empty subtrees T1, T2, T3,….Tk, each of whose roots is connected by a directed edge from r.

4. Trees are recursive in their definition and, therefore, in their implementation.

Page 3: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

3

A General TreeA

B G K

C D LH I J

E F

Page 4: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

4

Tree Terminology• Tree terminology takes its terms from both

nature and genealogy.• A node directly below the root, r, of a subtree

is a child of r, and r is called its parent.• All children with the same parent are called

siblings.• A node with one or more children is called

an internal node.• A node with no children is called a leaf or

external node.

Page 5: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

5

Tree Terminology (con’t)• A path in a tree is a sequence of nodes,

(N1, N2, … Nk) such that Ni is the parent of Ni+1 for 1 <= i <= k.

• The length of this path is the number of edges encountered (k – 1).

• If there is a path from node N1 to N2, then N1 is an ancestor of N2 and N2 is a descendant of N1.

Page 6: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

6

Tree Terminology (con’t)• The depth of a node is the length of the

path from the root to the node.• The height of a node is the length of the

longest path from the node to a leaf.• The depth of a tree is the depth of its

deepest leaf.• The height of a tree is the height of the

root.• True or False – The height of a tree and the

depth of a tree always have the same value.

Page 7: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

7

Tree Storage• First attempt - each tree node contains

– The data being stored• We assume that the objects contained in the nodes support all

necessary operations required by the tree.– Links to all of its children

• Problem: A tree node can have an indeterminate number of children. So how many links do we define in the node?

Page 8: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

8

First Child, Next Sibling

• Since we can’t know how many children a node can have, we can’t create a static data structure -- we need a dynamic one.

• Each node will contain– The data which supports all necessary

operations– A link to its first child– A link to a sibling

Page 9: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

9

First Child, Next Sibling Representation

• To be supplied in class

Page 10: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

10

Tree Traversal• Traversing a tree means starting at the

root and visiting each node in the tree in some orderly fashion. “visit” is a generic term that means “perform whatever operation is applicable”.

• “Visiting” might mean – Print data stored in the tree– Check for a particular data item– Almost anything

Page 11: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

11

Breadth-First Tree Traversals• Start at the root.• Visit all the root’s children.• Then visit all the root’s grand-children.• Then visit all the roots great-grand-

children, and so on.

• This traversal goes down by levels.• A queue can be used to implement this

algorithm.

Page 12: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

12

BF Traversal Pseudocode

Create a queue, Q, to hold tree nodesQ.enqueue (the root)while (the queue is not empty)

Node N = Q.dequeue( )for each child, X, of N

Q.enqueue (X)

• The order in which the nodes are dequeued is the BF traversal order.

Page 13: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

13

Depth-First Traversal• Start at the root.• Choose a child to visit; remember those not chosen• Visit all of that child’s children.• Visit all of that child’s children’s children, and so on.• Repeat until all paths have been traversed

• This traversal goes down a path until the end, then comes back and does the next path.

• A stack can be used to implement this algorithm.

Page 14: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

14

DF Traversal Pseudocode

Create a stack, S, to hold tree nodesS.push (the root)While (the stack is not empty)

Node N = S.pop ( )for each child, X, of N

S.push (X)

The order in which the nodes are popped is the DF traversal order.

Page 15: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

15

Performance of BF and DF Traversals

• What is the asymptotic performance of breadth-first and depth-first traversals on a general tree?

Page 16: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

16

Binary Trees• A binary tree is a tree in which each node

may have at most two children and the children are designated as left and right.

• A full binary tree is one in which each node has either two children or is a leaf.

• A perfect binary tree is a full binary tree in which all leaves are at the same level.

Page 17: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

17

A Binary Tree

Page 18: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

18

A binary tree?

A full binary tree?

Page 19: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

19

A binary tree?

A full binary tree?

A perfect binary tree?

Page 20: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

20

Binary Tree Traversals• Because nodes in binary trees have at

most two children (left and right), we can write specialized versions of DF traversal. These are called– In-order traversal– Pre-order traversal– Post-order traversal

Page 21: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

21

In-Order Traversal• At each node

– visit my left child first– visit me– visit my right child last

10

5

79 12

8

3

15 6 2

Page 22: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

22

In-Order Traversal Code

void inOrderTraversal(Node *nodePtr) {

if (nodePtr != NULL) {

inOrderTraversal(nodePtr->leftPtr);

cout << nodePtr->data << endl;

inOrderTraversal(nodePtr->rightPtr);

}

}

Page 23: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

23

Pre-Order Traversal• At each node

– visit me first– visit my left child next– visit my right child last

10

5

79 12

8

3

15 6 2

Page 24: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

24

Pre-Order Traversal Code

void preOrderTraversal(Node *nodePtr) {

if (nodePtr != NULL) {

cout << nodePtr->data << endl;

preOrderTraversal(nodePtr->leftPtr);

preOrderTraversal(nodePtr->rightPtr);

}

}

Page 25: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

25

Post-Order Traversal• At each node

– visit my left child first– visit my right child next– visit me last

10

5

79 12

8

3

15 6 2

Page 26: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

26

Post-Order Traversal Code

void postOrderTraversal(Node *nodePtr) {

if (nodePtr != NULL) {

postOrderTraversal(nodePtr->leftPtr);

postOrderTraversal(nodePtr->rightPtr);

cout << nodePtr->data << endl;

}

}

Page 27: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

27

Binary Tree Operations• Recall that the data stored in the nodes

supports all necessary operators. We’ll refer to it as a “value” for our examples.

• Typical operations:– Create an empty tree– Insert a new value– Search for a value– Remove a value– Destroy the tree

Page 28: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

28

Creating an Empty Tree• Set the pointer to the root node equal to

NULL.

Page 29: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

29

Inserting a New Value• The first value goes in the root node.• What about the second value?• What about subsequent values?

• Since the tree has no properties which dictate where the values should be stored, we are at liberty to choose our own algorithm for storing the data.

Page 30: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

30

Searching for a Value

• Since there is no rhyme or reason to where the values are stored, we must search the entire tree using a BF or DF traversal.

Page 31: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

31

Removing a Value

• Once again, since the values are not stored in any special way, we have lots of choices. Example:– First, find the value via BF or DF traversal.– Second, replace it with one of its descendants

(if there are any).

Page 32: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

32

Destroying the Tree

• We have to be careful of the order in which nodes are destroyed (deallocated).

• We have to destroy the children first, and the parent last (because the parent points to the children).

• Which traversal (in-order, pre-order, or post-order) would be best for this algorithm?

Page 33: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

33

Giving Order to a Binary Tree• Binary trees can be made more useful if we

dictate the manner in which values are stored.

• When selecting where to insert new values, we could follow this rule:– “left is less”– “right is more”

• Note that this assumes no duplicate nodes (i.e., data).

Page 34: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

34

Binary Search Trees

• A binary tree with the additional property that at each node,– the value in the node’s left child is smaller

than the value in the node itself, and– the value in the node’s right child is larger

than the value in the node itself.

Page 35: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

35

A Binary Search Tree

50

42 57

30

22 34

53 67

Page 36: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

36

Searching a BST• Searching for the value X, given a

pointer to the root1. If the value in the root matches, we’re done.2. If X is smaller than the value in the root, look

in the root’s left subtree.3. If X is larger than the value in the root, look

in the root’s right subtree.• A recursive routine – what’s the base

case?

Page 37: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

37

Inserting a Value in a BST• To insert value X in a BST

1. Proceed as if searching for X.2. When the search fails, create a new node

to contain X and attach it to the tree at that node.

Page 38: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

38

Inserting100 38 56 150 20 40 125 138 90

Page 39: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

39

Removing a Value From a BST

• Non-trivial• Three separate cases:

– node is a leaf (has no children)– node has a single child– node has two children

Page 40: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

40

Removing100

50 150

70

80

30

60

120

1304020

65 8555

53 57

Page 41: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

41

Destroying a BST

• The fact that the values in the nodes are in a special order doesn’t help.

• We still have to destroy each child before destroying the parent.

• Which traversal must we use?

Page 42: CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.

CMSC 202, Version 5/02

42

Performance in a BST• What is the asymptotic performance of:

– insert– search– remove

• Is the performance of insert, search, and remove for a BST improved over that for a plain binary tree?– If so, why?– If not, why not?