Tree traversal

5 stars based on 44 reviews

Lately I'm really digging Functional Programmingand especially Haskell. I've been reading Real World Haskellwhich is a very nice free book about Haskell. Today I was wondering what Breadth First traversal was. Of course I should know this and it's stupid I forgot. To make sure I wouldn't forget in the future I made a little exercise to improve my Haskell skills, and to make sure I wouldn't forget the Breadth First algorithm anymore. Depth First and Breadth First are two different ways of traversing a tree.

It is best illustrated by the following images from Wikipedia:. The Depth First traversal in Haskell is very easy.

If you look at the pseudo code, we can almost directly translate search results for binary tree haskell to Haskell. This is simply, concatenate the a with the tree on the left and then on the tree on the right, by recursively calling traverseDF.

Breadth First is more difficult. If you look at the pseudo code there is some queue, which we kind of have to replicate, to make sure that first the root node is added to the resulting set, then the nodes at the first level, second level and finally the leaves.

At tbf [tree] we add the root node to the queue list. Then with map nodeValue xs the values of the nodes of this level are added to the resulting list. Then the tbf function is called again with all the nodes of the next level. These are concatenated with the other child nodes, and then recursively called with tbfuntil search results for binary tree haskell levels of the tree are traversed.

So now we have two functions, traverseDF and traverseBFso what are the results. So lets define some tree first:. Now I won't forget which algorithm is which, and I improved my Haskell skills a bit. It is best illustrated by the following images from Wikipedia: Node ' C' Empty Search results for binary tree haskell.

Node ' D' Empty Empty. Node ' F' Empty Empty. Node ' G' Empty Node ' H'. Node ' I' Empty Empty.

Best binary options brokers uk top 10 us

  • Binary options trading tutorials systems reviews

    Free webinars for school psychologists

  • Free webinars for school psychologists

    Optionen trading card game pokemon online free pack

T rowe price online trading

  • 5 minute eztrader binary options trading strategy

    Weigand omega b o s s binary options methods slant 50

  • Binary option market times

    Fxflat webtrader pro

  • Nigeria online forex trading

    Futuro y opcion trading ppt

Forex option broker online

26 comments Trade binary options ukulele

Strategi forex untung terus dubai

AVL Trees are binary search trees with a balance condition. The balance condition ensures that the height of the tree is bounded. Both the binary trees below obey the order property at every node. Let us see if they obey the height balance condition. The height of the subtree rooted at every node is listed next to the node. The height of an empty subtree is defined to be The height of a leaf node is 0. The height of a node is defined as larger of the heights of its two subtrees plus 1.

The path on the right does not satisfy the height balance condition at node 3. Therefore it is not an AVL tree. Consider the tree on the left, at every node the difference in the height of its subtrees is at most 1. Therefore the tree on the left is an AVL tree. Read the book by Weiss for the correctness and the analysis of the operations below.

Given the abstract data type Tree. The height of any node can be computed using the recurrence below. Given a tree, we can now check if it obeys the height balance condition. Function balanced above checks if the right and the left subtrees of the root are balanced.

If either of the subtrees is not height balanced then return false. If both the subtrees are balanced then the only node for which the height balance condition needs to be verified is the root node, and this verified in case 3, by computing the heights of the left and the right subtrees using the function height. If the root node is not height balanced then return false. If both the subtrees are balanced and the root is height balanced as well then return true.

Insertion works as in the case of binary search trees. The node is inserted in a unique place in the tree such that the order property is satisfied by the resulting tree. However, the resulting tree may not satisfy the height balance condition in which case it is to be restructured.

The restructuring happens in two ways. Let us see the first case called the single rotation. Consider inserting elements 1,2,3,4,5,6,7 in order starting with an empty AVL tree.

The sequence of first three insertions is shown below. The tree obtained after inserting 1,2,3 is not an AVL tree because the height balance condition is not obeyed at node 1. We fix the height balance condition by rotation the tree around node 3 as shown to the right.

