tree traversals, treesort 20 february 2003. 2 expression tree leaves are operands interior nodes are...

20
Tree Traversals, TreeSort 20 February 2003

Upload: neil-owen

Post on 21-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

Tree Traversals, TreeSort

20 February 2003

Page 2: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

2

Expression Tree

Leaves are operands

Interior nodes are operators

A binary tree to represent (A - B) + C * (E - F)

+

– *

BA –C

FE

Page 3: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

3

Formal Definition of Tree

An empty structure is an empty tree. If t1, t2, …, tk are disjoint trees, then the

structure whose root has as its children the roots of t1, t2, …, tk, is also a tree.

Only structures generated by rules 1 and 2 are trees.

Page 4: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

4

Observations

The recursive fashion in which a tree is defined provides a strong hint that most tree processing algorithms will also be recursive.

Most operations on the tree data structure are closely linked to the hierarchical relationship among nodes for that particular tree.

Page 5: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

5

Tree Properties

The minimal tree is an empty tree and has no nodes. If not empty, a tree consists of nodes and branches. Each node must be reachable from the root through a

unique sequence of branches, called a path. The number of branches in a path is the length of the

path. The level of a node is the length of the path from the

root to the node plus one, which is the number of nodes in the path.

The height of a nonempty tree is the maximum level of a node in the tree. [The empty tree has height 0 (by definition), and a single node is a tree of height 1. This is the only case in which a node is both the root and a leaf.]

Page 6: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

6

Linked Implementation of a Binary Tree

Since each node in a binary tree has at most two children, a node in a linked implementation has two pointer members, one for each child, and one or more members for storing data.

– *

+

–A B C

E F

Page 7: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

7

Tree Traversals

Move through all the nodes in a tree visiting them one at a time.

For lists we traverse nodes by visiting successors.

We have more freedom in traversing trees.

Page 8: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

8

Preorder Binary Tree Traversal

In a preorder traversal, the nodes will be visited in the following order:

1. First, process the root node.

2. Then, recursively visit all nodes in the left subtree.

3. Finally, recursively visit all nodes in the right subtree.

The traversal method works recursively, that is, once the root of the tree is processed, we go to the root of the left subtree, and then to the root of the left subtree of the left subtree, and so on until we can go no farther.

+ – A B * C – E F (prefix form)

Page 9: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

9

Inorder Binary Tree Traversal

The inorder traversal of a binary tree visits nodes in the following order.

1. First, recursively visit all nodes in the left subtree.

2. Then, process the root node.

3. Finally, recursively visit all nodes in the right subtree.

A – B + C * E – F(infix form)

Page 10: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

10

Postorder Binary Tree Traversal

The postorder traversal of a binary tree visits the nodes in the following order: 1. First, recursively visit all nodes in the left subtree

of the root node.

2. Then, recursively visit all nodes in the right subtree of the root node.

3. Finally, process the root.

A B – C E F – * +(postfix form)

Page 11: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

11

General Trees

So far we have considered binary trees. There are many applications with hierarchical

relationships in which a parent may have an unrestricted number of children.

A tree that represents such structures is called a general tree.

For example, consider the general tree of the next slide. – In this structure, A1 is the first child of A, with A2, A3,

and A4 as A1’s siblings. Similarly, A11 is the first child of A1, with A12 and A13 as A1’s siblings

Page 12: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

12

A General Tree

A413A411 A412

A41A32A31A13A12A11

A4A3A2A1

A

Page 13: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

13

Linked Representation

We can convert any general tree (or forest) into a binary tree.

We can use the linked representation of the converted binary tree to represent this general tree.

Since only two pointers are associated with each node of the binary tree, we do this by defining one of the pointers to be a pointer to the leftmost child of a node in the general tree, while the other pointer is used to identify the next sibling of that node in the general tree.

Page 14: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

14

Converting any Forest to a Tree

Connect the roots of the trees via right links As we descend the levels of the trees in the

forest, make the left pointer point to the first child of a node and the right pointer of the first child begins a linked list of that child’s siblings.

Rotate the entire structure by 45º

Page 15: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

15

Example

4

1 5

32 76

109

8

11

Page 16: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

16

Example (first child/sibling links)

4

1 5

32 76

109

8

11X X

XX X X

Page 17: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

17

Example

4

1 5

32 76

109

8

11

Page 18: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

18

Example (rotate 45º)

8

7

52

1

3

4

6

9

10

11

Page 19: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

19

Tree Sort

Algorithm: – create empty BST– insert (ordered) each element into BST – Inorder traverse BST– (destroy BST)

Page 20: Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C

20

Tree Sort

Advantages: – n elements, lg(n) insert O(nlg(n)) sort– don’t need to have a fixed set of data, nodes can be

inserted and deleted dynamically

Disadvantages: – additional overhead of entire Tree– if data arrives in order or reverse order degenerate

to O(n2) behavior just like QuickSort