Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Tree Traversals, TreeSort 20 February 2003

2 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C * (E - F) + –* BA–C FE

3 3 Formal Definition of Tree An empty structure is an empty tree. If t 1, t 2, …, t k are disjoint trees, then the structure whose root has as its children the roots of t 1, t 2, …, t k, is also a tree. Only structures generated by rules 1 and 2 are trees.

4 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.

5 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.]

6 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. –*+–ABCEF

7 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.

8 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)

9 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)

10 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)

11 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, A 1 is the first child of A, with A 2, A 3, and A 4 as A 1 ’s siblings. Similarly, A 11 is the first child of A 1, with A 12 and A 13 as A 1 ’s siblings

12 12 A General Tree A 413 A 411 A 412 A 41 A 32 A 31 A 13 A 12 A 11 A4A4 A3A3 A2A2 A1A1 A

13 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.

14 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º

15 15 Example 4 15 3276 109 8 11

16 16 Example (first child/sibling links) 4 15 3276 109 8 11 X X X X X X

17 17 Example 4 15 3276 109 8 11

18 18 Example (rotate 45º) 8 7 52 1 3 4 6 9 10 11

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

20 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(n 2 ) behavior just like QuickSort


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

Similar presentations


Ads by Google