data structures in scala

Download Data structures in scala

If you can't read please download the document

Upload: meetu-maltiar

Post on 26-May-2015

2.587 views

Category:

Technology


0 download

TRANSCRIPT

  • 1. Data Structures In Scala Meetu MaltiarPrincipal ConsultantKnoldus

2. AgendaQueueBinary TreeBinary Tree Traversals 3. Functional QueueFunctional Queue is a data structure that has threeoperations: head: returns first element of the Queue tail: returns a Queue without its Head enqueue: returns a new Queue with given element at Head Has therefore First In First Out (FIFO) property 4. Functional Queue Continuedscala> val q = scala.collection.immutable.Queue(1, 2, 3)q: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3)scala> val q1 = q enqueue 4q1: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3, 4)scala> qres3: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3)scala> q1res4: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3, 4) 5. Simple Queue Implementationclass SlowAppendQueue[T](elems: List[T]) {def head = elems.headdef tail = new SlowAppendQueue(elems.tail)def enqueue(x: T) = new SlowAppendQueue(elems ::: List(x))}Head and tail operations are fast. Enqueue operation is slow as its performance is directlyproportional to number of elements. 6. Queue Optimizing Enqueueclass SlowHeadQueue[T](smele: List[T]) {// smele is elems reverseddef head = smele.last // Not efficientdef tail = new SlowHeadQueue(smele.init) // Not efficientdef enqueue(x: T) = new SlowHeadQueue(x :: smele)}smele is elems reversed. Head operation is not efficient. Neither is tail operation. As bothlast and init performance is directly proportional to number of elements in Queue 7. Functional Queueclass Queue[T](private val leading: List[T], private val trailing:List[T]) {private def mirror =if (leading.isEmpty) new Queue(trailing.reverse, Nil)else thisdef head = mirror.leading.headdef tail = {val q = mirrornew Queue(q.leading.tail, q.trailing)}def enqueue(x: T) = new Queue(leading, x :: trailing)} 8. Binary Search TreeBST is organized tree.BST has nodes one of them is specified as Root node.Each node in BST has not more than two Children.Each Child is also a Sub-BST.Child is a leaf if it just has a root. 9. Binary Search PropertyThe keys in Binary Search Tree is stored to satisfyfollowing property:Let x be a node in BST.If y is a node in left subtree of xThen Key[y] less than equal key[x]If y is a node in right subtree of xThen key[x] less than equal key[y] 10. Binary Search PropertyThe Key of the root is 6The keys 2, 5 and 5 in left subtree is nolarger than 6.The key 5 in root left child is no smallerthan the key 2 in that nodes leftsubtree and no larger than key 5 in theright sub tree 11. Tree Scala Representationcase class Tree[+T](value: T, left:Option[Tree[T]], right: Option[Tree[T]])This Tree representation is a recursive definition and has typeparameterization and is covariant due to is [+T] signatureThis Tree class definition has following properties:1. Tree has value of the given node2. Tree has left sub-tree and it may have or do not contain value3. Tree has right sub-tree and it may have or do not contain valueIt is covariant to allow subtypes to be contained in the Tree 12. Tree In-order TraversalBST property enables us to print out allthe Keys in a sorted order using simplerecursive In-order traversal.It is called In-Order because it printskey of the root of a sub-tree betweenprinting of the values in its left sub-tree and printing those in its right sub-tree 13. Tree In-order AlgorithmINORDER-TREE-WALK(x)1. if x != Nil2. INORDER-TREE-WALK(x.left)3. println x.key4. INORDER-TREE-WALK(x.right)For our BST in example before the output expected will be:255678 14. Tree In-order Scaladef inOrder[A](t: Option[Tree[A]], f: Tree[A] =>Unit): Unit = t match {case None =>case Some(x) =>if (x.left != None) inOrder(x.left, f)f(x)if (x.right != None) inOrder(x.right, f)} 15. Tree Pre-order AlgorithmPREORDER-TREE-WALK(x)1. if x != Nil2. println x.key3. PREORDER-TREE-WALK(x.left)4. PREORDER-TREE-WALK(x.right)For our BST in example before the output expected will be:652578 16. Tree Pre-order Scaladef preOrder[A](t: Option[Tree[A]], f: Tree[A]=> Unit): Unit = t match {case None =>case Some(x) =>f(x)if (x.left != None) inOrder(x.left, f)if (x.right != None) inOrder(x.right, f)}Pre-Order traversal is good for creating a copy of the Tree 17. Tree Post-Order AlgorithmPOSTORDER-TREE-WALK(x)1. if x != Nil2. POSTORDER-TREE-WALK(x.left)3. POSTORDER-TREE-WALK(x.right)4. println x.keyFor our BST in example before the output expected will be:255876Useful in deleting a tree. In order to free up resources anode in the tree can only be deleted if all the children (leftand right) are also deletedPost-Order does exactly that. It processes left and rightsub-trees before processing current node 18. Tree Post-order Scaladef postOrder[A](t: Option[Tree[A]], f: Tree[A]=> Unit): Unit = t match {case None =>case Some(x) =>if (x.left != None) postOrder(x.left, f)if (x.right != None) postOrder(x.right, f)f(x)} 19. References1. Cormen Introduction to Algorithms2. Binary Search Trees Wikipedia3. Martin Odersky Programming In Scala4. Daniel spiewak talk Extreme Cleverness:Functional Data Structures In Scala 20. Thank You!!