After insertion of 4,5, another height imbalance occurs at node 3. This is fixed by rotating the subtree rooted at 3, around node 4. After insertion of 6, the root does not satisfy the height balance condition. Now a single rotation is performed at node 4. In general, single rotation is performed as follows.

Let x be the deepest node the node farthest away from the root which does not satisfy the balance condition after insertion has happened in a subtree of x. Suppose the insertion happened in the left right subtree of the left right child of x. Let y be the child of x, and z be the child of y. Note that both y, z is not Nil.

The sub tree rooted at x is shown below on the left. T1 is the left subtree of x. T2 is left sub tree of y. T3 is the left subtree of z, and T4 is the right subtree of z. Rotation around y, make x the left child of y, and z the right child of y. T1, T3, T4 are all connected to their original parents. However, T2 cannot be connected to y as its left subtree x occupies that position. T2 contains all value that are larger than x, and smaller than y.

Therefore T2 can be made the right child of x in the tree on the right. Note that this satisfies the order property. Let us see why this satisfies the height balance condition at node x. By assumption, insertion happened in T3 or T4. Before insertion the tree was balanced. Therefore the height of T2 is at one less than the height of the larger of T3 or T4 after insertion.

The height of z has not changed after rotation. Height of y has not changed after rotation, longest past from y to leaf is still in T3 or T4. The height of x has reduced by 1. Hence the tree obtained after rotation obeys the balance condition at node y.

The height of all the nodes on the path from x to the root of the tree on the left has also been reduced by 1, in the tree on the right.

Therefore they also satisfy the balance condition. The height of the nodes which do not lie on the path from the root to the inserted node is not affected by the insertion. Therefore all the nodes satisfy the height balance condition. The second type of rotation occurs when the insertion happens in the right left subtree of the left right child of the deepest node which does not satisfy the height balance condition.

Again let x, y, z be as defined above. Recall that x is the deepest node which does not satisfy the height balance condition. We consider the case when y is the left child of x, and z is the right child of y. The other case is symmetric. The insertion occurred in T2 or T3. The height of T1, and larger of T2 or T3 differ by at most 1, as y is balanced. Height of y and root of T4 differ by more than 1. The rotation reduces the height of y. First, let us verify the order property for the rotated tree.

T1 contains values smaller than y, T4 contains values larger than x. T2 contains values larger than y, and smaller than z, therefore it can be the right child of y on the right. Similarly, T3 contains values larger than z, and smaller than x, therefore T3 can be the left child of x on the right. At the start, the height of y and root of T4 differ by two. After rotation height of y has reduced by 1, as z is no longer on the longest path.

The height of x has possibly reduced by 2, y, z are not on the longest path possibly. The height of z has increased by 1. After rotation height of y, x differ by at most 1. The Haskell code below implements insertion. The first step is to insert, as we do in the case of binary search trees. The resulting tree is then fixed using the function rotate. Rotate is first called on the subtrees in case the subtrees are not balanced. Note that we can check whether the subtree is balanced using the function balanced.

If both the subtrees are balanced, then we check whether the root node obeys the height balance condition. The imbalance is fixed using either a single or a double rotation. The resulting sub tree rotated is patched together to obtain the complete tree.

Which is passed back as the result. In the worst case a rotation either SR or DR is performed to fix the height balance condition. The work done is proportional to the length of the longest path in the AVL tree.

Find is implemented as in the binary search trees. Find takes two arguments, a tree and a key, and returns the Node that contains the key. The base case states that if the tree is Nil then the search returns Nil. If the key is stored in the current Node then return the current Node. If the key is smaller then the key at the node then search in the left subtree else search in the right subtree.

Leaving this as an exercise. Implement the deletion as in BSTs. Then, walk up to the root, fixing the imbalance at every node using the rotations above. Multiple rotations might be needed.