[
  {
    "path": ".gitignore",
    "content": "# Binaries for programs and plugins\n*.exe\n*.dll\n*.so\n*.dylib\n\n# Test binary, build with `go test -c`\n*.test\n\n# Output of the go coverage tool, specifically when used with LiteIDE\n*.out\n\n# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736\n.glide/\n"
  },
  {
    "path": ".travis.yml",
    "content": "language: go\ngo:\n  - 1.10.x\n  - 1.11.x\nscript:\n  - go build -v ./...\n  - go test -v -cover -race ./...\nafter_success:\n  - bash <(curl -s https://codecov.io/bash)\n"
  },
  {
    "path": "CONTRIBUTING.md",
    "content": ""
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2017 Furkan Türkal\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "<h1 align=\"center\">Data Structures with Go</h1>\n\n[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)\n[![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.png?v=103)](https://github.com/ellerbrock/open-source-badges/)\n[![Build status](https://ci.appveyor.com/api/projects/status/jv28205majdbvvj0?svg=true)](https://ci.appveyor.com/project/Dentrax/data-structures-with-go)\n[![Build Status](https://travis-ci.org/Dentrax/Data-Structures-with-Go.svg?branch=master)](https://travis-ci.org/Dentrax/Data-Structures-with-Go)\n[![codecov](https://codecov.io/gh/Dentrax/Data-Structures-with-Go/branch/master/graph/badge.svg)](https://codecov.io/gh/Dentrax/Data-Structures-with-Go)\n[![Go Report Card](https://goreportcard.com/badge/github.com/Dentrax/Data-Structures-with-Go)](https://goreportcard.com/report/github.com/Dentrax/Data-Structures-with-Go)\n[![Sourcegraph](https://img.shields.io/badge/view%20on-Sourcegraph-brightgreen.svg)](https://sourcegraph.com/github.com/Dentrax/Data-Structures-with-Go)\n\nClick here for **[GO Language](https://golang.org/)**\n\nClick here for **[Guide & Theory](https://goo.gl/Ej9kzs)**\n\nClick here for **[VSCode IDE](https://code.visualstudio.com/)**\n\n[What It Is](#what-it-is)\n\n[How To Use](#how-to-use)\n\n[About](#about)  \n\n[Collaborators](#collaborators)  \n\n[Branches](#branches) \n\n[Copyright & Licensing](#copyright--licensing)  \n\n[Contributing](#contributing)  \n\n[Contact](#contact)\n\n## What It Is\n\n**Data Structures with Go**\n\nData-Structures-with-Go guide for GO language is an easy and advanced way to learn Data Structures.\n\n**Uses : `GO Language`** -> **[Official GO Language Web Site](https://golang.org/)**\n\n## How To Use\n\nJust research the **[main repository](https://github.com/Dentrax/Data-Structures-with-Go)** and learn it gradually. Thats all.\n\n## About\n\nData-Structures-with-Go was created to serve three purposes:\n\n**Data-Structures-with-Go is a basically Data-Structures learning repository which all structures coded in Go language**\n\n1. To act as a guide to learn basic Data Structures with enhanced and rich content.\n\n2. To provide a simplest and easiest way to learn. \n\n3. There is no any Data Structures guide coded in Go language on the internet.\n\n## Collaborators\n\n**Project Manager** - Furkan Türkal (GitHub: **[dentrax](https://github.com/dentrax)**)\n\n## Branches\n\nWe publish source for the **[Data-Structures-with-Go]** in single rolling branch:\n\nThe **[master branch](https://github.com/dentrax/Data-Structures-with-Go/tree/master)** is extensively tested by our QA team and makes a great starting point for learning the GO language. Also tracks [live changes](https://github.com/dentrax/Data-Structures-with-Go/commits/master) by our team. \n\n## Copyright & Licensing\n\nThe base project code is copyrighted by Furkan 'Dentrax' Türkal and is covered by single licence.\n\nAll program code (i.e. Go) is licensed under MIT License unless otherwise specified. Please see the **[LICENSE.md](https://github.com/Dentrax/Data-Structures-with-Go/blob/master/LICENSE)** file for more information.\n\n**References**\n\nWhile this repository is being prepared, it may have been quoted from some sources. \nIf there is an unspecified source, please contact me.\n\n## Contributing\n\nPlease check the [CONTRIBUTING.md](CONTRIBUTING.md) file for contribution instructions and naming guidelines.\n\n## Contact\n\nData-Structures-with-Go was created by Furkan 'Dentrax' Türkal\n\n * <https://www.furkanturkal.com>\n \nYou can contact by URL:\n    **[CONTACT](https://github.com/dentrax)**\n\n<kbd>Best Regards</kbd>\n"
  },
  {
    "path": "appveyor.yml",
    "content": "version: \"{build}\"\nskip_tags: true\nclone_depth: 1\n\nenvironment:\n  GOVERSION: 1.11\n\nbuild: false\ndeploy: false\n\ninstall:\n  - go build -v ./...\n"
  },
  {
    "path": "array-minimum-distance/README.md",
    "content": "<h1 align=\"center\">Array Minimum Distance Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\n* Given an unsorted array `arr[]` and two numbers x and y, find the minimum distance between `x` and `y` in `arr[]`. The array might also contain duplicates. You may assume that both `x` and `y` are different and present in `arr[]`.\n\nExamples\n--------------------------\n\n> * Input: arr[] = {1, 2}, x = 1, y = 2\n> * Output: Minimum distance between 1 and 2 is 1.\n\n> * Input: arr[] = {3, 4, 5}, x = 3, y = 5\n> * Output: Minimum distance between 3 and 5 is 2.\n\n> * Input: arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, x = 3, y = 6\n> * Output: Minimum distance between 3 and 6 is 4.\n\n> * Input: arr[] = {2, 5, 3, 5, 4, 4, 2, 3}, x = 3, y = 2\n> * Output: Minimum distance between 3 and 2 is 1.\n\nMETHOD 1 (Simple)\n--------------------------\n\nUse two loops: The outer loop picks all the elements of arr[] one by one. The inner loop picks all the elements after the element picked by outer loop. If the elements picked by outer and inner loops have same values as x or y then if needed update the minimum distance calculated so far.\n\n**Algorithm Complexity**\n\n| Complexity\t    | Notation  |\n| ----------------- |:---------:|\n| `Time Complexity`\t| `O(n^2)`  |"
  },
  {
    "path": "array-minimum-distance/array-minimum-distance.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\nconst MaxUint = ^uint(0) \nconst MinUint = 0 \nconst MaxInt = int(MaxUint >> 1) \nconst MinInt = -MaxInt - 1\n\nfunc Abs(x int) int {\n\tif x < 0 {\n\t\treturn -x\n\t}\n\tif x == 0 {\n\t\treturn 0\n\t}\n\treturn x\n}\n\nfunc minDist (arr []int, n, x, y int) int {\n\tvar min_dist int = MaxInt\n\n\tfor i := 0; i < n; i++ {\n\t\tfor j := i + 1; j < n; j++ {\n\t\t\tif((x == arr[i] && y == arr[j] || y == arr[i] && x == arr[j]) && min_dist > Abs(i - j)) {\n\t\t\t\tmin_dist = Abs(i - j)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn min_dist\n}\n\nfunc main() {\n\tarr := []int{3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}\n\tvar n int = len(arr)\n\tvar x int = 3\n\tvar y int = 6\n\tfmt.Printf(\"Minimum distance between %d and %d is %d\", x, y, minDist(arr, n, x, y))\n}"
  },
  {
    "path": "array-minimum-distance/array-minimum-distance_test.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestAbs(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tInput  int\n\t\tOutput int\n\t}{\n\t\t{1, 1},\n\t\t{7, 7},\n\t\t{-1, 1},\n\t\t{-7, 7},\n\t\t{0, 0},\n\t}\n\tfor _, data := range testDatas {\n\t\texpected := data.Output\n\t\tactual := Abs(data.Input)\n\n\t\tif !reflect.DeepEqual(expected, actual) {\n\t\t\tt.Errorf(\"Abs: Expected: %d, Actual: %d\", expected, actual)\n\t\t}\n\n\t}\n}\n\nfunc TestMinDist(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tArray    []int\n\t\tNum1     int\n\t\tNum2     int\n\t\tDistance int\n\t}{\n\t\t{[]int{3, 4, 5, 6}, 3, 6, 3},\n\t\t{[]int{1, 2, 3}, 1, 3, 2},\n\t\t{[]int{9, 7, 5, 3, 1}, 1, 7, 3},\n\t\t{[]int{-1, -7, 5, 5, 1, 5}, -1, 5, 2},\n\t\t{[]int{-1, -7, 5, 5, -7, -1}, -1, -7, 1},\n\t}\n\tfor _, data := range testDatas {\n\t\texpected := data.Distance\n\t\tactual := minDist(data.Array, len(data.Array), data.Num1, data.Num2)\n\n\t\tif !reflect.DeepEqual(expected, actual) {\n\t\t\tt.Errorf(\"MinDist: Expected: %d, Actual: %d\", expected, actual)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "array-rotation/README.md",
    "content": "<h1 align=\"center\">Array Rotation Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\n> * Input: Write a function `rotate(ar[], d, n)` that rotates `arr[]` of size `n` by `d` elements\n> * Output: `1 2 3 4 5 6 7`\n\n> * Input: Rotation of the above array by `2` will make array\n> * Output: `3 4 5 6 7 1 2`\n\nMETHOD 1 (Use temp array)\n--------------------------\n\nInput arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7\n\n* 1) Store `d` elements in a temp array\n   temp[] = [1, 2]\n* 2) Shift rest of the `arr[]`\n   arr[] = [3, 4, 5, 6, 7, 6, 7]\n* 3) Store back the `d` elements\n   arr[] = [3, 4, 5, 6, 7, 1, 2]\n\n**Algorithm Complexity**\n\n| Complexity\t\t    | Notation  |\n| ----------------- |:---------:|\n| `Time Complexity`\t| `O(n)`    |\n| `Auxiliary Space` | `O(d)`    |\n\n\nMETHOD 2 (Rotate one by one)\n--------------------------\n\n```go\nleftRotate(arr[], d, n)\nstart\n  For i = 0 to i < d\n    Left rotate all elements of arr[] by one\nend\n```\n\nTo rotate by one, store arr[0] in a temporary variable temp, move arr[1] to arr[0], arr[2] to arr[1] …and finally temp to arr[n-1]\nLet us take the same example arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2\nRotate arr[] by one 2 times\nWe get `[2, 3, 4, 5, 6, 7, 1]` after first rotation and `[3, 4, 5, 6, 7, 1, 2]` after second rotation.\n\n**Algorithm Complexity**\n\n| Complexity\t\t    | Notation  |\n| ----------------- |:---------:|\n| `Time Complexity`\t| `O(n*d)`  |\n| `Auxiliary Space` | `O(1)`    |"
  },
  {
    "path": "array-rotation/array-rotation.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\nfunc leftRotate(arr []int, d int, n int){\n\tfor i := 0; i < d; i++ {\n\t\tleftRotateByOne(arr, n)\n\t}\n}\n\nfunc leftRotateByOne(arr []int, n int){\n\tvar i, temp int\n\ttemp = arr[0]\n\tfor i = 0; i < n - 1; i++ {\n\t\tarr[i] = arr[i + 1]\n\t}\n\tarr[i] = temp\n}\n\nfunc printArray(arr []int, size int){\n\tif(len(arr) < size){\n\t\tfmt.Println(\"[ArrayRotation::printArray()] -> Index out of range. Max : \", len(arr))\n\t\treturn;\n\t}\n\tfor i := 0; i < size; i++ {\n\t\tfmt.Print(arr[i])\n\t}\n}\n\nfunc main() {\n\tarr := []int{1, 2, 3, 4, 5, 6, 7}\n\tfmt.Println(\"-INPUT-\")\n\tprintArray(arr, len(arr) - 1)\n\tleftRotate(arr, 2 , 7)\n\tfmt.Println()\n\tfmt.Println(\"-OUTPUT-\")\n\tprintArray(arr, 7)\n}\n\n\n\n"
  },
  {
    "path": "array-rotation/array-rotation_test.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestLeftRotate(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tArrayIn  []int\n\t\tCount    int\n\t\tDepth    int\n\t\tArrayOut []int\n\t}{\n\t\t{[]int{1, 2, 3, 4, 5, 6, 7}, 2, 7, []int{3, 4, 5, 6, 7, 1, 2}},\n\t\t{[]int{1, 2, 3, 4, 5, 6, 7}, 2, 6, []int{3, 4, 5, 6, 1, 2, 7}},\n\t\t{[]int{1, 2, 3, 4, 5, 6, 7}, 1, 2, []int{2, 1, 3, 4, 5, 6, 7}},\n\t\t{[]int{1, 2, 3, 4, 5, 6, 7}, 7, 7, []int{1, 2, 3, 4, 5, 6, 7}},\n\t\t{[]int{1, 2, 3, 4, 5, 6, 7}, 7, 6, []int{2, 3, 4, 5, 6, 1, 7}},\n\t}\n\tfor _, data := range testDatas {\n\t\texpected := data.ArrayOut\n\t\tleftRotate(data.ArrayIn, data.Count, data.Depth)\n\t\tactual := data.ArrayIn\n\n\t\tif !reflect.DeepEqual(expected, actual) {\n\t\t\tt.Errorf(\"LeftRotate: Expected: %d, Actual: %d\", expected, actual)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "array-smallest-missing-number/README.md",
    "content": "<h1 align=\"center\">Array Smallest Missing Number Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\nGiven a sorted array of `n` distinct integers where each integer is in the range from `0 to m-1 and m > n`. Find the smallest number that is missing from the array.\n\nExamples\n--------------------------\n\n> * Input: {0, 1, 2, 6, 9}, n = 5, m = 10\n> * Output: `3`\n\n> * Input: {4, 5, 10, 11}, n = 4, m = 12\n> * Output: `0`\n\n> * Input: {0, 1, 2, 3}, n = 4, m = 5\n> * Output: `4`\n\n> * Input: {0, 1, 2, 3, 4, 5, 6, 7, 10}, n = 9, m = 11\n> * Output: `8`\n\nMETHOD 1 (Use Binary Search)\n--------------------------\n\n`For i = 0 to m-1`, do binary search for i in the array. If i is not present in the array then `return i`.\n\n**Algorithm Complexity**\n\n| Complexity\t    | Notation     |\n| ----------------- |:------------:|\n| `Time Complexity`\t| `O(m log n)` |\n\nMETHOD 2 (Linear Search)\n--------------------------\n\nIf `arr[0] is not 0`, `return 0`. Otherwise traverse the input array starting from index 0, and for each pair of elements a[i] and a[i+1], find the difference between them. if the difference is greater than 1 then `a[i] + 1` is the missing number.\n\n**Algorithm Complexity**\n\n| Complexity\t    | Notation     |\n| ----------------- |:------------:|\n| `Time Complexity`\t| `O(n)`       |\n\nMETHOD 3 (Use Modified Binary Search)\n--------------------------\n\nIn the standard Binary Search process, the element to be searched is compared with the middle element and on the basis of comparison result, we decide whether to search is over or to go to left half or right half.\nIn this method, we modify the standard Binary Search algorithm to compare the middle element with its index and make decision on the basis of this comparison.\n\n1) If the first element is not same as its index then return first index\n2) Else get the middle index say mid\n    a) If arr[mid] greater than mid then the required element lies in left half.\n    b) Else the required element lies in right half.\n\n`Note: This method doesn’t work if there are duplicate elements in the array.`\n\n**Algorithm Complexity**\n\n| Complexity\t    | Notation     |\n| ----------------- |:------------:|\n| `Time Complexity`\t| `O(log n)`   |\n"
  },
  {
    "path": "array-smallest-missing-number/array-smallest-missing-number.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\nimport \"os\"\n\nfunc findFirstMissing(arr []int, start, end int) int {\n\tif start < 0 {\n\t\tfmt.Println(\"Start must be greater than 0\")\n\t\tos.Exit(1)\n\t\treturn 0\n\t}\n\n\tif start > end {\n\t\treturn end + 1\n\t}\n\n\tif start != arr[start] {\n\t\treturn start\n\t}\n\n\tmid := (start + end) / 2\n\n\tif arr[mid] == mid {\n\t\treturn findFirstMissing(arr, mid+1, end)\n\t}\n\n\treturn findFirstMissing(arr, start, end)\n}\n\nfunc main() {\n\tarr := []int{0, 1, 2, 3, 4, 5, 6, 7, 10}\n\tvar n int = len(arr)\n\tfmt.Printf(\"Smallest missing element is %d\", findFirstMissing(arr, 0, n-1))\n}\n"
  },
  {
    "path": "array-smallest-missing-number/array-smallest-missing-number_test.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestFindFirstMissing(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tArrayIn []int\n\t\tStart   int\n\t\tEnd     int\n\t\tOut     int\n\t}{\n\t\t{[]int{0, 1, 2, 4, 5}, 0, 4, 3},\n\t\t{[]int{0, 1, 2, 3, 4, 6, 8, 9, 10}, 0, 5, 5},\n\t\t{[]int{0, 1, 2, 3, 4, 6, 8, 9, 10}, 0, 8, 5},\n\t\t{[]int{0, 1, 2, 3, 4, 5, 6, 8, 9, 10}, 0, 8, 7},\n\t\t{[]int{1, 2, 3, 4, 5}, 0, 5, 0},\n\t}\n\tfor _, data := range testDatas {\n\t\texpected := data.Out\n\t\tactual := findFirstMissing(data.ArrayIn, data.Start, data.End)\n\n\t\tif !reflect.DeepEqual(expected, actual) {\n\t\t\tt.Errorf(\"FindFirstMissing: Expected: %d, Actual: %d\", expected, actual)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "binary-search-tree-1-insertion/binary-search-tree-1-insertion.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata  int\n\tleft  *Node\n\tright *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init(data int) *Node {\n\tn.data = data\n\tn.left = nil\n\tn.right = nil\n\treturn n\n}\n\n//Returns an new list\nfunc New(data int) *Node {\n\treturn new(Node).Init(data)\n}\n\n//Function to search a given key in a given BST\nfunc Search(root *Node, key int) *Node {\n\t//1. Base Cases: root is null or key is present at root\n\tif root == nil || root.data == key {\n\t\t//fmt.Println(\"The given previous node cannot be NULL\")\n\t\treturn root\n\t}\n\n\t//2. Key is greater than root's key\n\tif root.data < key {\n\t\treturn Search(root.right, key)\n\t}\n\n\t//3. Key is smaller than root's key\n\treturn Search(root.left, key)\n}\n\n//A utility function to do inorder traversal of BST\nfunc PrintInOrder(root *Node) {\n\tif root != nil {\n\t\tPrintInOrder(root.left)\n\t\tfmt.Printf(\"%d \\n\", root.data)\n\t\tPrintInOrder(root.right)\n\t}\n}\n\n//A utility function to insert a new node with given key in BST\nfunc Insert(node *Node, key int) *Node {\n\t//1. If the tree is empty, return a new node\n\tif node == nil {\n\t\treturn New(key)\n\t}\n\n\t//2. Otherwise, recur down the tree\n\tif key < node.data {\n\t\tnode.left = Insert(node.left, key)\n\t} else if key > node.data {\n\t\tnode.right = Insert(node.right, key)\n\t}\n\n\t//3. Return the (unchanged) node pointer\n\treturn node\n}\n\nfunc main() {\n\t/* Let us create following BST\n\t              50\n\t           /     \\\n\t          30      70\n\t         /  \\    /  \\\n\t       20   40  60   80 */\n\n\t//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));\n\troot := New(50)\n\tInsert(root, 30)\n\tInsert(root, 20)\n\tInsert(root, 40)\n\tInsert(root, 70)\n\tInsert(root, 60)\n\tInsert(root, 80)\n\n\t//Print inoder traversal of the BST\n\tPrintInOrder(root)\n}\n"
  },
  {
    "path": "binary-search-tree-1-insertion/binary-search-tree-1-insertion_test.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestBinarySearchTreeNew(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tNode    *Node\n\t\tArrayIn []int\n\t\tTest    int\n\t\tOut     int\n\t}{\n\t\t{New(0), []int{20, 4, 15, 85}, 15, 15},\n\t\t{New(3), []int{10, 20, 30, 40, 50}, 50, 50},\n\t\t{New(7), []int{7}, 7, 7},\n\t}\n\n\tfor _, data := range testDatas {\n\t\tfor i := 0; i < len(data.ArrayIn); i++ {\n\t\t\tInsert(data.Node, data.ArrayIn[i])\n\t\t}\n\n\t\tactual := Search(data.Node, data.Test)\n\n\t\texpected := data.Out\n\n\t\tif !reflect.DeepEqual(expected, actual.data) {\n\t\t\tt.Errorf(\"BinarySearchTree: Expected: %d, Actual: %d\", expected, actual)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "binary-tree-1-introduction/binary-tree-1-introduction.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata  int\n\tleft  *Node\n\tright *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init(data int) *Node {\n\tn.data = data\n\tn.left = nil\n\tn.right = nil\n\treturn n\n}\n\n//Returns an new list\nfunc New(data int) *Node {\n\treturn new(Node).Init(data)\n}\n\nfunc main() {\n\t//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));\n\troot := New(1)\n\t/*\n\t\t        1\n\t\t      /   \\\n\t\t     NULL  NULL\n\t*/\n\n\troot.left = New(2)\n\troot.right = New(3)\n\t/* 2 and 3 become left and right children of 1\n\t           1\n\t         /   \\\n\t        2      3\n\t     /    \\    /  \\\n\t    NULL NULL NULL NULL\n\t*/\n\n\troot.left.left = New(4)\n\t/* 4 becomes left child of 2\n\t           1\n\t       /       \\\n\t      2          3\n\t    /   \\       /  \\\n\t   4    NULL  NULL  NULL\n\t  /  \\\n\tNULL NULL\n\t*/\n\n\tfmt.Println(\"Root data : \", root.data)\n\tfmt.Println(\"Root->Left  data : \", root.left.data)\n\tfmt.Println(\"Root->Right data : \", root.right.data)\n\tfmt.Println(\"Root->Left->Left data : \", root.left.left.data)\n}\n"
  },
  {
    "path": "binary-tree-1-introduction/binary-tree-1-introduction_test.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestBinaryTreeNew(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tNode  *Node\n\t\tData  int\n\t\tLeft  *Node\n\t\tRight *Node\n\t}{\n\t\t{New(-1), -1, New(-1), New(-1)},\n\t\t{New(0), 0, New(0), New(0)},\n\t\t{New(1), 1, New(1), New(1)},\n\t\t{New(7), 7, New(7), New(7)},\n\t}\n\n\tfor _, data := range testDatas {\n\t\tdata.Node.right = data.Right\n\t\tdata.Node.left = data.Left\n\n\t\texpected := data.Data\n\n\t\tactualLeft := data.Node.left.data\n\t\tactualRight := data.Node.right.data\n\n\t\tif !reflect.DeepEqual(expected, actualLeft) || !reflect.DeepEqual(expected, actualRight) {\n\t\t\tt.Errorf(\"BinaryTreeNew: Expected: %d, ActualLeft: %d, ActualRight: %d\", expected, actualLeft, actualRight)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "binary-tree-2-traversals-in-pre-post-order/binary-tree-traversals-2-in-pre-post-order.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata  int\n\tleft  *Node\n\tright *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init(data int) *Node {\n\tn.data = data\n\tn.left = nil\n\tn.right = nil\n\treturn n\n}\n\n//Returns an new list\nfunc New(data int) *Node {\n\treturn new(Node).Init(data)\n}\n\nfunc PrintPostOrder(node *Node) {\n\t//1. Check if the given node is NULL\n\tif node == nil {\n\t\treturn\n\t}\n\n\t//2. First recur on left subtree\n\tPrintPostOrder(node.left)\n\n\t//3. Then recur on right subtree\n\tPrintPostOrder(node.right)\n\n\t//4. Now deal with the node\n\tfmt.Printf(\"%d \", node.data)\n}\n\nfunc PrintInOrder(node *Node) {\n\t//1. Check if the given node is NULL\n\tif node == nil {\n\t\treturn\n\t}\n\n\t//2. First recur on left child\n\tPrintInOrder(node.left)\n\n\t//3. Then print the data of node\n\tfmt.Printf(\"%d \", node.data)\n\n\t//4. Now recur on right child\n\tPrintInOrder(node.right)\n}\n\nfunc PrintPreOrder(node *Node) {\n\t//1. Check if the given node is NULL\n\tif node == nil {\n\t\treturn\n\t}\n\n\t//2. First print data of node\n\tfmt.Printf(\"%d \", node.data)\n\n\t//3. Then recur on left sutree\n\tPrintPreOrder(node.left)\n\n\t//4. Now recur on right subtree\n\tPrintPreOrder(node.right)\n}\n\nfunc main() {\n\n\t//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));\n\troot := New(1)\n\n\troot.left = New(2)\n\troot.right = New(3)\n\n\troot.left.left = New(4)\n\troot.left.right = New(5)\n\n\tfmt.Println(\"\\nPreorder traversal of binary tree is :\")\n\tPrintPreOrder(root)\n\tfmt.Println(\"\\nInorder traversal of binary tree is :\")\n\tPrintInOrder(root)\n\tfmt.Println(\"\\nPostorder traversal of binary tree is :\")\n\tPrintPostOrder(root)\n}\n"
  },
  {
    "path": "binary-tree-2-traversals-level-order/binary-tree-traversals-2-level-order.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata  int\n\tleft  *Node\n\tright *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init(data int) *Node {\n\tn.data = data\n\tn.left = nil\n\tn.right = nil\n\treturn n\n}\n\n//Returns an new list\nfunc New(data int) *Node {\n\treturn new(Node).Init(data)\n}\n\n/* Compute the \"height\" of a tree -- the number of\n    nodes along the longest path from the root node\n    down to the farthest leaf node.*/\nfunc GetHeight(node *Node) int {\n\t//1. Check if the given node is NULL\n\tif node == nil {\n\t\treturn 0\n\t}\n\n\t//2. Compute the height of each subtree\n\tlheight := GetHeight(node.left)\n\trheight := GetHeight(node.right)\n\n\t//3. Use the larger one\n\tif lheight > rheight {\n\t\treturn lheight + 1\n\t}\n\treturn rheight + 1\n}\n\n/* Print nodes at a given level */\nfunc PrintGivenLevel(root *Node, level int) {\n\t//1. Check if the given root is NULL\n\tif root == nil {\n\t\treturn\n\t}\n\n\tif level == 1 {\n\t\tfmt.Printf(\"%d \", root.data)\n\t} else if level > 1 {\n\t\tPrintGivenLevel(root.left, level-1)\n\t\tPrintGivenLevel(root.right, level-1)\n\t}\n}\n\n/* Function to print level order traversal a tree*/\nfunc PrintLevelOrder(root *Node) {\n\t//1. Check if the given root is NULL\n\tif root == nil {\n\t\tfmt.Println(\"The given root node cannot be NULL\")\n\t\treturn\n\t}\n\n\th := GetHeight(root)\n\n\tfor i := 1; i <= h; i++ {\n\t\tPrintGivenLevel(root, i)\n\t}\n}\n\nfunc main() {\n\n\t//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));\n\troot := New(1)\n\n\troot.left = New(2)\n\troot.right = New(3)\n\n\troot.left.left = New(4)\n\troot.left.right = New(5)\n\n\tfmt.Println(\"\\nLevel Order traversal of binary tree is :\")\n\tPrintLevelOrder(root)\n}\n"
  },
  {
    "path": "binary-tree-3-doubly-linked-list/binary-tree-3-doubly-linked-list.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata  int\n\tleft  *Node\n\tright *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init(data int) *Node {\n\tn.data = data\n\tn.left = nil\n\tn.right = nil\n\treturn n\n}\n\n//Returns an new list\nfunc New(data int) *Node {\n\treturn new(Node).Init(data)\n}\n\n// Standard Inorder traversal of tree\nfunc InOrder(root *Node) {\n\t//1. Check if the given node is NULL\n\tif root == nil {\n\t\treturn\n\t}\n\n\tInOrder(root.left)\n\tfmt.Printf(\"%d \", root.data)\n\tInOrder(root.right)\n}\n\n// Changes left pointers to work as previous pointers in converted DLL\n// The function simply does inorder traversal of Binary Tree and updates\n// left pointer using previously visited node\nvar pre *Node = nil\n\nfunc FixPrevPtr(root *Node) {\n\t//1. Check if the given root is NULL\n\tif root == nil {\n\t\treturn\n\t}\n\n\tFixPrevPtr(root.left)\n\troot.left = pre\n\tpre = root\n\tFixPrevPtr(root.right)\n}\n\n// Changes right pointers to work as next pointers in converted DLL\nvar prev *Node = nil\n\nfunc FixNextPtr(root *Node) *Node {\n\t// Find the right most node in BT or last node in DLL\n\tfor root != nil && root.right != nil {\n\t\troot = root.right\n\t}\n\n\t// Start from the rightmost node, traverse back using left pointers.\n\t// While traversing, change right pointer of nodes.\n\tfor root != nil && root.left != nil {\n\t\tprev = root\n\t\troot = root.left\n\t\troot.right = prev\n\t}\n\n\t// The leftmost node is head of linked list, return it\n\treturn root\n}\n\nfunc BTToDLL(root *Node) *Node {\n\t// Set the previous pointer\n\tFixPrevPtr(root)\n\n\t// Set the next pointer and return head of DLL\n\treturn FixNextPtr(root)\n}\n\nfunc PrintList(root *Node) {\n\tfor root != nil {\n\t\tfmt.Printf(\"%d \", root.data)\n\t\troot = root.right\n\t}\n}\n\nfunc main() {\n\n\t//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));\n\troot := New(10)\n\n\troot.left = New(12)\n\troot.right = New(15)\n\n\troot.left.left = New(25)\n\troot.left.right = New(30)\n\n\troot.right.left = New(36)\n\n\tfmt.Println(\"\\nInorder Tree Traversal\")\n\tInOrder(root)\n\n\thead := BTToDLL(root)\n\tfmt.Println(\"\\nDLL Traversal\")\n\tPrintList(head)\n}\n"
  },
  {
    "path": "binary-tree-4-delete/binary-tree-4-delete.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata  int\n\tleft  *Node\n\tright *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init(data int) *Node {\n\tn.data = data\n\tn.left = nil\n\tn.right = nil\n\treturn n\n}\n\n//Returns an new list\nfunc New(data int) *Node {\n\treturn new(Node).Init(data)\n}\n\n/* Compute the \"height\" of a tree -- the number of\n    nodes along the longest path from the root node\n    down to the farthest leaf node.*/\nfunc DeleteTree(node *Node) {\n\t//1. Check if the given node is NULL\n\tif node == nil {\n\t\treturn\n\t}\n\n\t//2. First delete both subtrees\n\tDeleteTree(node.left)\n\tDeleteTree(node.right)\n\n\t//3. Then delete the node\n\tfmt.Printf(\"\\nDeleting node: %d\", node.data)\n\t//free(node);\n\tnode = nil\n}\n\nfunc main() {\n\n\t//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));\n\troot := New(1)\n\n\troot.left = New(2)\n\troot.right = New(3)\n\n\troot.left.left = New(4)\n\troot.left.right = New(5)\n\n\tDeleteTree(root)\n\troot = nil\n\n\tfmt.Println(\"\\nTree deleted\")\n}\n"
  },
  {
    "path": "binary-tree-5-find-min-max/binary-tree-5-find-min-max.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\nconst MaxUint = ^uint(0)\nconst MinUint = 0\nconst MaxInt = int(MaxUint >> 1)\nconst MinInt = -MaxInt - 1\n\ntype Node struct {\n\tdata  int\n\tleft  *Node\n\tright *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init(data int) *Node {\n\tn.data = data\n\tn.left = nil\n\tn.right = nil\n\treturn n\n}\n\n//Returns an new list\nfunc New(data int) *Node {\n\treturn new(Node).Init(data)\n}\n\n// Returns minimum value in a given Binary Tree\nfunc FindMin(root *Node) int {\n\t//1. Check if the given node is NULL\n\tif root == nil {\n\t\treturn MaxInt\n\t}\n\n\t//2. Return maximum of 3 values: 1) Root's data 2) Max in Left Subtree 3) Max in right subtree\n\tres := root.data\n\tlres := FindMin(root.left)\n\trres := FindMin(root.right)\n\n\tif lres < res {\n\t\tres = lres\n\t}\n\tif rres < res {\n\t\tres = rres\n\t}\n\n\treturn res\n}\n\n// Returns maximum value in a given Binary Tree\nfunc FindMax(root *Node) int {\n\t//1. Check if the given node is NULL\n\tif root == nil {\n\t\treturn MinInt\n\t}\n\n\t//2. Return maximum of 3 values: 1) Root's data 2) Max in Left Subtree 3) Max in right subtree\n\tres := root.data\n\tlres := FindMax(root.left)\n\trres := FindMax(root.right)\n\n\tif lres > res {\n\t\tres = lres\n\t}\n\tif rres > res {\n\t\tres = rres\n\t}\n\n\treturn res\n}\n\nfunc main() {\n\n\t//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));\n\troot := New(2)\n\n\troot.left = New(7)\n\troot.right = New(5)\n\n\troot.left.right = New(6)\n\troot.right.right = New(9)\n\n\troot.left.right.left = New(1)\n\troot.left.right.right = New(11)\n\n\troot.right.right.left = New(1)\n\n\tfmt.Printf(\"\\nMaximum element is %d\", FindMax(root))\n\tfmt.Printf(\"\\nMinimum element is %d\", FindMin(root))\n}\n"
  },
  {
    "path": "coverage.txt",
    "content": "mode: atomic\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:13.54,14.15 1 11\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:20.2,20.17 1 11\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:24.2,24.25 1 11\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:28.2,30.21 2 6\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:34.2,34.42 1 0\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:14.15,18.3 3 0\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:20.17,22.3 1 0\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:24.25,26.3 1 5\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:30.21,32.3 1 6\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:37.13,41.2 3 0\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:17.21,18.11 1 20\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:21.2,21.12 1 3\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:24.2,24.10 1 2\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:18.11,20.3 1 17\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:21.12,23.3 1 1\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:27.43,30.25 2 5\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:38.2,38.17 1 5\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:30.25,31.30 1 24\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:31.30,32.92 1 49\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:32.92,34.5 1 5\nData-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:41.13,47.2 5 0\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:19.37,24.2 4 12\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:27.26,29.2 1 12\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:32.40,34.37 1 11\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:40.2,40.21 1 8\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:45.2,45.31 1 1\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:34.37,37.3 1 3\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:40.21,42.3 1 7\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:49.31,50.17 1 0\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:50.17,54.3 3 0\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:58.40,60.17 1 33\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:65.2,65.21 1 24\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:72.2,72.13 1 24\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:60.17,62.3 1 9\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:65.21,67.3 1 2\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:67.8,67.28 1 22\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:67.28,69.3 1 21\nData-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:75.13,94.2 8 0\nData-Structures-with-Go/binary-tree-1-introduction/binary-tree-1-introduction.go:19.37,24.2 4 12\nData-Structures-with-Go/binary-tree-1-introduction/binary-tree-1-introduction.go:27.26,29.2 1 12\nData-Structures-with-Go/binary-tree-1-introduction/binary-tree-1-introduction.go:31.13,65.2 8 0\nData-Structures-with-Go/array-rotation/array-rotation.go:12.41,13.25 1 5\nData-Structures-with-Go/array-rotation/array-rotation.go:13.25,15.3 1 19\nData-Structures-with-Go/array-rotation/array-rotation.go:18.39,21.28 3 19\nData-Structures-with-Go/array-rotation/array-rotation.go:24.2,24.15 1 19\nData-Structures-with-Go/array-rotation/array-rotation.go:21.28,23.3 1 100\nData-Structures-with-Go/array-rotation/array-rotation.go:27.37,28.21 1 0\nData-Structures-with-Go/array-rotation/array-rotation.go:32.2,32.28 1 0\nData-Structures-with-Go/array-rotation/array-rotation.go:28.21,31.3 2 0\nData-Structures-with-Go/array-rotation/array-rotation.go:32.28,34.3 1 0\nData-Structures-with-Go/array-rotation/array-rotation.go:37.13,45.2 7 0\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:18.29,21.2 2 46\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:24.18,26.2 1 46\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:29.29,31.2 1 0\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:34.29,36.44 2 0\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:39.2,39.16 1 0\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:36.44,38.3 1 0\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:42.42,57.2 4 30\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:71.51,83.2 4 9\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:88.42,98.6 4 4\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:117.2,117.19 1 4\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:98.6,99.15 1 13\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:108.3,108.23 1 9\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:114.3,114.19 1 9\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:99.15,102.9 2 2\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:103.9,103.22 1 11\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:103.22,105.9 2 2\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:108.23,110.4 1 3\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:110.9,112.4 1 6\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:121.25,122.15 1 0\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:122.15,126.3 3 0\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:129.33,131.15 2 0\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:135.2,135.13 1 0\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:131.15,134.3 2 0\nData-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:138.13,157.2 12 0\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:18.29,21.2 2 19\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:24.18,26.2 1 19\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:29.29,31.2 1 16\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:34.29,36.44 2 0\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:39.2,39.16 1 0\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:36.44,38.3 1 0\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:42.42,57.2 4 10\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:59.31,64.21 4 3\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:71.2,71.18 1 3\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:64.21,69.3 4 13\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:75.24,76.15 1 0\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:76.15,79.3 2 0\nData-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:82.13,101.2 10 0\nData-Structures-with-Go/merge-sort/merge-sort.go:12.36,22.25 6 19\nData-Structures-with-Go/merge-sort/merge-sort.go:26.2,26.25 1 19\nData-Structures-with-Go/merge-sort/merge-sort.go:31.2,35.23 4 19\nData-Structures-with-Go/merge-sort/merge-sort.go:47.2,47.13 1 19\nData-Structures-with-Go/merge-sort/merge-sort.go:54.2,54.13 1 19\nData-Structures-with-Go/merge-sort/merge-sort.go:22.25,24.3 1 34\nData-Structures-with-Go/merge-sort/merge-sort.go:26.25,28.3 1 28\nData-Structures-with-Go/merge-sort/merge-sort.go:35.23,36.19 1 35\nData-Structures-with-Go/merge-sort/merge-sort.go:43.3,43.6 1 35\nData-Structures-with-Go/merge-sort/merge-sort.go:36.19,39.4 2 17\nData-Structures-with-Go/merge-sort/merge-sort.go:39.9,42.4 2 18\nData-Structures-with-Go/merge-sort/merge-sort.go:47.13,51.3 3 17\nData-Structures-with-Go/merge-sort/merge-sort.go:54.13,58.3 3 10\nData-Structures-with-Go/merge-sort/merge-sort.go:61.37,62.11 1 42\nData-Structures-with-Go/merge-sort/merge-sort.go:62.11,71.3 4 19\nData-Structures-with-Go/merge-sort/merge-sort.go:74.36,75.28 1 0\nData-Structures-with-Go/merge-sort/merge-sort.go:78.2,78.18 1 0\nData-Structures-with-Go/merge-sort/merge-sort.go:75.28,77.3 1 0\nData-Structures-with-Go/merge-sort/merge-sort.go:81.13,92.2 7 0\nData-Structures-with-Go/quick-sort/quick-sort.go:12.27,16.2 3 38\nData-Structures-with-Go/quick-sort/quick-sort.go:18.47,25.34 3 15\nData-Structures-with-Go/quick-sort/quick-sort.go:33.2,35.16 2 15\nData-Structures-with-Go/quick-sort/quick-sort.go:25.34,27.22 1 49\nData-Structures-with-Go/quick-sort/quick-sort.go:27.22,30.4 2 23\nData-Structures-with-Go/quick-sort/quick-sort.go:43.43,44.17 1 34\nData-Structures-with-Go/quick-sort/quick-sort.go:44.17,51.3 3 15\nData-Structures-with-Go/quick-sort/quick-sort.go:54.38,55.28 1 0\nData-Structures-with-Go/quick-sort/quick-sort.go:58.2,58.18 1 0\nData-Structures-with-Go/quick-sort/quick-sort.go:55.28,57.3 1 0\nData-Structures-with-Go/quick-sort/quick-sort.go:61.13,69.2 5 0\nData-Structures-with-Go/stack/stack.go:24.44,29.2 4 4\nData-Structures-with-Go/stack/stack.go:32.32,34.2 1 4\nData-Structures-with-Go/stack/stack.go:37.32,39.2 1 14\nData-Structures-with-Go/stack/stack.go:42.33,44.2 1 14\nData-Structures-with-Go/stack/stack.go:46.35,47.19 1 14\nData-Structures-with-Go/stack/stack.go:50.2,51.31 2 14\nData-Structures-with-Go/stack/stack.go:47.19,49.3 1 0\nData-Structures-with-Go/stack/stack.go:54.28,55.20 1 14\nData-Structures-with-Go/stack/stack.go:58.2,60.13 3 14\nData-Structures-with-Go/stack/stack.go:55.20,57.3 1 0\nData-Structures-with-Go/stack/stack.go:63.13,74.2 8 0\n"
  },
  {
    "path": "linked-list-1-introduction/README.md",
    "content": "<h1 align=\"center\">LinkedList | SET 1 (INTRODUCTION) Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\nLike arrays, LinkedList is a linear dat structure. Unlike arrays, LinkedList elements are not stored at contigous location; the elements are linked using pointers.\n\n![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-1-introduction/resources/linked-list.png)\n\nWhy Linked List ?\n--------------------------\n\nArrays can be used to store linear data of similar types, but arrays have following limitations ;\n\n* 1) The size of the arrays is fixed. So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to upper limit irrespective of the usage.\n\n* 2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted.\n\n**Example**\n\n> * ID[] = [1000, 1010, 1050, 2000, 2040]\n\nAnd if we want to insert a new ID 10005, the to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000).\n\nDeletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in ID[], everythink after 1010 has to moved.\n\nAdvantages Over Arrays\n--------------------------\n\n* 1) Dynamic size\n\n* 2) Ease of insertion/deletion\n\nDrawbacks\n--------------------------\n\n* 1) Random access is not allowed. We have to access elements sequientally starting from the first node. So we cannot do binary search with LinkedLists.\n\n* 2) Extra memory space for a pointer is required with each element of the list."
  },
  {
    "path": "linked-list-1-introduction/linked-list-1-introduction.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata int\n\tnext *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init() *Node {\n\tn.data = -1\n\treturn n\n}\n\n//Returns an new list\nfunc New() *Node {\n\treturn new(Node).Init()\n}\n\n//Returns the first node in list\nfunc (n *Node) Next() *Node {\n\treturn n.next\n}\n  \n//Returns the last node in list if exist, otherwise returns current\nfunc (n *Node) Back() *Node {\n\tcurrent := n.next\n\tfor current != nil && current.next != nil {\n\t\tcurrent = current.next\n\t}\n\treturn current\n}\n\n//This function prints contents of linked list starting from the given node\nfunc printList(n *Node){\n\tfor n != nil {\n\t\tfmt.Println(n.data)\n\t\tn = n.next\n\t}\n}\n\nfunc main() {\n\t//To allocate dynamically a new Node in C language : head = (struct Node*) malloc(sizeof(struct Node));\n\thead := New()\n\tsecond := New()\n\tthird := New()\n\n\t//Assign data in first node\n\thead.data = 1\n\t//Link first node with second node\n\thead.next = second\n\n\tsecond.data = 2\n\tsecond.next = third\n\n\tthird.data = 3\n\tthird.next = nil\n\n\tprintList(head)\n}"
  },
  {
    "path": "linked-list-2-inserting-a-node/README.md",
    "content": "<h1 align=\"center\">LinkedList | SET 2 (INSERTING A NODE) Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\nWe have introduced Linked Lists in the **[previous post](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/linked-list-1-introduction)**. We also created a simple linked list with 3 nodes and discussed linked list traversal.\nAll programs discussed in this post consider following representations of linked list .\n\nIn this post, methods to insert a new node in linked list are discussed. A node can be added in three ways ;\n\n* 1) At the front of the linked list\n* 2) After a given node.\n* 3) At the end of the linked list.\n\n\nAdd a node at the front (A 4 steps process)\n--------------------------\n\nThe new node is always added before the head of the given Linked List. And newly added node becomes the new head of the Linked List. For example if the given Linked List is `10->15->20->25` and we add an item 5 at the front, then the Linked List becomes `5->10->15->20->25`. Let us call the function that adds at the front of the list is `push()`. The `push()` must receive a pointer to the head pointer, because push must change the head pointer to point to the new node.\n\n![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-2-inserting-a-node/resources/inserting-a-node-1.png)\n\n**Algorithm Complexity**\n\n| Complexity\t\t| Notation  |\n| ----------------- |:---------:|\n| `Time Complexity`\t| `O(1)`    |\n\n\nAdd a node after a given node (5 steps process)\n--------------------------\n\nWe are given pointer to a node, and the new node is inserted after the given node.\n\n![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-2-inserting-a-node/resources/inserting-a-node-2.png)\n\n**Algorithm Complexity**\n\n| Complexity\t\t| Notation  |\n| ----------------- |:---------:|\n| `Time Complexity`\t| `O(1)`    |\n\n\nAdd a node at the end (6 steps process)\n--------------------------\n\nThe new node is always added after the last node of the given Linked List. For example if the given Linked List is `5->10->15->20->25` and we add an item 30 at the end, then the Linked List becomes `5->10->15->20->25->30`.\nSince a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.\n\n![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-2-inserting-a-node/resources/inserting-a-node-3.png)\n\n**Algorithm Complexity**\n\n| Complexity\t\t| Notation  |\n| ----------------- |:---------:|\n| `Time Complexity`\t| `O(n)`    |"
  },
  {
    "path": "linked-list-2-inserting-a-node/linked-list-2-inserting-a-node.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata int\n\tnext *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init() *Node {\n\tn.data = -1\n\treturn n\n}\n\n//Returns an new list\nfunc New() *Node {\n\treturn new(Node).Init()\n}\n\n//Returns the first node in list\nfunc (n *Node) Next() *Node {\n\treturn n.next\n}\n  \n//Returns the last node in list if exist, otherwise returns current\nfunc (n *Node) Back() *Node {\n\tcurrent := n.next\n\tfor current != nil && current.next != nil {\n\t\tcurrent = current.next\n\t}\n\treturn current\n}\n\nfunc Push(head_ref **Node, new_data int) {\n\t//new_node := Node{data: new_data, next: (*head_ref)}\n\t//*head_ref = new_node\n\n\t//1. Allocate new node\n\tnew_node := New()\n\n\t//2. Put in the data\n\tnew_node.data = new_data\n\n\t//3. Make next of new node as head\n\tnew_node.next = (*head_ref)\n\n\t//4. Move the head to point to new_node\n\t*head_ref = new_node\n}\n\nfunc InsertAfter(prev_node *Node, new_data int) {\n\t//1. Check if the given prev_node is NULL\n\tif(prev_node == nil){\n\t\tfmt.Println(\"The given previous node cannot be NULL\")\n\t\treturn\n\t}\n\n\t//2. Allocate new node\n\tnew_node := New()\n\n\t//3. Put in the data\n\tnew_node.data = new_data\n\n\t//4. Make next of new node as next of prev_node\n\tnew_node.next = prev_node.next\n\n\t//5. Move the next of prev_node as new_node\n\tprev_node.next = new_node\n}\n\nfunc Append(head_ref **Node, new_data int) {\n\t//1. Allocate new node\n\tnew_node := New()\n\n\tlast := *head_ref\n\n\t//2. Put in the data\n\tnew_node.data = new_data\n\n\t//3. this new node is going to be last node, so make next of it as NULL\n\tnew_node.next = nil\n\n\t//4. If the LinkedList is empty, then make the new node as head\n\tif(*head_ref == nil){\n\t\t*head_ref = new_node\n\t\treturn\n\t}\n\n\t//5. Else traverse till the last node\n\tfor(last.next != nil){\n\t\tlast = last.next\n\t}\n\n\t//6. Change the next of last node\n\tlast.next = new_node\n\treturn\n}\n\n//This function prints contents of linked list starting from the given node\nfunc printList(n *Node){\n\tfor n != nil {\n\t\tfmt.Println(n.data)\n\t\tn = n.next\n\t}\n}\n\nfunc main() {\n\t//Start with the empty list\n\thead := New()\n\t\n\t//Insert 6.\n\t//So LinkedList becomes [NULL->6]\n\tAppend(&head, 6)\n\n\t//Insert 7 at the beginning. \n\t//So LinkedList becomes [7->NULL->6]\n\tPush(&head, 7)\n\n\t//Insert 1 at the beginning. \n\t//So LinkedList becomes [1->7->NULL->6]\n\tPush(&head, 1)\n\n\t//Insert 4 at the end. \n\t//So LinkedList becomes [1->7->NULL->6->4]\n\tAppend(&head, 4)\n\n\t//Insert 8, after 7\n\t//So LinkedList becomes [1->7->8->NULL->6->4]\n\tInsertAfter(head.next, 8)\n\n\tfmt.Println(\"Created LinkedList is: \")\n\n\tprintList(head)\n}"
  },
  {
    "path": "linked-list-3-deleting-a-node/README.md",
    "content": "<h1 align=\"center\">LinkedList | SET 3 (DELETING A NODE) Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\nWe have discussed **[Linked List Introduction](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/linked-list-1-introduction)** and **[Linked List Insertion](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/linked-list-2-inserting-a-node)** in previous posts on singly linked list.\nLet us formulate the problem statement to understand the deletion process. Given a `key`, delete the first occurrence of this key in linked list.\n\nTo delete a node from linked list, we need to do following steps.\n\n* 1) Find previous node of the node to be deleted.\n* 2) Changed next of previous node.\n* 3) Free memory for the node to be deleted.\n\n> * Input: Linked List = `[7 -> 1 -> 3 -> 2]`\n> * Output: Created Linked List `[2 -> 3 -> 1 -> 7]`\n> * Output: Linked List after Deletion of 1: `[2 -> 3 -> 7]`\n\n> * Input: Position = 1, Linked List = `[8 -> 2 -> 3 -> 1 -> 7]`\n> * Output: Linked List =  `[8 -> 3 -> 1 -> 7]`\n\n> * Input: Position = 0, Linked List = `[8 -> 2 -> 3 -> 1 -> 7]`\n> * Output: Linked List =  `[2 -> 3 -> 1 -> 7]`\n\n\n\n**Algorithm Complexity**\n\n| Complexity\t\t| Notation  |\n| ----------------- |:---------:|\n| `Time Complexity`\t| `O(1)`    |\n"
  },
  {
    "path": "linked-list-3-deleting-a-node/linked-list-3-deleting-a-node.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata int\n\tnext *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init() *Node {\n\tn.data = -1\n\treturn n\n}\n\n//Returns an new list\nfunc New() *Node {\n\treturn new(Node).Init()\n}\n\n//Returns the first node in list\nfunc (n *Node) Next() *Node {\n\treturn n.next\n}\n  \n//Returns the last node in list if exist, otherwise returns current\nfunc (n *Node) Back() *Node {\n\tcurrent := n.next\n\tfor current != nil && current.next != nil {\n\t\tcurrent = current.next\n\t}\n\treturn current\n}\n\nfunc Push(head_ref **Node, new_data int) {\n\t//new_node := Node{data: new_data, next: (*head_ref)}\n\t//*head_ref = new_node\n\n\t//1. Allocate new node\n\tnew_node := New()\n\n\t//2. Put in the data\n\tnew_node.data = new_data\n\n\t//3. Make next of new node as head\n\tnew_node.next = (*head_ref)\n\n\t//4. Move the head to point to new_node\n\t*head_ref = new_node\n}\n\nfunc DeleteNodeWithData(head_ref **Node, delete_data int) {\n\t//Store head node\n\ttemp := *head_ref\n\tprev := *head_ref\n\n\t//If head node itself holds the delete_data to be deleted\n\tif(temp != nil && temp.data == delete_data){\n\t\t*head_ref = temp.next;\n\t\ttemp = nil\n\t\treturn\n\t}\n\n\t//Search for the delete_data to be deleted, keep track of the previous node as we need to change prev->next\n\tfor(temp != nil && temp.data != delete_data){\n\t\tprev = temp\n\t\ttemp = temp.next\n\t}\n\n\t// If delete_data was not present in linked list\n\tif(temp == nil){\n\t\treturn\n\t}\n\n\t//Unlink the node from linked list\n\tprev.next = temp.next\n\n\ttemp = nil\n}\n\nfunc DeleteNodeWithPosition(head_ref **Node, delete_position int){\n\t//If LinkedList is empty\n\tif(*head_ref == nil){\n\t\treturn\n\t}\n\n\t//Store head node\n\ttemp := *head_ref\n\n\t//If head needs to be removed\n\tif(delete_position == 0){\n\t\t*head_ref = temp.next\n\t\ttemp = nil\n\t\treturn\n\t}\n\n\t//Find previous node of the node to be deleted\n\tfor i := 0; temp != nil && i < delete_position - 1; i++ {\n\t\ttemp = temp.next\n\t}\n\n\t//If position is more than number of nodes\n\tif(temp == nil || temp.next == nil){\n\t\treturn\n\t}\n\n\t//Node temp->next is the node to be deleted, Store pointer to the next of node to be deleted\n\tnext := temp.next.next\n\n\t//Unlink the node from linked list\n\ttemp.next = nil\n\n\t//Unlink the deleted node from list\n\ttemp.next = next\n}\n\n//This function prints contents of linked list starting from the given node\nfunc printList(n *Node){\n\tfor n != nil {\n\t\tfmt.Println(n.data)\n\t\tn = n.next\n\t}\n}\n\nfunc main() {\n\t//Start with the empty list\n\thead := New()\n\t\n\tPush(&head, 7)\n\n\tPush(&head, 1)\n\n\tPush(&head, 3)\n\n\tPush(&head, 2)\n\n\tPush(&head, 8)\n\n\tfmt.Println(\"Created LinkedList is: \")\n\n\tprintList(head)\n\n\tDeleteNodeWithData(&head, 1)\n\n\tDeleteNodeWithPosition(&head, 4)\n\n\tfmt.Println(\"LinkedList after Deletion of 1: \")\n\n\tprintList(head)\n}"
  },
  {
    "path": "linked-list-circular-1-introduction/README.md",
    "content": ""
  },
  {
    "path": "linked-list-circular-2-traversal/README.md",
    "content": ""
  },
  {
    "path": "linked-list-circular-2-traversal/linked-list-circular-2-traversal.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata int\n\tnext *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init() *Node {\n\tn.data = -1\n\treturn n\n}\n\n//Returns an new list\nfunc New() *Node {\n\treturn new(Node).Init()\n}\n\n//Returns the first node in list\nfunc (n *Node) Next() *Node {\n\treturn n.next\n}\n\n//Returns the last node in list if exist, otherwise returns current\nfunc (n *Node) Back() *Node {\n\tcurrent := n.next\n\tfor current != nil && current.next != nil {\n\t\tcurrent = current.next\n\t}\n\treturn current\n}\n\n//This function prints contents of linked list starting from the given node\nfunc printList(n *Node) {\n\tfor n != nil {\n\t\tfmt.Println(n.data)\n\t\tn = n.next\n\t}\n}\n\nfunc Push(head_ref **Node, new_data int) {\n\t//1. Allocate new node\n\tnew_node := New()\n\ttemp := *head_ref\n\n\tnew_node.data = new_data\n\tnew_node.next = *head_ref\n\n\t//2. If linked list is not NULL then set the next of last node\n\tif *head_ref != nil {\n\t\tfor temp.next != *head_ref {\n\t\t\ttemp = temp.next\n\t\t}\n\t\ttemp.next = new_node\n\t} else {\n\t\t//For the fist node\n\t\tnew_node.next = new_node\n\t}\n\t//4. Move the head to point to new_node\n\t*head_ref = new_node\n}\n\nfunc main() {\n\t//Start with the empty list\n\thead := New()\n\n\tPush(&head, 12)\n\tPush(&head, 56)\n\tPush(&head, 2)\n\tPush(&head, 11)\n\n\tfmt.Println(\"Created Circular LinkedList is: \")\n\n\tprintList(head)\n}\n"
  },
  {
    "path": "linked-list-circular-singly-1-insertion/README.md",
    "content": ""
  },
  {
    "path": "linked-list-circular-singly-1-insertion/linked-list-circular-singly-1-insertion.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata int\n\tnext *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init() *Node {\n\tn.data = -1\n\treturn n\n}\n\n//Returns an new list\nfunc New() *Node {\n\treturn new(Node).Init()\n}\n\n//Returns the first node in list\nfunc (n *Node) Next() *Node {\n\treturn n.next\n}\n  \n//Returns the last node in list if exist, otherwise returns current\nfunc (n *Node) Back() *Node {\n\tcurrent := n.next\n\tfor current != nil && current.next != nil {\n\t\tcurrent = current.next\n\t}\n\treturn current\n}\n\n//Function to insert node in an empty List\nfunc AddToEmpty(last *Node, data int) *Node {\n\tif(last != nil){\n\t\treturn last\n\t}\n\n\t//Creating a node\n\ttemp := New()\n\n\t//Assigning the data\n\ttemp.data = data\n\tlast = temp\n\n\t//Creating the link\n\tlast.next = last\n\n\treturn last\n}\n\n//Function to insert node in the beginning of the List\nfunc AddBegin(last *Node, data int) *Node {\n\tif(last == nil){\n\t\treturn AddToEmpty(last, data)\n\t}\n\n\t//Creating a node\n\ttemp := New()\n\n\t//Assigning the data\n\ttemp.data = data\n\n\t//List was empty. We link single node to itself\n\ttemp.next = last.next\n\tlast.next = temp\n\n\treturn last\n}\n\n//Function to insert node in the end of the List\nfunc AddEnd(last *Node, data int) *Node {\n\tif(last == nil){\n\t\treturn AddToEmpty(last, data)\n\t}\n\n\t//Creating a node\n\ttemp := New()\n\n\t//Assigning the data\n\ttemp.data = data\n\n\t//List was empty. We link single node to itself\n\ttemp.next = last.next\n\tlast.next = temp\n\tlast = temp\n\n\treturn last\n}\n\n//Function to insert node in the end of the List\nfunc AddAfter(last *Node, data int, item int) *Node {\n\tif(last == nil){\n\t\treturn nil\n\t}\n\n\t//Creating a node\n\ttemp := New()\n\tp := New()\n\n\tp = last.next\n\n\tfor(p != last.next){\n\t\tif(p.data == item){\n\t\t\t//Assigning the data\n\t\t\ttemp.data = data\n\t\t\t//Adjusting the links\n\t\t\ttemp.next = p.next\n\n\t\t\t//Adding newly allocated node after p\n\t\t\tp.next = temp\n\n\t\t\t//Checking for the last node\n\t\t\tif(p == last){\n\t\t\t\tlast = temp\n\t\t\t}\n\n\t\t\treturn last\n\t\t}\n\t\tp = p.next\n\t}\n\n\tfmt.Printf(\"\\n%d not present in the list.\\n\", item)\n\n\treturn last\n}\n\nfunc Traverse(last *Node){\n\tp := New()\n\n\t//If list is empty, return\n\tif(last == nil){\n\t\tfmt.Println(\"List is empty\")\n\t\treturn\n\t}\n\n\t//Pointing to first Node of the list\n\tp = last.next\n\n\t//Traversing the list\n\tfor p != nil {\n\t\tfmt.Println(p.data)\n\t\tp = p.next\n\t}\n}\n\n//This function prints contents of linked list starting from the given node\nfunc printList(n *Node){\n\tfor n != nil {\n\t\tfmt.Println(n.data)\n\t\tn = n.next\n\t}\n}\n\nfunc main() {\n\tlast := New()\n\n\tlast = AddEnd(last, 12)\n\tlast = AddToEmpty(last, 6)\n\tlast = AddBegin(last, 4)\n\tlast = AddBegin(last, 2)\n\tlast = AddEnd(last, 8)\n\tlast = AddAfter(last, 10, 8)\n\n\tTraverse(last)\n}"
  },
  {
    "path": "linked-list-find-length/README.md",
    "content": "<h1 align=\"center\">LinkedList Find Length Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\nFor example, the function should return 5 for linked list `[1 -> 3 -> 1 -> 2 -> 1]`\n\n![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-find-length/resources/find-length.png)\n\n\nMETHOD 1 (Iterative Solution)\n--------------------------\n\n* 1) Initialize count as 0 \n* 2) Initialize a node pointer, current = head.\n* 3) Do following while current is not NULL\n     a) current = current -> next\n     b) count++;\n* 4) Return count \n\n> * Input: Linked List = `[1 -> 3 -> 1 -> 2 -> 1]`\n> * Output: count of nodes is `5`\n\n**Algorithm Complexity**\n\n| Complexity\t\t| Notation  |\n| ----------------- |:---------:|\n| `Time Complexity`\t| `O(n)`    |\n"
  },
  {
    "path": "linked-list-find-length/linked-list-find-length.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata int\n\tnext *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init() *Node {\n\tn.data = -1\n\treturn n\n}\n\n//Returns an new list\nfunc New() *Node {\n\treturn new(Node).Init()\n}\n\n//Returns the first node in list\nfunc (n *Node) Next() *Node {\n\treturn n.next\n}\n  \n//Returns the last node in list if exist, otherwise returns current\nfunc (n *Node) Back() *Node {\n\tcurrent := n.next\n\tfor current != nil && current.next != nil {\n\t\tcurrent = current.next\n\t}\n\treturn current\n}\n\nfunc Push(head_ref **Node, new_data int) {\n\t//new_node := Node{data: new_data, next: (*head_ref)}\n\t//*head_ref = new_node\n\n\t//1. Allocate new node\n\tnew_node := New()\n\n\t//2. Put in the data\n\tnew_node.data = new_data\n\n\t//3. Make next of new node as head\n\tnew_node.next = (*head_ref)\n\n\t//4. Move the head to point to new_node\n\t*head_ref = new_node\n}\n\nfunc GetCount(head *Node) int {\n\tvar count int = 0\n\tcurrent := head\n\n\tfor(current != nil){\n\t\tcount++\n\t\tcurrent = current.next\n\t}\n\n\treturn count\n}\n\n//This function prints contents of linked list starting from the given node\nfunc printList(n *Node){\n\tfor n != nil {\n\t\tfmt.Println(n.data)\n\t\tn = n.next\n\t}\n}\n\nfunc main() {\n\t//Start with the empty list\n\thead := New()\n\t\n\tPush(&head, 1)\n\n\tPush(&head, 3)\n\n\tPush(&head, 1)\n\n\tPush(&head, 2)\n\n\tPush(&head, 1)\n\n\tfmt.Printf(\"Count of Nodes is %d\", GetCount(head))\n}"
  },
  {
    "path": "linked-list-merge-two-sorted/README.md",
    "content": "<h1 align=\"center\">Merge Two Sorted Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\nWrite a `SortedMerge()` function that takes two lists, each of which is sorted in increasing order, and merges the two together into one list which is in increasing order. `SortedMerge()` should return the new list. The new list should be made by splicing\ntogether the nodes of the first two lists.\n\n> * Input: LinkedList a `5->10->15`\n> * Input: LinkedList b `2->3->20`\n> * Output: `2->3->5->10->15->20`\n\nThere are many cases to deal with: either `a` or `b` may be empty, during processing either `a` or `b` may run out first, and finally there’s the problem of starting the result list empty, and building it up while going through `a` and `b`.\n\nMETHOD 1 (Using Dummy Nodes)\n--------------------------\n\nThe strategy here uses a temporary dummy node as the start of the result list. The pointer Tail always points to the last node in the result list, so appending new nodes is easy.\nThe dummy node gives tail something to point to initially when the result list is empty. This dummy node is efficient, since it is only temporary, and it is allocated in the stack. The loop proceeds, removing one node from either `a` or `b`, and adding it to tail. When\nwe are done, the result is in dummy.next\n\n**Algorithm Complexity**\n\n| Complexity\t    | Notation  |\n| ----------------- |:---------:|\n| `Time Complexity`\t| `O(n)`    |"
  },
  {
    "path": "linked-list-merge-two-sorted/linked-list-merge-two-sorted.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata int\n\tnext *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init() *Node {\n\tn.data = -1\n\treturn n\n}\n\n//Returns an new list\nfunc New() *Node {\n\treturn new(Node).Init()\n}\n\n//Returns the first node in list\nfunc (n *Node) Next() *Node {\n\treturn n.next\n}\n\n//Returns the last node in list if exist, otherwise returns current\nfunc (n *Node) Back() *Node {\n\tcurrent := n.next\n\tfor current != nil && current.next != nil {\n\t\tcurrent = current.next\n\t}\n\treturn current\n}\n\nfunc Push(head_ref **Node, new_data int) {\n\t//new_node := Node{data: new_data, next: (*head_ref)}\n\t//*head_ref = new_node\n\n\t//1. Allocate new node\n\tnew_node := New()\n\n\t//2. Put in the data\n\tnew_node.data = new_data\n\n\t//3. Make next of new node as head\n\tnew_node.next = (*head_ref)\n\n\t//4. Move the head to point to new_node\n\t*head_ref = new_node\n}\n\n//Pull off the front node of the source and put it in dest\n/* MoveNode() function takes the node from the front of the\n   source, and move it to the front of the dest.\n   It is an error to call this with the source list empty.\n\n   Before calling MoveNode():\n   source == {1, 2, 3}\n   dest == {1, 2, 3}\n\n   Affter calling MoveNode():\n   source == {2, 3}\n   dest == {1, 1, 2, 3} */\nfunc MoveNode(dest_ref **Node, source_ref **Node) {\n\t//The front source code\n\tnew_node := *source_ref\n\n\t//Advance the source pointer\n\t*source_ref = new_node.next\n\n\t//Link the old dest off the new node\n\tnew_node.next = *dest_ref\n\n\t//Move dest to point to the new node\n\t*dest_ref = new_node\n}\n\n/* Takes two lists sorted in increasing order, and splices\n   their nodes together to make one big sorted list which\n   is returned.  */\nfunc SortedMerge(a *Node, b *Node) *Node {\n\t//A dummy first node to hang the result on\n\tdummy := New()\n\n\t//Tail points to the last result node\n\ttail := dummy\n\n\t//So tail.next is the place to add new nodes to result\n\tdummy.next = nil\n\n\tfor {\n\t\tif a == nil {\n\t\t\t//If either list runs out, use the other list\n\t\t\ttail.next = b\n\t\t\tbreak\n\t\t} else if b == nil {\n\t\t\ttail.next = a\n\t\t\tbreak\n\t\t}\n\n\t\tif a.data <= b.data {\n\t\t\tMoveNode(&(tail.next), &a)\n\t\t} else {\n\t\t\tMoveNode(&(tail.next), &b)\n\t\t}\n\n\t\ttail = tail.next\n\t}\n\n\treturn dummy.next\n}\n\n//This function prints contents of linked list starting from the given node\nfunc printList(n *Node) {\n\tfor n != nil {\n\t\tfmt.Print(n.data)\n\t\tfmt.Print(\" ,\")\n\t\tn = n.next\n\t}\n}\n\nfunc GetDataList(n *Node) []int {\n\tdata := []int{}\n\tfor n != nil {\n\t\tdata = append(data, n.data)\n\t\tn = n.next\n\t}\n\treturn data\n}\n\nfunc main() {\n\t//Start with the empty list\n\tres := New()\n\ta := New()\n\tb := New()\n\n\tPush(&a, 15)\n\tPush(&a, 10)\n\tPush(&a, 5)\n\n\tPush(&b, 20)\n\tPush(&b, 3)\n\tPush(&b, 2)\n\n\t//Remove duplicates from LinkedList\n\tres = SortedMerge(a, b)\n\n\tfmt.Println(\"Merged LinkedList is:\")\n\tprintList(res)\n}\n"
  },
  {
    "path": "linked-list-merge-two-sorted/linked-list-merge-two-sorted.go_test.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestMergeTwoSortedNew(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tArrayIn1 []int\n\t\tArrayIn2 []int\n\t\tOut      []int\n\t}{\n\t\t{[]int{20, 4, 15, 85}, []int{6, 7}, []int{20, 4, 15, 85, -1, 6, 7}},\n\t\t{[]int{10, 20, 30}, []int{5, 6}, []int{10, 20, 30, -1, 5, 6}},\n\t\t{[]int{7}, []int{7}, []int{7, -1, 7}},\n\t\t{[]int{}, []int{}, []int{-1}},\n\t}\n\n\tfor _, data := range testDatas {\n\n\t\ta := New()\n\t\tb := New()\n\n\t\tfor i := 0; i < len(data.ArrayIn1); i++ {\n\t\t\tPush(&a, data.ArrayIn1[i])\n\t\t}\n\n\t\tfor i := 0; i < len(data.ArrayIn2); i++ {\n\t\t\tPush(&b, data.ArrayIn2[i])\n\t\t}\n\n\t\tactual := SortedMerge(a, b)\n\n\t\texpected := New()\n\n\t\tfor i := 0; i < len(data.Out); i++ {\n\t\t\tPush(&expected, data.Out[i])\n\t\t}\n\n\t\tif !reflect.DeepEqual(actual, expected) {\n\t\t\tt.Errorf(\"MergeTwoSorted: Expected: %v, Actual: %v\", GetDataList(expected), GetDataList(actual))\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "linked-list-reverse/README.md",
    "content": "<h1 align=\"center\">LinkedList Reverse Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\nGiven pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes.\n\n\n> * Input: Head of following linked list `[1->2->3->4->NULL]`\n> * Output: Linked list should be changed to `[4->3->2->1->NULL]`\n\n> * Input: Head of following linked list `[1->2->3->4->5->NULL]`\n> * Output: Linked list should be changed to `[5->4->3->2->1->NULL]`\n\n**Algorithm Complexity**\n\n| Complexity\t\t| Notation  |\n| ----------------- |:---------:|\n| `Time Complexity`\t| `O(n)`    |\n| `Auxiliary Space` | `O(1)`    |"
  },
  {
    "path": "linked-list-reverse/linked-list-reverse.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\ntype Node struct {\n\tdata int\n\tnext *Node\n}\n\n//Returns an initialized list\nfunc (n *Node) Init() *Node {\n\tn.data = -1\n\treturn n\n}\n\n//Returns an new list\nfunc New() *Node {\n\treturn new(Node).Init()\n}\n\n//Returns the first node in list\nfunc (n *Node) Next() *Node {\n\treturn n.next\n}\n  \n//Returns the last node in list if exist, otherwise returns current\nfunc (n *Node) Back() *Node {\n\tcurrent := n.next\n\tfor current != nil && current.next != nil {\n\t\tcurrent = current.next\n\t}\n\treturn current\n}\n\nfunc Push(head_ref **Node, new_data int) {\n\t//new_node := Node{data: new_data, next: (*head_ref)}\n\t//*head_ref = new_node\n\n\t//1. Allocate new node\n\tnew_node := New()\n\n\t//2. Put in the data\n\tnew_node.data = new_data\n\n\t//3. Make next of new node as head\n\tnew_node.next = (*head_ref)\n\n\t//4. Move the head to point to new_node\n\t*head_ref = new_node\n}\n\nfunc Reverse(head_ref **Node) {\n\tprev := New()\n\tcurrent := *head_ref\n\tnext := New()\n\n\tfor(current != nil){\n\t\tnext = current.next\n\t\tcurrent.next = prev\n\t\tprev = current\n\t\tcurrent = next\n\t}\n\n\t*head_ref = prev\n}\n\n//This function prints contents of linked list starting from the given node\nfunc printList(n *Node){\n\tfor n != nil {\n\t\tfmt.Println(n.data)\n\t\tn = n.next\n\t}\n}\n\nfunc main() {\n\t//Start with the empty list\n\thead := New()\n\t\n\tPush(&head, 20)\n\n\tPush(&head, 4)\n\n\tPush(&head, 15)\n\n\tPush(&head, 85)\n\n\tfmt.Println(\"Given LinkedList is:\")\n\tprintList(head)\n\n\tReverse(&head)\n\n\tfmt.Println(\"Reversed LinkedList is:\")\n\tprintList(head)\n}"
  },
  {
    "path": "linked-list-reverse/linked-list-reverse_test.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestLinkedListReverse(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tNode     *Node\n\t\tArrayIn  []int\n\t\tArrayOut []int\n\t}{\n\t\t{New(), []int{20, 4, 15, 85}, []int{-1, 20, 4, 15, 85, -1}},\n\t\t{New(), []int{10, 20, 30, 40, 50}, []int{-1, 10, 20, 30, 40, 50, -1}},\n\t\t{New(), []int{7}, []int{-1, 7, -1}},\n\t}\n\n\tfor _, data := range testDatas {\n\t\tfor i := 0; i < len(data.ArrayIn); i++ {\n\t\t\tPush(&data.Node, data.ArrayIn[i])\n\t\t}\n\n\t\tReverse(&data.Node)\n\n\t\tn := data.Node\n\t\ti := 0\n\t\tfor n != nil {\n\t\t\texpected := data.ArrayOut[i]\n\t\t\tactual := n.data\n\n\t\t\tif !reflect.DeepEqual(expected, actual) {\n\t\t\t\tt.Errorf(\"LinkedListReverse: Expected: %d, Actual: %d\", expected, actual)\n\t\t\t}\n\n\t\t\tn = n.Next()\n\t\t\ti++\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "merge-sort/README.md",
    "content": "<h1 align=\"center\">Merge Sort Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\nLike **[QuickSort](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/quick-sort)**, `MergeSort` is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The `merge()` function is used for merging two halves. The `merge(arr, l, m, r)` is key process that assumes that `arr[l..m]` and `arr[m+1..r]` are sorted and merges the two sorted sub-arrays into one. See following C implementation for details\n\n![Preview Thumbnail](https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif)\n\nMergeSort(arr[], l,  r)\n--------------------------\n\n* If r > l\n* 1. Find the middle point to divide the array into two halves:  \n        middle m = (l+r)/2\n* 2. Call mergeSort for first half:   \n        Call mergeSort(arr, l, m)\n* 3. Call mergeSort for second half:\n        Call mergeSort(arr, m+1, r)\n* 4. Merge the two halves sorted in step 2 and 3:\n        Call merge(arr, l, m, r)\n\n\nThe following diagram from **[Wikipedia](https://en.wikipedia.org/wiki/File:Merge_sort_algorithm_diagram.svg)** shows the complete merge sort process for an example array {38, 27, 43, 3, 9, 82, 10}. If we take a closer look at the diagram, we can see that the array is recursively divided in two halves till the size becomes 1. Once the size becomes 1, the merge processes comes into action and starts merging arrays back till the complete array is merged\n\n![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/merge-sort/resources/merge-sort.png)\n\n> * Input: {12, 11, 13, 5, 6, 7}\n> * Output: Given array is `12 11 13 5 6 7`\n> * Output: Sorted array is `5 6 7 11 12 13`\n\n**Algorithm Complexity**\n\n| Complexity\t\t| Notation     |\n| ----------------- |:------------:|\n| `Time Complexity`\t| `O(n log n)` |"
  },
  {
    "path": "merge-sort/merge-sort.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\nfunc Merge(arr []int, l, m, r int) {\n\tvar i, j, k int\n\tvar n1 int = m - l + 1\n\tvar n2 int = r - m\n\n\t//Create temp arrays\n\tL := make([]int, n1)\n\tR := make([]int, n2)\n\n\t//Copy data to temp arrays L[] and R[]\n\tfor i = 0; i < n1; i++ {\n\t\tL[i] = arr[l+i]\n\t}\n\n\tfor j = 0; j < n2; j++ {\n\t\tR[j] = arr[m+1+j]\n\t}\n\n\t//Merge the temp arrays back into arr[l..r]\n\ti = 0 //Initial index of first subarray\n\tj = 0 //Initial index of second subarray\n\tk = l //Initial index of merged subarray\n\n\tfor i < n1 && j < n2 {\n\t\tif L[i] <= R[j] {\n\t\t\tarr[k] = L[i]\n\t\t\ti++\n\t\t} else {\n\t\t\tarr[k] = R[j]\n\t\t\tj++\n\t\t}\n\t\tk++\n\t}\n\n\t//Copy the remaining elements of L[], if there are any\n\tfor i < n1 {\n\t\tarr[k] = L[i]\n\t\ti++\n\t\tk++\n\t}\n\n\t//Copy the remaining elements of R[], if there are any\n\tfor j < n2 {\n\t\tarr[k] = R[j]\n\t\tj++\n\t\tk++\n\t}\n}\n\nfunc MergeSort(arr []int, l, r int) {\n\tif l < r {\n\t\t//Same as (l + r) / 2 but avoids overflow for large l and h\n\t\tvar m int = l + (r-l)/2\n\n\t\t//Sort first and second halves\n\t\tMergeSort(arr, l, m)\n\t\tMergeSort(arr, m+1, r)\n\n\t\tMerge(arr, l, m, r)\n\t}\n}\n\nfunc PrintArray(A []int, size int) {\n\tfor i := 0; i < size; i++ {\n\t\tfmt.Printf(\"%d \", A[i])\n\t}\n\tfmt.Printf(\"\\n\")\n}\n\nfunc main() {\n\tarr := []int{12, 11, 13, 5, 6, 7}\n\tvar arr_size int = len(arr)\n\n\tfmt.Println(\"Given array size is: \")\n\tPrintArray(arr, arr_size)\n\n\tMergeSort(arr, 0, arr_size-1)\n\n\tfmt.Println(\"Sorted array is: \")\n\tPrintArray(arr, arr_size)\n}\n"
  },
  {
    "path": "merge-sort/merge-sort_test.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestMergeSort(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tArrayIn  []int\n\t\tArrayOut []int\n\t}{\n\t\t{[]int{1, 3, 2, 4}, []int{1, 2, 3, 4}},\n\t\t{[]int{3, 2, 1, 4}, []int{1, 2, 3, 4}},\n\t\t{[]int{9, 8, 6, 5, 7, 4, 3, 0, 2, 1}, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}},\n\t\t{[]int{-3, -2, -1, -4, 0}, []int{-4, -3, -2, -1, 0}},\n\t}\n\tfor _, data := range testDatas {\n\t\texpected := data.ArrayOut\n\t\tMergeSort(data.ArrayIn, 0, len(data.ArrayIn)-1)\n\t\tactual := data.ArrayIn\n\n\t\tif !reflect.DeepEqual(expected, actual) {\n\t\t\tt.Errorf(\"MergeSort: Expected: %d, Actual: %d\", expected, actual)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "quick-sort/README.md",
    "content": "<h1 align=\"center\">Quick Sort Source</h1>\n\n[What It Is](#what-it-is)\n\n## What It Is\n\nLike **[MergeSort](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/merge-sort)**, `QuickSort` is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.\n\n* Always pick first element as pivot.\n* Always pick last element as pivot (implemented below)\n* Pick a random element as pivot.\n* Pick median as pivot.\n\nThe key process in quickSort is `partition()`. Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.\n\n![Preview Thumbnail](https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif)\n\nQuickSort(arr[], start,  end)\n--------------------------\n\n**Partition Algorithm**\n\nThere can be many ways to do partition, following pseudo code adopts the method given in CLRS book. The logic is simple, we start from the leftmost element and keep track of index of smaller (or equal to) elements as i. While traversing, if we find a smaller element, we swap current element with arr[i]. Otherwise we ignore current element.\n\n![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/quick-sort/resources/quick-sort.png)\n\n> * Input: {10, 7, 8, 9, 1, 5}\n> * Output: Sorted array is `1 5 7 8 9 10`\n\n**Algorithm Complexity**\n\n| Complexity\t\t| Notation     |\n| ----------------- |:------------:|\n| `Time Complexity`\t| `O(n log n)` |\n| `Auxiliary Space` | `O(n)`       |"
  },
  {
    "path": "quick-sort/quick-sort.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\nfunc Swap(a *int, b *int) {\n\tt := *a\n\t*a = *b\n\t*b = t\n}\n\nfunc Partition(arr []int, start, end int) int {\n\n\tpivot := arr[end]\n\n\t//Index of smaller element\n\tvar i int = (start - 1)\n\n\tfor j := start; j <= end-1; j++ {\n\t\t//If current element is smaller than or equal to pivot\n\t\tif arr[j] <= pivot {\n\t\t\ti++\n\t\t\tSwap(&arr[i], &arr[j])\n\t\t}\n\t}\n\n\tSwap(&arr[i+1], &arr[end])\n\n\treturn (i + 1)\n}\n\n/*The main function that implements QuickSort\narr[] -> Array to be sorted\nstart -> Starting index\nend   -> Ending index\n*/\nfunc QuickSort(arr []int, start, end int) {\n\tif start < end {\n\t\t//pi is partitioning index, arr[p] is now at right place\n\t\tvar pi int = Partition(arr, start, end)\n\n\t\t//Separately sort elements before partition and after partition\n\t\tQuickSort(arr, start, pi-1)\n\t\tQuickSort(arr, pi+1, end)\n\t}\n}\n\nfunc PrintArray(arr []int, size int) {\n\tfor i := 0; i < size; i++ {\n\t\tfmt.Printf(\"%d \", arr[i])\n\t}\n\tfmt.Printf(\"\\n\")\n}\n\nfunc main() {\n\tarr := []int{10, 7, 8, 9, 1, 5}\n\tvar n int = len(arr)\n\n\tQuickSort(arr, 0, n-1)\n\n\tfmt.Println(\"Sorted array is: \")\n\tPrintArray(arr, n)\n}\n"
  },
  {
    "path": "quick-sort/quick-sort_test.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestQuickSort(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tArrayIn  []int\n\t\tArrayOut []int\n\t}{\n\t\t{[]int{1, 3, 2, 4}, []int{1, 2, 3, 4}},\n\t\t{[]int{3, 2, 1, 4}, []int{1, 2, 3, 4}},\n\t\t{[]int{9, 8, 6, 5, 7, 4, 3, 0, 2, 1}, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}},\n\t\t{[]int{-3, -2, -1, -4, 0}, []int{-4, -3, -2, -1, 0}},\n\t}\n\tfor _, data := range testDatas {\n\t\texpected := data.ArrayOut\n\t\tQuickSort(data.ArrayIn, 0, len(data.ArrayIn)-1)\n\t\tactual := data.ArrayIn\n\n\t\tif !reflect.DeepEqual(expected, actual) {\n\t\t\tt.Errorf(\"QuickSort: Expected: %d, Actual: %d\", expected, actual)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "stack/stack.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport \"fmt\"\n\nconst MaxUint = ^uint(0)\nconst MinUint = 0\nconst MaxInt = int(MaxUint >> 1)\nconst MinInt = -MaxInt - 1\n\ntype Stack struct {\n\ttop      int\n\tcapacity uint\n\tarray    []int\n}\n\n//Returns an initialized list\nfunc (s *Stack) Init(capacity uint) *Stack {\n\ts.top = -1\n\ts.capacity = capacity\n\ts.array = make([]int, capacity)\n\treturn s\n}\n\n//Returns an new list\nfunc New(capacity uint) *Stack {\n\treturn new(Stack).Init(capacity)\n}\n\n// Stack is full when top is equal to the last index\nfunc IsFull(stack *Stack) bool {\n\treturn stack.top == int(stack.capacity)-1\n}\n\n// Stack is empty when top is equal to -1\nfunc IsEmpty(stack *Stack) bool {\n\treturn stack.top == -1\n}\n\nfunc Push(stack *Stack, item int) {\n\tif IsFull(stack) {\n\t\treturn\n\t}\n\tstack.top++\n\tstack.array[stack.top] = item\n}\n\nfunc Pop(stack *Stack) int {\n\tif IsEmpty(stack) {\n\t\treturn MinInt\n\t}\n\ttemp := stack.array[stack.top]\n\tstack.top--\n\treturn temp\n}\n\nfunc main() {\n\tstack := New(100)\n\n\tPush(stack, 10)\n\tfmt.Println(\"Pushed to stack : 10\")\n\tPush(stack, 20)\n\tfmt.Println(\"Pushed to stack : 20\")\n\tPush(stack, 30)\n\tfmt.Println(\"Pushed to stack : 30\")\n\n\tfmt.Printf(\"Popped from stack : %d\", Pop(stack))\n}\n"
  },
  {
    "path": "stack/stack_test.go",
    "content": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n// and you are welcome to redistribute it under certain conditions; See\n// file LICENSE, which is part of this source code package, for details.\n// ====================================================\n\npackage main\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestStack(t *testing.T) {\n\tvar testDatas = []struct {\n\t\tStack    *Stack\n\t\tArrayIn  []int\n\t\tArrayOut []int\n\t}{\n\t\t{New(3), []int{10, 20, 30}, []int{30, 20, 10}},\n\t\t{New(5), []int{10, 20, 30, 40, 50}, []int{50, 40, 30, 20, 10}},\n\t\t{New(1), []int{7}, []int{7}},\n\t\t{New(5), []int{15, 10, 22, 33, 77}, []int{77, 33, 22, 10, 15}},\n\t}\n\n\tfor _, data := range testDatas {\n\t\tfor i := uint(0); i < data.Stack.capacity; i++ {\n\t\t\tPush(data.Stack, data.ArrayIn[i])\n\t\t}\n\t\tfor i := uint(0); i < data.Stack.capacity; i++ {\n\t\t\texpected := data.ArrayOut[i]\n\t\t\tactual := Pop(data.Stack)\n\n\t\t\tif !reflect.DeepEqual(expected, actual) {\n\t\t\t\tt.Errorf(\"Stack: Expected: %d, Actual: %d\", expected, actual)\n\t\t\t}\n\t\t}\n\t}\n}\n"
  }
]