220 likes | 357 Views
Tree Traversals, TreeSort. 20 February 2003. +. –. *. A. B. C. –. E. F. Expression Tree. Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C * (E - F). Formal Definition of Tree. An empty structure is an empty tree.
E N D
Tree Traversals, TreeSort 20 February 2003
+ – * A B C – E F Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C * (E - F)
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.
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.
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.]
– * + – A B C E F 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.
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.
Preorder Binary Tree Traversal In a preorder traversal, the nodes will be visited in the following order: • First, process the root node. • Then, recursively visit all nodes in the left subtree. • 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)
Inorder Binary Tree Traversal The inorder traversal of a binary tree visits nodes in the following order. • First, recursively visit all nodes in the left subtree. • Then, process the root node. • Finally, recursively visit all nodes in the right subtree. A – B + C * E – F (infix form)
Postorder Binary Tree Traversal The postorder traversal of a binary tree visits the nodes in the following order: • First, recursively visit all nodes in the left subtree of the root node. • Then, recursively visit all nodes in the right subtree of the root node. • Finally, process the root. A B – C E F – * + (postfix form)
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
A A1 A2 A3 A4 A11 A12 A13 A31 A32 A41 A411 A412 A413 A General Tree
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.
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º
Example 1 5 2 3 4 6 7 8 11 9 10
Example (first child/sibling links) 1 5 X X X X 2 3 4 6 7 8 X X 11 9 10
Example 1 5 2 3 4 6 7 8 9 10 11
Example (rotate 45º) 1 2 5 6 3 7 4 8 9 10 11
Tree Sort • Algorithm: • create empty BST • insert (ordered) each element into BST • Inorder traverse BST • (destroy BST)
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