Repository: Dentrax/Data-Structures-with-Go
Branch: master
Commit: 17ec0461bcf3
Files: 52
Total size: 83.1 KB
Directory structure:
gitextract_9396xqnw/
├── .gitignore
├── .travis.yml
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── appveyor.yml
├── array-minimum-distance/
│ ├── README.md
│ ├── array-minimum-distance.go
│ └── array-minimum-distance_test.go
├── array-rotation/
│ ├── README.md
│ ├── array-rotation.go
│ └── array-rotation_test.go
├── array-smallest-missing-number/
│ ├── README.md
│ ├── array-smallest-missing-number.go
│ └── array-smallest-missing-number_test.go
├── binary-search-tree-1-insertion/
│ ├── binary-search-tree-1-insertion.go
│ └── binary-search-tree-1-insertion_test.go
├── binary-tree-1-introduction/
│ ├── binary-tree-1-introduction.go
│ └── binary-tree-1-introduction_test.go
├── binary-tree-2-traversals-in-pre-post-order/
│ └── binary-tree-traversals-2-in-pre-post-order.go
├── binary-tree-2-traversals-level-order/
│ └── binary-tree-traversals-2-level-order.go
├── binary-tree-3-doubly-linked-list/
│ └── binary-tree-3-doubly-linked-list.go
├── binary-tree-4-delete/
│ └── binary-tree-4-delete.go
├── binary-tree-5-find-min-max/
│ └── binary-tree-5-find-min-max.go
├── coverage.txt
├── linked-list-1-introduction/
│ ├── README.md
│ └── linked-list-1-introduction.go
├── linked-list-2-inserting-a-node/
│ ├── README.md
│ └── linked-list-2-inserting-a-node.go
├── linked-list-3-deleting-a-node/
│ ├── README.md
│ └── linked-list-3-deleting-a-node.go
├── linked-list-circular-1-introduction/
│ └── README.md
├── linked-list-circular-2-traversal/
│ ├── README.md
│ └── linked-list-circular-2-traversal.go
├── linked-list-circular-singly-1-insertion/
│ ├── README.md
│ └── linked-list-circular-singly-1-insertion.go
├── linked-list-find-length/
│ ├── README.md
│ └── linked-list-find-length.go
├── linked-list-merge-two-sorted/
│ ├── README.md
│ ├── linked-list-merge-two-sorted.go
│ └── linked-list-merge-two-sorted.go_test.go
├── linked-list-reverse/
│ ├── README.md
│ ├── linked-list-reverse.go
│ └── linked-list-reverse_test.go
├── merge-sort/
│ ├── README.md
│ ├── merge-sort.go
│ └── merge-sort_test.go
├── quick-sort/
│ ├── README.md
│ ├── quick-sort.go
│ └── quick-sort_test.go
└── stack/
├── stack.go
└── stack_test.go
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
================================================
FILE: .travis.yml
================================================
language: go
go:
- 1.10.x
- 1.11.x
script:
- go build -v ./...
- go test -v -cover -race ./...
after_success:
- bash <(curl -s https://codecov.io/bash)
================================================
FILE: CONTRIBUTING.md
================================================
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2017 Furkan Türkal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
<h1 align="center">Data Structures with Go</h1>
[](https://opensource.org/licenses/mit-license.php)
[](https://github.com/ellerbrock/open-source-badges/)
[](https://ci.appveyor.com/project/Dentrax/data-structures-with-go)
[](https://travis-ci.org/Dentrax/Data-Structures-with-Go)
[](https://codecov.io/gh/Dentrax/Data-Structures-with-Go)
[](https://goreportcard.com/report/github.com/Dentrax/Data-Structures-with-Go)
[](https://sourcegraph.com/github.com/Dentrax/Data-Structures-with-Go)
Click here for **[GO Language](https://golang.org/)**
Click here for **[Guide & Theory](https://goo.gl/Ej9kzs)**
Click here for **[VSCode IDE](https://code.visualstudio.com/)**
[What It Is](#what-it-is)
[How To Use](#how-to-use)
[About](#about)
[Collaborators](#collaborators)
[Branches](#branches)
[Copyright & Licensing](#copyright--licensing)
[Contributing](#contributing)
[Contact](#contact)
## What It Is
**Data Structures with Go**
Data-Structures-with-Go guide for GO language is an easy and advanced way to learn Data Structures.
**Uses : `GO Language`** -> **[Official GO Language Web Site](https://golang.org/)**
## How To Use
Just research the **[main repository](https://github.com/Dentrax/Data-Structures-with-Go)** and learn it gradually. Thats all.
## About
Data-Structures-with-Go was created to serve three purposes:
**Data-Structures-with-Go is a basically Data-Structures learning repository which all structures coded in Go language**
1. To act as a guide to learn basic Data Structures with enhanced and rich content.
2. To provide a simplest and easiest way to learn.
3. There is no any Data Structures guide coded in Go language on the internet.
## Collaborators
**Project Manager** - Furkan Türkal (GitHub: **[dentrax](https://github.com/dentrax)**)
## Branches
We publish source for the **[Data-Structures-with-Go]** in single rolling branch:
The **[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.
## Copyright & Licensing
The base project code is copyrighted by Furkan 'Dentrax' Türkal and is covered by single licence.
All 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.
**References**
While this repository is being prepared, it may have been quoted from some sources.
If there is an unspecified source, please contact me.
## Contributing
Please check the [CONTRIBUTING.md](CONTRIBUTING.md) file for contribution instructions and naming guidelines.
## Contact
Data-Structures-with-Go was created by Furkan 'Dentrax' Türkal
* <https://www.furkanturkal.com>
You can contact by URL:
**[CONTACT](https://github.com/dentrax)**
<kbd>Best Regards</kbd>
================================================
FILE: appveyor.yml
================================================
version: "{build}"
skip_tags: true
clone_depth: 1
environment:
GOVERSION: 1.11
build: false
deploy: false
install:
- go build -v ./...
================================================
FILE: array-minimum-distance/README.md
================================================
<h1 align="center">Array Minimum Distance Source</h1>
[What It Is](#what-it-is)
## What It Is
* 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[]`.
Examples
--------------------------
> * Input: arr[] = {1, 2}, x = 1, y = 2
> * Output: Minimum distance between 1 and 2 is 1.
> * Input: arr[] = {3, 4, 5}, x = 3, y = 5
> * Output: Minimum distance between 3 and 5 is 2.
> * Input: arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, x = 3, y = 6
> * Output: Minimum distance between 3 and 6 is 4.
> * Input: arr[] = {2, 5, 3, 5, 4, 4, 2, 3}, x = 3, y = 2
> * Output: Minimum distance between 3 and 2 is 1.
METHOD 1 (Simple)
--------------------------
Use 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.
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(n^2)` |
================================================
FILE: array-minimum-distance/array-minimum-distance.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
func Abs(x int) int {
if x < 0 {
return -x
}
if x == 0 {
return 0
}
return x
}
func minDist (arr []int, n, x, y int) int {
var min_dist int = MaxInt
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if((x == arr[i] && y == arr[j] || y == arr[i] && x == arr[j]) && min_dist > Abs(i - j)) {
min_dist = Abs(i - j)
}
}
}
return min_dist
}
func main() {
arr := []int{3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}
var n int = len(arr)
var x int = 3
var y int = 6
fmt.Printf("Minimum distance between %d and %d is %d", x, y, minDist(arr, n, x, y))
}
================================================
FILE: array-minimum-distance/array-minimum-distance_test.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import (
"reflect"
"testing"
)
func TestAbs(t *testing.T) {
var testDatas = []struct {
Input int
Output int
}{
{1, 1},
{7, 7},
{-1, 1},
{-7, 7},
{0, 0},
}
for _, data := range testDatas {
expected := data.Output
actual := Abs(data.Input)
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Abs: Expected: %d, Actual: %d", expected, actual)
}
}
}
func TestMinDist(t *testing.T) {
var testDatas = []struct {
Array []int
Num1 int
Num2 int
Distance int
}{
{[]int{3, 4, 5, 6}, 3, 6, 3},
{[]int{1, 2, 3}, 1, 3, 2},
{[]int{9, 7, 5, 3, 1}, 1, 7, 3},
{[]int{-1, -7, 5, 5, 1, 5}, -1, 5, 2},
{[]int{-1, -7, 5, 5, -7, -1}, -1, -7, 1},
}
for _, data := range testDatas {
expected := data.Distance
actual := minDist(data.Array, len(data.Array), data.Num1, data.Num2)
if !reflect.DeepEqual(expected, actual) {
t.Errorf("MinDist: Expected: %d, Actual: %d", expected, actual)
}
}
}
================================================
FILE: array-rotation/README.md
================================================
<h1 align="center">Array Rotation Source</h1>
[What It Is](#what-it-is)
## What It Is
> * Input: Write a function `rotate(ar[], d, n)` that rotates `arr[]` of size `n` by `d` elements
> * Output: `1 2 3 4 5 6 7`
> * Input: Rotation of the above array by `2` will make array
> * Output: `3 4 5 6 7 1 2`
METHOD 1 (Use temp array)
--------------------------
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
* 1) Store `d` elements in a temp array
temp[] = [1, 2]
* 2) Shift rest of the `arr[]`
arr[] = [3, 4, 5, 6, 7, 6, 7]
* 3) Store back the `d` elements
arr[] = [3, 4, 5, 6, 7, 1, 2]
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(n)` |
| `Auxiliary Space` | `O(d)` |
METHOD 2 (Rotate one by one)
--------------------------
```go
leftRotate(arr[], d, n)
start
For i = 0 to i < d
Left rotate all elements of arr[] by one
end
```
To 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]
Let us take the same example arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2
Rotate arr[] by one 2 times
We get `[2, 3, 4, 5, 6, 7, 1]` after first rotation and `[3, 4, 5, 6, 7, 1, 2]` after second rotation.
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(n*d)` |
| `Auxiliary Space` | `O(1)` |
================================================
FILE: array-rotation/array-rotation.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
func leftRotate(arr []int, d int, n int){
for i := 0; i < d; i++ {
leftRotateByOne(arr, n)
}
}
func leftRotateByOne(arr []int, n int){
var i, temp int
temp = arr[0]
for i = 0; i < n - 1; i++ {
arr[i] = arr[i + 1]
}
arr[i] = temp
}
func printArray(arr []int, size int){
if(len(arr) < size){
fmt.Println("[ArrayRotation::printArray()] -> Index out of range. Max : ", len(arr))
return;
}
for i := 0; i < size; i++ {
fmt.Print(arr[i])
}
}
func main() {
arr := []int{1, 2, 3, 4, 5, 6, 7}
fmt.Println("-INPUT-")
printArray(arr, len(arr) - 1)
leftRotate(arr, 2 , 7)
fmt.Println()
fmt.Println("-OUTPUT-")
printArray(arr, 7)
}
================================================
FILE: array-rotation/array-rotation_test.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import (
"reflect"
"testing"
)
func TestLeftRotate(t *testing.T) {
var testDatas = []struct {
ArrayIn []int
Count int
Depth int
ArrayOut []int
}{
{[]int{1, 2, 3, 4, 5, 6, 7}, 2, 7, []int{3, 4, 5, 6, 7, 1, 2}},
{[]int{1, 2, 3, 4, 5, 6, 7}, 2, 6, []int{3, 4, 5, 6, 1, 2, 7}},
{[]int{1, 2, 3, 4, 5, 6, 7}, 1, 2, []int{2, 1, 3, 4, 5, 6, 7}},
{[]int{1, 2, 3, 4, 5, 6, 7}, 7, 7, []int{1, 2, 3, 4, 5, 6, 7}},
{[]int{1, 2, 3, 4, 5, 6, 7}, 7, 6, []int{2, 3, 4, 5, 6, 1, 7}},
}
for _, data := range testDatas {
expected := data.ArrayOut
leftRotate(data.ArrayIn, data.Count, data.Depth)
actual := data.ArrayIn
if !reflect.DeepEqual(expected, actual) {
t.Errorf("LeftRotate: Expected: %d, Actual: %d", expected, actual)
}
}
}
================================================
FILE: array-smallest-missing-number/README.md
================================================
<h1 align="center">Array Smallest Missing Number Source</h1>
[What It Is](#what-it-is)
## What It Is
Given 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.
Examples
--------------------------
> * Input: {0, 1, 2, 6, 9}, n = 5, m = 10
> * Output: `3`
> * Input: {4, 5, 10, 11}, n = 4, m = 12
> * Output: `0`
> * Input: {0, 1, 2, 3}, n = 4, m = 5
> * Output: `4`
> * Input: {0, 1, 2, 3, 4, 5, 6, 7, 10}, n = 9, m = 11
> * Output: `8`
METHOD 1 (Use Binary Search)
--------------------------
`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`.
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:------------:|
| `Time Complexity` | `O(m log n)` |
METHOD 2 (Linear Search)
--------------------------
If `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.
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:------------:|
| `Time Complexity` | `O(n)` |
METHOD 3 (Use Modified Binary Search)
--------------------------
In 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.
In 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.
1) If the first element is not same as its index then return first index
2) Else get the middle index say mid
a) If arr[mid] greater than mid then the required element lies in left half.
b) Else the required element lies in right half.
`Note: This method doesn’t work if there are duplicate elements in the array.`
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:------------:|
| `Time Complexity` | `O(log n)` |
================================================
FILE: array-smallest-missing-number/array-smallest-missing-number.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
import "os"
func findFirstMissing(arr []int, start, end int) int {
if start < 0 {
fmt.Println("Start must be greater than 0")
os.Exit(1)
return 0
}
if start > end {
return end + 1
}
if start != arr[start] {
return start
}
mid := (start + end) / 2
if arr[mid] == mid {
return findFirstMissing(arr, mid+1, end)
}
return findFirstMissing(arr, start, end)
}
func main() {
arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 10}
var n int = len(arr)
fmt.Printf("Smallest missing element is %d", findFirstMissing(arr, 0, n-1))
}
================================================
FILE: array-smallest-missing-number/array-smallest-missing-number_test.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import (
"reflect"
"testing"
)
func TestFindFirstMissing(t *testing.T) {
var testDatas = []struct {
ArrayIn []int
Start int
End int
Out int
}{
{[]int{0, 1, 2, 4, 5}, 0, 4, 3},
{[]int{0, 1, 2, 3, 4, 6, 8, 9, 10}, 0, 5, 5},
{[]int{0, 1, 2, 3, 4, 6, 8, 9, 10}, 0, 8, 5},
{[]int{0, 1, 2, 3, 4, 5, 6, 8, 9, 10}, 0, 8, 7},
{[]int{1, 2, 3, 4, 5}, 0, 5, 0},
}
for _, data := range testDatas {
expected := data.Out
actual := findFirstMissing(data.ArrayIn, data.Start, data.End)
if !reflect.DeepEqual(expected, actual) {
t.Errorf("FindFirstMissing: Expected: %d, Actual: %d", expected, actual)
}
}
}
================================================
FILE: binary-search-tree-1-insertion/binary-search-tree-1-insertion.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
left *Node
right *Node
}
//Returns an initialized list
func (n *Node) Init(data int) *Node {
n.data = data
n.left = nil
n.right = nil
return n
}
//Returns an new list
func New(data int) *Node {
return new(Node).Init(data)
}
//Function to search a given key in a given BST
func Search(root *Node, key int) *Node {
//1. Base Cases: root is null or key is present at root
if root == nil || root.data == key {
//fmt.Println("The given previous node cannot be NULL")
return root
}
//2. Key is greater than root's key
if root.data < key {
return Search(root.right, key)
}
//3. Key is smaller than root's key
return Search(root.left, key)
}
//A utility function to do inorder traversal of BST
func PrintInOrder(root *Node) {
if root != nil {
PrintInOrder(root.left)
fmt.Printf("%d \n", root.data)
PrintInOrder(root.right)
}
}
//A utility function to insert a new node with given key in BST
func Insert(node *Node, key int) *Node {
//1. If the tree is empty, return a new node
if node == nil {
return New(key)
}
//2. Otherwise, recur down the tree
if key < node.data {
node.left = Insert(node.left, key)
} else if key > node.data {
node.right = Insert(node.right, key)
}
//3. Return the (unchanged) node pointer
return node
}
func main() {
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
root := New(50)
Insert(root, 30)
Insert(root, 20)
Insert(root, 40)
Insert(root, 70)
Insert(root, 60)
Insert(root, 80)
//Print inoder traversal of the BST
PrintInOrder(root)
}
================================================
FILE: binary-search-tree-1-insertion/binary-search-tree-1-insertion_test.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import (
"reflect"
"testing"
)
func TestBinarySearchTreeNew(t *testing.T) {
var testDatas = []struct {
Node *Node
ArrayIn []int
Test int
Out int
}{
{New(0), []int{20, 4, 15, 85}, 15, 15},
{New(3), []int{10, 20, 30, 40, 50}, 50, 50},
{New(7), []int{7}, 7, 7},
}
for _, data := range testDatas {
for i := 0; i < len(data.ArrayIn); i++ {
Insert(data.Node, data.ArrayIn[i])
}
actual := Search(data.Node, data.Test)
expected := data.Out
if !reflect.DeepEqual(expected, actual.data) {
t.Errorf("BinarySearchTree: Expected: %d, Actual: %d", expected, actual)
}
}
}
================================================
FILE: binary-tree-1-introduction/binary-tree-1-introduction.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
left *Node
right *Node
}
//Returns an initialized list
func (n *Node) Init(data int) *Node {
n.data = data
n.left = nil
n.right = nil
return n
}
//Returns an new list
func New(data int) *Node {
return new(Node).Init(data)
}
func main() {
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
root := New(1)
/*
1
/ \
NULL NULL
*/
root.left = New(2)
root.right = New(3)
/* 2 and 3 become left and right children of 1
1
/ \
2 3
/ \ / \
NULL NULL NULL NULL
*/
root.left.left = New(4)
/* 4 becomes left child of 2
1
/ \
2 3
/ \ / \
4 NULL NULL NULL
/ \
NULL NULL
*/
fmt.Println("Root data : ", root.data)
fmt.Println("Root->Left data : ", root.left.data)
fmt.Println("Root->Right data : ", root.right.data)
fmt.Println("Root->Left->Left data : ", root.left.left.data)
}
================================================
FILE: binary-tree-1-introduction/binary-tree-1-introduction_test.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import (
"reflect"
"testing"
)
func TestBinaryTreeNew(t *testing.T) {
var testDatas = []struct {
Node *Node
Data int
Left *Node
Right *Node
}{
{New(-1), -1, New(-1), New(-1)},
{New(0), 0, New(0), New(0)},
{New(1), 1, New(1), New(1)},
{New(7), 7, New(7), New(7)},
}
for _, data := range testDatas {
data.Node.right = data.Right
data.Node.left = data.Left
expected := data.Data
actualLeft := data.Node.left.data
actualRight := data.Node.right.data
if !reflect.DeepEqual(expected, actualLeft) || !reflect.DeepEqual(expected, actualRight) {
t.Errorf("BinaryTreeNew: Expected: %d, ActualLeft: %d, ActualRight: %d", expected, actualLeft, actualRight)
}
}
}
================================================
FILE: binary-tree-2-traversals-in-pre-post-order/binary-tree-traversals-2-in-pre-post-order.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
left *Node
right *Node
}
//Returns an initialized list
func (n *Node) Init(data int) *Node {
n.data = data
n.left = nil
n.right = nil
return n
}
//Returns an new list
func New(data int) *Node {
return new(Node).Init(data)
}
func PrintPostOrder(node *Node) {
//1. Check if the given node is NULL
if node == nil {
return
}
//2. First recur on left subtree
PrintPostOrder(node.left)
//3. Then recur on right subtree
PrintPostOrder(node.right)
//4. Now deal with the node
fmt.Printf("%d ", node.data)
}
func PrintInOrder(node *Node) {
//1. Check if the given node is NULL
if node == nil {
return
}
//2. First recur on left child
PrintInOrder(node.left)
//3. Then print the data of node
fmt.Printf("%d ", node.data)
//4. Now recur on right child
PrintInOrder(node.right)
}
func PrintPreOrder(node *Node) {
//1. Check if the given node is NULL
if node == nil {
return
}
//2. First print data of node
fmt.Printf("%d ", node.data)
//3. Then recur on left sutree
PrintPreOrder(node.left)
//4. Now recur on right subtree
PrintPreOrder(node.right)
}
func main() {
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
root := New(1)
root.left = New(2)
root.right = New(3)
root.left.left = New(4)
root.left.right = New(5)
fmt.Println("\nPreorder traversal of binary tree is :")
PrintPreOrder(root)
fmt.Println("\nInorder traversal of binary tree is :")
PrintInOrder(root)
fmt.Println("\nPostorder traversal of binary tree is :")
PrintPostOrder(root)
}
================================================
FILE: binary-tree-2-traversals-level-order/binary-tree-traversals-2-level-order.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
left *Node
right *Node
}
//Returns an initialized list
func (n *Node) Init(data int) *Node {
n.data = data
n.left = nil
n.right = nil
return n
}
//Returns an new list
func New(data int) *Node {
return new(Node).Init(data)
}
/* Compute the "height" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
func GetHeight(node *Node) int {
//1. Check if the given node is NULL
if node == nil {
return 0
}
//2. Compute the height of each subtree
lheight := GetHeight(node.left)
rheight := GetHeight(node.right)
//3. Use the larger one
if lheight > rheight {
return lheight + 1
}
return rheight + 1
}
/* Print nodes at a given level */
func PrintGivenLevel(root *Node, level int) {
//1. Check if the given root is NULL
if root == nil {
return
}
if level == 1 {
fmt.Printf("%d ", root.data)
} else if level > 1 {
PrintGivenLevel(root.left, level-1)
PrintGivenLevel(root.right, level-1)
}
}
/* Function to print level order traversal a tree*/
func PrintLevelOrder(root *Node) {
//1. Check if the given root is NULL
if root == nil {
fmt.Println("The given root node cannot be NULL")
return
}
h := GetHeight(root)
for i := 1; i <= h; i++ {
PrintGivenLevel(root, i)
}
}
func main() {
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
root := New(1)
root.left = New(2)
root.right = New(3)
root.left.left = New(4)
root.left.right = New(5)
fmt.Println("\nLevel Order traversal of binary tree is :")
PrintLevelOrder(root)
}
================================================
FILE: binary-tree-3-doubly-linked-list/binary-tree-3-doubly-linked-list.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
left *Node
right *Node
}
//Returns an initialized list
func (n *Node) Init(data int) *Node {
n.data = data
n.left = nil
n.right = nil
return n
}
//Returns an new list
func New(data int) *Node {
return new(Node).Init(data)
}
// Standard Inorder traversal of tree
func InOrder(root *Node) {
//1. Check if the given node is NULL
if root == nil {
return
}
InOrder(root.left)
fmt.Printf("%d ", root.data)
InOrder(root.right)
}
// Changes left pointers to work as previous pointers in converted DLL
// The function simply does inorder traversal of Binary Tree and updates
// left pointer using previously visited node
var pre *Node = nil
func FixPrevPtr(root *Node) {
//1. Check if the given root is NULL
if root == nil {
return
}
FixPrevPtr(root.left)
root.left = pre
pre = root
FixPrevPtr(root.right)
}
// Changes right pointers to work as next pointers in converted DLL
var prev *Node = nil
func FixNextPtr(root *Node) *Node {
// Find the right most node in BT or last node in DLL
for root != nil && root.right != nil {
root = root.right
}
// Start from the rightmost node, traverse back using left pointers.
// While traversing, change right pointer of nodes.
for root != nil && root.left != nil {
prev = root
root = root.left
root.right = prev
}
// The leftmost node is head of linked list, return it
return root
}
func BTToDLL(root *Node) *Node {
// Set the previous pointer
FixPrevPtr(root)
// Set the next pointer and return head of DLL
return FixNextPtr(root)
}
func PrintList(root *Node) {
for root != nil {
fmt.Printf("%d ", root.data)
root = root.right
}
}
func main() {
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
root := New(10)
root.left = New(12)
root.right = New(15)
root.left.left = New(25)
root.left.right = New(30)
root.right.left = New(36)
fmt.Println("\nInorder Tree Traversal")
InOrder(root)
head := BTToDLL(root)
fmt.Println("\nDLL Traversal")
PrintList(head)
}
================================================
FILE: binary-tree-4-delete/binary-tree-4-delete.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
left *Node
right *Node
}
//Returns an initialized list
func (n *Node) Init(data int) *Node {
n.data = data
n.left = nil
n.right = nil
return n
}
//Returns an new list
func New(data int) *Node {
return new(Node).Init(data)
}
/* Compute the "height" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
func DeleteTree(node *Node) {
//1. Check if the given node is NULL
if node == nil {
return
}
//2. First delete both subtrees
DeleteTree(node.left)
DeleteTree(node.right)
//3. Then delete the node
fmt.Printf("\nDeleting node: %d", node.data)
//free(node);
node = nil
}
func main() {
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
root := New(1)
root.left = New(2)
root.right = New(3)
root.left.left = New(4)
root.left.right = New(5)
DeleteTree(root)
root = nil
fmt.Println("\nTree deleted")
}
================================================
FILE: binary-tree-5-find-min-max/binary-tree-5-find-min-max.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
type Node struct {
data int
left *Node
right *Node
}
//Returns an initialized list
func (n *Node) Init(data int) *Node {
n.data = data
n.left = nil
n.right = nil
return n
}
//Returns an new list
func New(data int) *Node {
return new(Node).Init(data)
}
// Returns minimum value in a given Binary Tree
func FindMin(root *Node) int {
//1. Check if the given node is NULL
if root == nil {
return MaxInt
}
//2. Return maximum of 3 values: 1) Root's data 2) Max in Left Subtree 3) Max in right subtree
res := root.data
lres := FindMin(root.left)
rres := FindMin(root.right)
if lres < res {
res = lres
}
if rres < res {
res = rres
}
return res
}
// Returns maximum value in a given Binary Tree
func FindMax(root *Node) int {
//1. Check if the given node is NULL
if root == nil {
return MinInt
}
//2. Return maximum of 3 values: 1) Root's data 2) Max in Left Subtree 3) Max in right subtree
res := root.data
lres := FindMax(root.left)
rres := FindMax(root.right)
if lres > res {
res = lres
}
if rres > res {
res = rres
}
return res
}
func main() {
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
root := New(2)
root.left = New(7)
root.right = New(5)
root.left.right = New(6)
root.right.right = New(9)
root.left.right.left = New(1)
root.left.right.right = New(11)
root.right.right.left = New(1)
fmt.Printf("\nMaximum element is %d", FindMax(root))
fmt.Printf("\nMinimum element is %d", FindMin(root))
}
================================================
FILE: coverage.txt
================================================
mode: atomic
Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:13.54,14.15 1 11
Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:20.2,20.17 1 11
Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:24.2,24.25 1 11
Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:28.2,30.21 2 6
Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:34.2,34.42 1 0
Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:14.15,18.3 3 0
Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:20.17,22.3 1 0
Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:24.25,26.3 1 5
Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:30.21,32.3 1 6
Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:37.13,41.2 3 0
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:17.21,18.11 1 20
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:21.2,21.12 1 3
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:24.2,24.10 1 2
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:18.11,20.3 1 17
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:21.12,23.3 1 1
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:27.43,30.25 2 5
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:38.2,38.17 1 5
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:30.25,31.30 1 24
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:31.30,32.92 1 49
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:32.92,34.5 1 5
Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:41.13,47.2 5 0
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:19.37,24.2 4 12
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:27.26,29.2 1 12
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:32.40,34.37 1 11
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:40.2,40.21 1 8
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:45.2,45.31 1 1
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:34.37,37.3 1 3
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:40.21,42.3 1 7
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:49.31,50.17 1 0
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:50.17,54.3 3 0
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:58.40,60.17 1 33
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:65.2,65.21 1 24
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:72.2,72.13 1 24
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:60.17,62.3 1 9
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:65.21,67.3 1 2
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:67.8,67.28 1 22
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:67.28,69.3 1 21
Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:75.13,94.2 8 0
Data-Structures-with-Go/binary-tree-1-introduction/binary-tree-1-introduction.go:19.37,24.2 4 12
Data-Structures-with-Go/binary-tree-1-introduction/binary-tree-1-introduction.go:27.26,29.2 1 12
Data-Structures-with-Go/binary-tree-1-introduction/binary-tree-1-introduction.go:31.13,65.2 8 0
Data-Structures-with-Go/array-rotation/array-rotation.go:12.41,13.25 1 5
Data-Structures-with-Go/array-rotation/array-rotation.go:13.25,15.3 1 19
Data-Structures-with-Go/array-rotation/array-rotation.go:18.39,21.28 3 19
Data-Structures-with-Go/array-rotation/array-rotation.go:24.2,24.15 1 19
Data-Structures-with-Go/array-rotation/array-rotation.go:21.28,23.3 1 100
Data-Structures-with-Go/array-rotation/array-rotation.go:27.37,28.21 1 0
Data-Structures-with-Go/array-rotation/array-rotation.go:32.2,32.28 1 0
Data-Structures-with-Go/array-rotation/array-rotation.go:28.21,31.3 2 0
Data-Structures-with-Go/array-rotation/array-rotation.go:32.28,34.3 1 0
Data-Structures-with-Go/array-rotation/array-rotation.go:37.13,45.2 7 0
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:18.29,21.2 2 46
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:24.18,26.2 1 46
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:29.29,31.2 1 0
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:34.29,36.44 2 0
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:39.2,39.16 1 0
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:36.44,38.3 1 0
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:42.42,57.2 4 30
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:71.51,83.2 4 9
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:88.42,98.6 4 4
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:117.2,117.19 1 4
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:98.6,99.15 1 13
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:108.3,108.23 1 9
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:114.3,114.19 1 9
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:99.15,102.9 2 2
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:103.9,103.22 1 11
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:103.22,105.9 2 2
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:108.23,110.4 1 3
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:110.9,112.4 1 6
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:121.25,122.15 1 0
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:122.15,126.3 3 0
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:129.33,131.15 2 0
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:135.2,135.13 1 0
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:131.15,134.3 2 0
Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:138.13,157.2 12 0
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:18.29,21.2 2 19
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:24.18,26.2 1 19
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:29.29,31.2 1 16
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:34.29,36.44 2 0
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:39.2,39.16 1 0
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:36.44,38.3 1 0
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:42.42,57.2 4 10
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:59.31,64.21 4 3
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:71.2,71.18 1 3
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:64.21,69.3 4 13
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:75.24,76.15 1 0
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:76.15,79.3 2 0
Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:82.13,101.2 10 0
Data-Structures-with-Go/merge-sort/merge-sort.go:12.36,22.25 6 19
Data-Structures-with-Go/merge-sort/merge-sort.go:26.2,26.25 1 19
Data-Structures-with-Go/merge-sort/merge-sort.go:31.2,35.23 4 19
Data-Structures-with-Go/merge-sort/merge-sort.go:47.2,47.13 1 19
Data-Structures-with-Go/merge-sort/merge-sort.go:54.2,54.13 1 19
Data-Structures-with-Go/merge-sort/merge-sort.go:22.25,24.3 1 34
Data-Structures-with-Go/merge-sort/merge-sort.go:26.25,28.3 1 28
Data-Structures-with-Go/merge-sort/merge-sort.go:35.23,36.19 1 35
Data-Structures-with-Go/merge-sort/merge-sort.go:43.3,43.6 1 35
Data-Structures-with-Go/merge-sort/merge-sort.go:36.19,39.4 2 17
Data-Structures-with-Go/merge-sort/merge-sort.go:39.9,42.4 2 18
Data-Structures-with-Go/merge-sort/merge-sort.go:47.13,51.3 3 17
Data-Structures-with-Go/merge-sort/merge-sort.go:54.13,58.3 3 10
Data-Structures-with-Go/merge-sort/merge-sort.go:61.37,62.11 1 42
Data-Structures-with-Go/merge-sort/merge-sort.go:62.11,71.3 4 19
Data-Structures-with-Go/merge-sort/merge-sort.go:74.36,75.28 1 0
Data-Structures-with-Go/merge-sort/merge-sort.go:78.2,78.18 1 0
Data-Structures-with-Go/merge-sort/merge-sort.go:75.28,77.3 1 0
Data-Structures-with-Go/merge-sort/merge-sort.go:81.13,92.2 7 0
Data-Structures-with-Go/quick-sort/quick-sort.go:12.27,16.2 3 38
Data-Structures-with-Go/quick-sort/quick-sort.go:18.47,25.34 3 15
Data-Structures-with-Go/quick-sort/quick-sort.go:33.2,35.16 2 15
Data-Structures-with-Go/quick-sort/quick-sort.go:25.34,27.22 1 49
Data-Structures-with-Go/quick-sort/quick-sort.go:27.22,30.4 2 23
Data-Structures-with-Go/quick-sort/quick-sort.go:43.43,44.17 1 34
Data-Structures-with-Go/quick-sort/quick-sort.go:44.17,51.3 3 15
Data-Structures-with-Go/quick-sort/quick-sort.go:54.38,55.28 1 0
Data-Structures-with-Go/quick-sort/quick-sort.go:58.2,58.18 1 0
Data-Structures-with-Go/quick-sort/quick-sort.go:55.28,57.3 1 0
Data-Structures-with-Go/quick-sort/quick-sort.go:61.13,69.2 5 0
Data-Structures-with-Go/stack/stack.go:24.44,29.2 4 4
Data-Structures-with-Go/stack/stack.go:32.32,34.2 1 4
Data-Structures-with-Go/stack/stack.go:37.32,39.2 1 14
Data-Structures-with-Go/stack/stack.go:42.33,44.2 1 14
Data-Structures-with-Go/stack/stack.go:46.35,47.19 1 14
Data-Structures-with-Go/stack/stack.go:50.2,51.31 2 14
Data-Structures-with-Go/stack/stack.go:47.19,49.3 1 0
Data-Structures-with-Go/stack/stack.go:54.28,55.20 1 14
Data-Structures-with-Go/stack/stack.go:58.2,60.13 3 14
Data-Structures-with-Go/stack/stack.go:55.20,57.3 1 0
Data-Structures-with-Go/stack/stack.go:63.13,74.2 8 0
================================================
FILE: linked-list-1-introduction/README.md
================================================
<h1 align="center">LinkedList | SET 1 (INTRODUCTION) Source</h1>
[What It Is](#what-it-is)
## What It Is
Like arrays, LinkedList is a linear dat structure. Unlike arrays, LinkedList elements are not stored at contigous location; the elements are linked using pointers.

Why Linked List ?
--------------------------
Arrays can be used to store linear data of similar types, but arrays have following limitations ;
* 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.
* 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.
**Example**
> * ID[] = [1000, 1010, 1050, 2000, 2040]
And 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).
Deletion 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.
Advantages Over Arrays
--------------------------
* 1) Dynamic size
* 2) Ease of insertion/deletion
Drawbacks
--------------------------
* 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.
* 2) Extra memory space for a pointer is required with each element of the list.
================================================
FILE: linked-list-1-introduction/linked-list-1-introduction.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
next *Node
}
//Returns an initialized list
func (n *Node) Init() *Node {
n.data = -1
return n
}
//Returns an new list
func New() *Node {
return new(Node).Init()
}
//Returns the first node in list
func (n *Node) Next() *Node {
return n.next
}
//Returns the last node in list if exist, otherwise returns current
func (n *Node) Back() *Node {
current := n.next
for current != nil && current.next != nil {
current = current.next
}
return current
}
//This function prints contents of linked list starting from the given node
func printList(n *Node){
for n != nil {
fmt.Println(n.data)
n = n.next
}
}
func main() {
//To allocate dynamically a new Node in C language : head = (struct Node*) malloc(sizeof(struct Node));
head := New()
second := New()
third := New()
//Assign data in first node
head.data = 1
//Link first node with second node
head.next = second
second.data = 2
second.next = third
third.data = 3
third.next = nil
printList(head)
}
================================================
FILE: linked-list-2-inserting-a-node/README.md
================================================
<h1 align="center">LinkedList | SET 2 (INSERTING A NODE) Source</h1>
[What It Is](#what-it-is)
## What It Is
We 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.
All programs discussed in this post consider following representations of linked list .
In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways ;
* 1) At the front of the linked list
* 2) After a given node.
* 3) At the end of the linked list.
Add a node at the front (A 4 steps process)
--------------------------
The 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.

**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(1)` |
Add a node after a given node (5 steps process)
--------------------------
We are given pointer to a node, and the new node is inserted after the given node.

**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(1)` |
Add a node at the end (6 steps process)
--------------------------
The 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`.
Since 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.

**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(n)` |
================================================
FILE: linked-list-2-inserting-a-node/linked-list-2-inserting-a-node.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
next *Node
}
//Returns an initialized list
func (n *Node) Init() *Node {
n.data = -1
return n
}
//Returns an new list
func New() *Node {
return new(Node).Init()
}
//Returns the first node in list
func (n *Node) Next() *Node {
return n.next
}
//Returns the last node in list if exist, otherwise returns current
func (n *Node) Back() *Node {
current := n.next
for current != nil && current.next != nil {
current = current.next
}
return current
}
func Push(head_ref **Node, new_data int) {
//new_node := Node{data: new_data, next: (*head_ref)}
//*head_ref = new_node
//1. Allocate new node
new_node := New()
//2. Put in the data
new_node.data = new_data
//3. Make next of new node as head
new_node.next = (*head_ref)
//4. Move the head to point to new_node
*head_ref = new_node
}
func InsertAfter(prev_node *Node, new_data int) {
//1. Check if the given prev_node is NULL
if(prev_node == nil){
fmt.Println("The given previous node cannot be NULL")
return
}
//2. Allocate new node
new_node := New()
//3. Put in the data
new_node.data = new_data
//4. Make next of new node as next of prev_node
new_node.next = prev_node.next
//5. Move the next of prev_node as new_node
prev_node.next = new_node
}
func Append(head_ref **Node, new_data int) {
//1. Allocate new node
new_node := New()
last := *head_ref
//2. Put in the data
new_node.data = new_data
//3. this new node is going to be last node, so make next of it as NULL
new_node.next = nil
//4. If the LinkedList is empty, then make the new node as head
if(*head_ref == nil){
*head_ref = new_node
return
}
//5. Else traverse till the last node
for(last.next != nil){
last = last.next
}
//6. Change the next of last node
last.next = new_node
return
}
//This function prints contents of linked list starting from the given node
func printList(n *Node){
for n != nil {
fmt.Println(n.data)
n = n.next
}
}
func main() {
//Start with the empty list
head := New()
//Insert 6.
//So LinkedList becomes [NULL->6]
Append(&head, 6)
//Insert 7 at the beginning.
//So LinkedList becomes [7->NULL->6]
Push(&head, 7)
//Insert 1 at the beginning.
//So LinkedList becomes [1->7->NULL->6]
Push(&head, 1)
//Insert 4 at the end.
//So LinkedList becomes [1->7->NULL->6->4]
Append(&head, 4)
//Insert 8, after 7
//So LinkedList becomes [1->7->8->NULL->6->4]
InsertAfter(head.next, 8)
fmt.Println("Created LinkedList is: ")
printList(head)
}
================================================
FILE: linked-list-3-deleting-a-node/README.md
================================================
<h1 align="center">LinkedList | SET 3 (DELETING A NODE) Source</h1>
[What It Is](#what-it-is)
## What It Is
We 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.
Let us formulate the problem statement to understand the deletion process. Given a `key`, delete the first occurrence of this key in linked list.
To delete a node from linked list, we need to do following steps.
* 1) Find previous node of the node to be deleted.
* 2) Changed next of previous node.
* 3) Free memory for the node to be deleted.
> * Input: Linked List = `[7 -> 1 -> 3 -> 2]`
> * Output: Created Linked List `[2 -> 3 -> 1 -> 7]`
> * Output: Linked List after Deletion of 1: `[2 -> 3 -> 7]`
> * Input: Position = 1, Linked List = `[8 -> 2 -> 3 -> 1 -> 7]`
> * Output: Linked List = `[8 -> 3 -> 1 -> 7]`
> * Input: Position = 0, Linked List = `[8 -> 2 -> 3 -> 1 -> 7]`
> * Output: Linked List = `[2 -> 3 -> 1 -> 7]`
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(1)` |
================================================
FILE: linked-list-3-deleting-a-node/linked-list-3-deleting-a-node.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
next *Node
}
//Returns an initialized list
func (n *Node) Init() *Node {
n.data = -1
return n
}
//Returns an new list
func New() *Node {
return new(Node).Init()
}
//Returns the first node in list
func (n *Node) Next() *Node {
return n.next
}
//Returns the last node in list if exist, otherwise returns current
func (n *Node) Back() *Node {
current := n.next
for current != nil && current.next != nil {
current = current.next
}
return current
}
func Push(head_ref **Node, new_data int) {
//new_node := Node{data: new_data, next: (*head_ref)}
//*head_ref = new_node
//1. Allocate new node
new_node := New()
//2. Put in the data
new_node.data = new_data
//3. Make next of new node as head
new_node.next = (*head_ref)
//4. Move the head to point to new_node
*head_ref = new_node
}
func DeleteNodeWithData(head_ref **Node, delete_data int) {
//Store head node
temp := *head_ref
prev := *head_ref
//If head node itself holds the delete_data to be deleted
if(temp != nil && temp.data == delete_data){
*head_ref = temp.next;
temp = nil
return
}
//Search for the delete_data to be deleted, keep track of the previous node as we need to change prev->next
for(temp != nil && temp.data != delete_data){
prev = temp
temp = temp.next
}
// If delete_data was not present in linked list
if(temp == nil){
return
}
//Unlink the node from linked list
prev.next = temp.next
temp = nil
}
func DeleteNodeWithPosition(head_ref **Node, delete_position int){
//If LinkedList is empty
if(*head_ref == nil){
return
}
//Store head node
temp := *head_ref
//If head needs to be removed
if(delete_position == 0){
*head_ref = temp.next
temp = nil
return
}
//Find previous node of the node to be deleted
for i := 0; temp != nil && i < delete_position - 1; i++ {
temp = temp.next
}
//If position is more than number of nodes
if(temp == nil || temp.next == nil){
return
}
//Node temp->next is the node to be deleted, Store pointer to the next of node to be deleted
next := temp.next.next
//Unlink the node from linked list
temp.next = nil
//Unlink the deleted node from list
temp.next = next
}
//This function prints contents of linked list starting from the given node
func printList(n *Node){
for n != nil {
fmt.Println(n.data)
n = n.next
}
}
func main() {
//Start with the empty list
head := New()
Push(&head, 7)
Push(&head, 1)
Push(&head, 3)
Push(&head, 2)
Push(&head, 8)
fmt.Println("Created LinkedList is: ")
printList(head)
DeleteNodeWithData(&head, 1)
DeleteNodeWithPosition(&head, 4)
fmt.Println("LinkedList after Deletion of 1: ")
printList(head)
}
================================================
FILE: linked-list-circular-1-introduction/README.md
================================================
================================================
FILE: linked-list-circular-2-traversal/README.md
================================================
================================================
FILE: linked-list-circular-2-traversal/linked-list-circular-2-traversal.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
next *Node
}
//Returns an initialized list
func (n *Node) Init() *Node {
n.data = -1
return n
}
//Returns an new list
func New() *Node {
return new(Node).Init()
}
//Returns the first node in list
func (n *Node) Next() *Node {
return n.next
}
//Returns the last node in list if exist, otherwise returns current
func (n *Node) Back() *Node {
current := n.next
for current != nil && current.next != nil {
current = current.next
}
return current
}
//This function prints contents of linked list starting from the given node
func printList(n *Node) {
for n != nil {
fmt.Println(n.data)
n = n.next
}
}
func Push(head_ref **Node, new_data int) {
//1. Allocate new node
new_node := New()
temp := *head_ref
new_node.data = new_data
new_node.next = *head_ref
//2. If linked list is not NULL then set the next of last node
if *head_ref != nil {
for temp.next != *head_ref {
temp = temp.next
}
temp.next = new_node
} else {
//For the fist node
new_node.next = new_node
}
//4. Move the head to point to new_node
*head_ref = new_node
}
func main() {
//Start with the empty list
head := New()
Push(&head, 12)
Push(&head, 56)
Push(&head, 2)
Push(&head, 11)
fmt.Println("Created Circular LinkedList is: ")
printList(head)
}
================================================
FILE: linked-list-circular-singly-1-insertion/README.md
================================================
================================================
FILE: linked-list-circular-singly-1-insertion/linked-list-circular-singly-1-insertion.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
next *Node
}
//Returns an initialized list
func (n *Node) Init() *Node {
n.data = -1
return n
}
//Returns an new list
func New() *Node {
return new(Node).Init()
}
//Returns the first node in list
func (n *Node) Next() *Node {
return n.next
}
//Returns the last node in list if exist, otherwise returns current
func (n *Node) Back() *Node {
current := n.next
for current != nil && current.next != nil {
current = current.next
}
return current
}
//Function to insert node in an empty List
func AddToEmpty(last *Node, data int) *Node {
if(last != nil){
return last
}
//Creating a node
temp := New()
//Assigning the data
temp.data = data
last = temp
//Creating the link
last.next = last
return last
}
//Function to insert node in the beginning of the List
func AddBegin(last *Node, data int) *Node {
if(last == nil){
return AddToEmpty(last, data)
}
//Creating a node
temp := New()
//Assigning the data
temp.data = data
//List was empty. We link single node to itself
temp.next = last.next
last.next = temp
return last
}
//Function to insert node in the end of the List
func AddEnd(last *Node, data int) *Node {
if(last == nil){
return AddToEmpty(last, data)
}
//Creating a node
temp := New()
//Assigning the data
temp.data = data
//List was empty. We link single node to itself
temp.next = last.next
last.next = temp
last = temp
return last
}
//Function to insert node in the end of the List
func AddAfter(last *Node, data int, item int) *Node {
if(last == nil){
return nil
}
//Creating a node
temp := New()
p := New()
p = last.next
for(p != last.next){
if(p.data == item){
//Assigning the data
temp.data = data
//Adjusting the links
temp.next = p.next
//Adding newly allocated node after p
p.next = temp
//Checking for the last node
if(p == last){
last = temp
}
return last
}
p = p.next
}
fmt.Printf("\n%d not present in the list.\n", item)
return last
}
func Traverse(last *Node){
p := New()
//If list is empty, return
if(last == nil){
fmt.Println("List is empty")
return
}
//Pointing to first Node of the list
p = last.next
//Traversing the list
for p != nil {
fmt.Println(p.data)
p = p.next
}
}
//This function prints contents of linked list starting from the given node
func printList(n *Node){
for n != nil {
fmt.Println(n.data)
n = n.next
}
}
func main() {
last := New()
last = AddEnd(last, 12)
last = AddToEmpty(last, 6)
last = AddBegin(last, 4)
last = AddBegin(last, 2)
last = AddEnd(last, 8)
last = AddAfter(last, 10, 8)
Traverse(last)
}
================================================
FILE: linked-list-find-length/README.md
================================================
<h1 align="center">LinkedList Find Length Source</h1>
[What It Is](#what-it-is)
## What It Is
For example, the function should return 5 for linked list `[1 -> 3 -> 1 -> 2 -> 1]`

METHOD 1 (Iterative Solution)
--------------------------
* 1) Initialize count as 0
* 2) Initialize a node pointer, current = head.
* 3) Do following while current is not NULL
a) current = current -> next
b) count++;
* 4) Return count
> * Input: Linked List = `[1 -> 3 -> 1 -> 2 -> 1]`
> * Output: count of nodes is `5`
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(n)` |
================================================
FILE: linked-list-find-length/linked-list-find-length.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
next *Node
}
//Returns an initialized list
func (n *Node) Init() *Node {
n.data = -1
return n
}
//Returns an new list
func New() *Node {
return new(Node).Init()
}
//Returns the first node in list
func (n *Node) Next() *Node {
return n.next
}
//Returns the last node in list if exist, otherwise returns current
func (n *Node) Back() *Node {
current := n.next
for current != nil && current.next != nil {
current = current.next
}
return current
}
func Push(head_ref **Node, new_data int) {
//new_node := Node{data: new_data, next: (*head_ref)}
//*head_ref = new_node
//1. Allocate new node
new_node := New()
//2. Put in the data
new_node.data = new_data
//3. Make next of new node as head
new_node.next = (*head_ref)
//4. Move the head to point to new_node
*head_ref = new_node
}
func GetCount(head *Node) int {
var count int = 0
current := head
for(current != nil){
count++
current = current.next
}
return count
}
//This function prints contents of linked list starting from the given node
func printList(n *Node){
for n != nil {
fmt.Println(n.data)
n = n.next
}
}
func main() {
//Start with the empty list
head := New()
Push(&head, 1)
Push(&head, 3)
Push(&head, 1)
Push(&head, 2)
Push(&head, 1)
fmt.Printf("Count of Nodes is %d", GetCount(head))
}
================================================
FILE: linked-list-merge-two-sorted/README.md
================================================
<h1 align="center">Merge Two Sorted Source</h1>
[What It Is](#what-it-is)
## What It Is
Write 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
together the nodes of the first two lists.
> * Input: LinkedList a `5->10->15`
> * Input: LinkedList b `2->3->20`
> * Output: `2->3->5->10->15->20`
There 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`.
METHOD 1 (Using Dummy Nodes)
--------------------------
The 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.
The 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
we are done, the result is in dummy.next
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(n)` |
================================================
FILE: linked-list-merge-two-sorted/linked-list-merge-two-sorted.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
next *Node
}
//Returns an initialized list
func (n *Node) Init() *Node {
n.data = -1
return n
}
//Returns an new list
func New() *Node {
return new(Node).Init()
}
//Returns the first node in list
func (n *Node) Next() *Node {
return n.next
}
//Returns the last node in list if exist, otherwise returns current
func (n *Node) Back() *Node {
current := n.next
for current != nil && current.next != nil {
current = current.next
}
return current
}
func Push(head_ref **Node, new_data int) {
//new_node := Node{data: new_data, next: (*head_ref)}
//*head_ref = new_node
//1. Allocate new node
new_node := New()
//2. Put in the data
new_node.data = new_data
//3. Make next of new node as head
new_node.next = (*head_ref)
//4. Move the head to point to new_node
*head_ref = new_node
}
//Pull off the front node of the source and put it in dest
/* MoveNode() function takes the node from the front of the
source, and move it to the front of the dest.
It is an error to call this with the source list empty.
Before calling MoveNode():
source == {1, 2, 3}
dest == {1, 2, 3}
Affter calling MoveNode():
source == {2, 3}
dest == {1, 1, 2, 3} */
func MoveNode(dest_ref **Node, source_ref **Node) {
//The front source code
new_node := *source_ref
//Advance the source pointer
*source_ref = new_node.next
//Link the old dest off the new node
new_node.next = *dest_ref
//Move dest to point to the new node
*dest_ref = new_node
}
/* Takes two lists sorted in increasing order, and splices
their nodes together to make one big sorted list which
is returned. */
func SortedMerge(a *Node, b *Node) *Node {
//A dummy first node to hang the result on
dummy := New()
//Tail points to the last result node
tail := dummy
//So tail.next is the place to add new nodes to result
dummy.next = nil
for {
if a == nil {
//If either list runs out, use the other list
tail.next = b
break
} else if b == nil {
tail.next = a
break
}
if a.data <= b.data {
MoveNode(&(tail.next), &a)
} else {
MoveNode(&(tail.next), &b)
}
tail = tail.next
}
return dummy.next
}
//This function prints contents of linked list starting from the given node
func printList(n *Node) {
for n != nil {
fmt.Print(n.data)
fmt.Print(" ,")
n = n.next
}
}
func GetDataList(n *Node) []int {
data := []int{}
for n != nil {
data = append(data, n.data)
n = n.next
}
return data
}
func main() {
//Start with the empty list
res := New()
a := New()
b := New()
Push(&a, 15)
Push(&a, 10)
Push(&a, 5)
Push(&b, 20)
Push(&b, 3)
Push(&b, 2)
//Remove duplicates from LinkedList
res = SortedMerge(a, b)
fmt.Println("Merged LinkedList is:")
printList(res)
}
================================================
FILE: linked-list-merge-two-sorted/linked-list-merge-two-sorted.go_test.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import (
"reflect"
"testing"
)
func TestMergeTwoSortedNew(t *testing.T) {
var testDatas = []struct {
ArrayIn1 []int
ArrayIn2 []int
Out []int
}{
{[]int{20, 4, 15, 85}, []int{6, 7}, []int{20, 4, 15, 85, -1, 6, 7}},
{[]int{10, 20, 30}, []int{5, 6}, []int{10, 20, 30, -1, 5, 6}},
{[]int{7}, []int{7}, []int{7, -1, 7}},
{[]int{}, []int{}, []int{-1}},
}
for _, data := range testDatas {
a := New()
b := New()
for i := 0; i < len(data.ArrayIn1); i++ {
Push(&a, data.ArrayIn1[i])
}
for i := 0; i < len(data.ArrayIn2); i++ {
Push(&b, data.ArrayIn2[i])
}
actual := SortedMerge(a, b)
expected := New()
for i := 0; i < len(data.Out); i++ {
Push(&expected, data.Out[i])
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("MergeTwoSorted: Expected: %v, Actual: %v", GetDataList(expected), GetDataList(actual))
}
}
}
================================================
FILE: linked-list-reverse/README.md
================================================
<h1 align="center">LinkedList Reverse Source</h1>
[What It Is](#what-it-is)
## What It Is
Given 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.
> * Input: Head of following linked list `[1->2->3->4->NULL]`
> * Output: Linked list should be changed to `[4->3->2->1->NULL]`
> * Input: Head of following linked list `[1->2->3->4->5->NULL]`
> * Output: Linked list should be changed to `[5->4->3->2->1->NULL]`
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(n)` |
| `Auxiliary Space` | `O(1)` |
================================================
FILE: linked-list-reverse/linked-list-reverse.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
type Node struct {
data int
next *Node
}
//Returns an initialized list
func (n *Node) Init() *Node {
n.data = -1
return n
}
//Returns an new list
func New() *Node {
return new(Node).Init()
}
//Returns the first node in list
func (n *Node) Next() *Node {
return n.next
}
//Returns the last node in list if exist, otherwise returns current
func (n *Node) Back() *Node {
current := n.next
for current != nil && current.next != nil {
current = current.next
}
return current
}
func Push(head_ref **Node, new_data int) {
//new_node := Node{data: new_data, next: (*head_ref)}
//*head_ref = new_node
//1. Allocate new node
new_node := New()
//2. Put in the data
new_node.data = new_data
//3. Make next of new node as head
new_node.next = (*head_ref)
//4. Move the head to point to new_node
*head_ref = new_node
}
func Reverse(head_ref **Node) {
prev := New()
current := *head_ref
next := New()
for(current != nil){
next = current.next
current.next = prev
prev = current
current = next
}
*head_ref = prev
}
//This function prints contents of linked list starting from the given node
func printList(n *Node){
for n != nil {
fmt.Println(n.data)
n = n.next
}
}
func main() {
//Start with the empty list
head := New()
Push(&head, 20)
Push(&head, 4)
Push(&head, 15)
Push(&head, 85)
fmt.Println("Given LinkedList is:")
printList(head)
Reverse(&head)
fmt.Println("Reversed LinkedList is:")
printList(head)
}
================================================
FILE: linked-list-reverse/linked-list-reverse_test.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import (
"reflect"
"testing"
)
func TestLinkedListReverse(t *testing.T) {
var testDatas = []struct {
Node *Node
ArrayIn []int
ArrayOut []int
}{
{New(), []int{20, 4, 15, 85}, []int{-1, 20, 4, 15, 85, -1}},
{New(), []int{10, 20, 30, 40, 50}, []int{-1, 10, 20, 30, 40, 50, -1}},
{New(), []int{7}, []int{-1, 7, -1}},
}
for _, data := range testDatas {
for i := 0; i < len(data.ArrayIn); i++ {
Push(&data.Node, data.ArrayIn[i])
}
Reverse(&data.Node)
n := data.Node
i := 0
for n != nil {
expected := data.ArrayOut[i]
actual := n.data
if !reflect.DeepEqual(expected, actual) {
t.Errorf("LinkedListReverse: Expected: %d, Actual: %d", expected, actual)
}
n = n.Next()
i++
}
}
}
================================================
FILE: merge-sort/README.md
================================================
<h1 align="center">Merge Sort Source</h1>
[What It Is](#what-it-is)
## What It Is
Like **[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

MergeSort(arr[], l, r)
--------------------------
* If r > l
* 1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
* 2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
* 3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
* 4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
The 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

> * Input: {12, 11, 13, 5, 6, 7}
> * Output: Given array is `12 11 13 5 6 7`
> * Output: Sorted array is `5 6 7 11 12 13`
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:------------:|
| `Time Complexity` | `O(n log n)` |
================================================
FILE: merge-sort/merge-sort.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
func Merge(arr []int, l, m, r int) {
var i, j, k int
var n1 int = m - l + 1
var n2 int = r - m
//Create temp arrays
L := make([]int, n1)
R := make([]int, n2)
//Copy data to temp arrays L[] and R[]
for i = 0; i < n1; i++ {
L[i] = arr[l+i]
}
for j = 0; j < n2; j++ {
R[j] = arr[m+1+j]
}
//Merge the temp arrays back into arr[l..r]
i = 0 //Initial index of first subarray
j = 0 //Initial index of second subarray
k = l //Initial index of merged subarray
for i < n1 && j < n2 {
if L[i] <= R[j] {
arr[k] = L[i]
i++
} else {
arr[k] = R[j]
j++
}
k++
}
//Copy the remaining elements of L[], if there are any
for i < n1 {
arr[k] = L[i]
i++
k++
}
//Copy the remaining elements of R[], if there are any
for j < n2 {
arr[k] = R[j]
j++
k++
}
}
func MergeSort(arr []int, l, r int) {
if l < r {
//Same as (l + r) / 2 but avoids overflow for large l and h
var m int = l + (r-l)/2
//Sort first and second halves
MergeSort(arr, l, m)
MergeSort(arr, m+1, r)
Merge(arr, l, m, r)
}
}
func PrintArray(A []int, size int) {
for i := 0; i < size; i++ {
fmt.Printf("%d ", A[i])
}
fmt.Printf("\n")
}
func main() {
arr := []int{12, 11, 13, 5, 6, 7}
var arr_size int = len(arr)
fmt.Println("Given array size is: ")
PrintArray(arr, arr_size)
MergeSort(arr, 0, arr_size-1)
fmt.Println("Sorted array is: ")
PrintArray(arr, arr_size)
}
================================================
FILE: merge-sort/merge-sort_test.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import (
"reflect"
"testing"
)
func TestMergeSort(t *testing.T) {
var testDatas = []struct {
ArrayIn []int
ArrayOut []int
}{
{[]int{1, 3, 2, 4}, []int{1, 2, 3, 4}},
{[]int{3, 2, 1, 4}, []int{1, 2, 3, 4}},
{[]int{9, 8, 6, 5, 7, 4, 3, 0, 2, 1}, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}},
{[]int{-3, -2, -1, -4, 0}, []int{-4, -3, -2, -1, 0}},
}
for _, data := range testDatas {
expected := data.ArrayOut
MergeSort(data.ArrayIn, 0, len(data.ArrayIn)-1)
actual := data.ArrayIn
if !reflect.DeepEqual(expected, actual) {
t.Errorf("MergeSort: Expected: %d, Actual: %d", expected, actual)
}
}
}
================================================
FILE: quick-sort/README.md
================================================
<h1 align="center">Quick Sort Source</h1>
[What It Is](#what-it-is)
## What It Is
Like **[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.
* Always pick first element as pivot.
* Always pick last element as pivot (implemented below)
* Pick a random element as pivot.
* Pick median as pivot.
The 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.

QuickSort(arr[], start, end)
--------------------------
**Partition Algorithm**
There 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.

> * Input: {10, 7, 8, 9, 1, 5}
> * Output: Sorted array is `1 5 7 8 9 10`
**Algorithm Complexity**
| Complexity | Notation |
| ----------------- |:------------:|
| `Time Complexity` | `O(n log n)` |
| `Auxiliary Space` | `O(n)` |
================================================
FILE: quick-sort/quick-sort.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
func Swap(a *int, b *int) {
t := *a
*a = *b
*b = t
}
func Partition(arr []int, start, end int) int {
pivot := arr[end]
//Index of smaller element
var i int = (start - 1)
for j := start; j <= end-1; j++ {
//If current element is smaller than or equal to pivot
if arr[j] <= pivot {
i++
Swap(&arr[i], &arr[j])
}
}
Swap(&arr[i+1], &arr[end])
return (i + 1)
}
/*The main function that implements QuickSort
arr[] -> Array to be sorted
start -> Starting index
end -> Ending index
*/
func QuickSort(arr []int, start, end int) {
if start < end {
//pi is partitioning index, arr[p] is now at right place
var pi int = Partition(arr, start, end)
//Separately sort elements before partition and after partition
QuickSort(arr, start, pi-1)
QuickSort(arr, pi+1, end)
}
}
func PrintArray(arr []int, size int) {
for i := 0; i < size; i++ {
fmt.Printf("%d ", arr[i])
}
fmt.Printf("\n")
}
func main() {
arr := []int{10, 7, 8, 9, 1, 5}
var n int = len(arr)
QuickSort(arr, 0, n-1)
fmt.Println("Sorted array is: ")
PrintArray(arr, n)
}
================================================
FILE: quick-sort/quick-sort_test.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import (
"reflect"
"testing"
)
func TestQuickSort(t *testing.T) {
var testDatas = []struct {
ArrayIn []int
ArrayOut []int
}{
{[]int{1, 3, 2, 4}, []int{1, 2, 3, 4}},
{[]int{3, 2, 1, 4}, []int{1, 2, 3, 4}},
{[]int{9, 8, 6, 5, 7, 4, 3, 0, 2, 1}, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}},
{[]int{-3, -2, -1, -4, 0}, []int{-4, -3, -2, -1, 0}},
}
for _, data := range testDatas {
expected := data.ArrayOut
QuickSort(data.ArrayIn, 0, len(data.ArrayIn)-1)
actual := data.ArrayIn
if !reflect.DeepEqual(expected, actual) {
t.Errorf("QuickSort: Expected: %d, Actual: %d", expected, actual)
}
}
}
================================================
FILE: stack/stack.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import "fmt"
const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
type Stack struct {
top int
capacity uint
array []int
}
//Returns an initialized list
func (s *Stack) Init(capacity uint) *Stack {
s.top = -1
s.capacity = capacity
s.array = make([]int, capacity)
return s
}
//Returns an new list
func New(capacity uint) *Stack {
return new(Stack).Init(capacity)
}
// Stack is full when top is equal to the last index
func IsFull(stack *Stack) bool {
return stack.top == int(stack.capacity)-1
}
// Stack is empty when top is equal to -1
func IsEmpty(stack *Stack) bool {
return stack.top == -1
}
func Push(stack *Stack, item int) {
if IsFull(stack) {
return
}
stack.top++
stack.array[stack.top] = item
}
func Pop(stack *Stack) int {
if IsEmpty(stack) {
return MinInt
}
temp := stack.array[stack.top]
stack.top--
return temp
}
func main() {
stack := New(100)
Push(stack, 10)
fmt.Println("Pushed to stack : 10")
Push(stack, 20)
fmt.Println("Pushed to stack : 20")
Push(stack, 30)
fmt.Println("Pushed to stack : 30")
fmt.Printf("Popped from stack : %d", Pop(stack))
}
================================================
FILE: stack/stack_test.go
================================================
// ====================================================
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
package main
import (
"reflect"
"testing"
)
func TestStack(t *testing.T) {
var testDatas = []struct {
Stack *Stack
ArrayIn []int
ArrayOut []int
}{
{New(3), []int{10, 20, 30}, []int{30, 20, 10}},
{New(5), []int{10, 20, 30, 40, 50}, []int{50, 40, 30, 20, 10}},
{New(1), []int{7}, []int{7}},
{New(5), []int{15, 10, 22, 33, 77}, []int{77, 33, 22, 10, 15}},
}
for _, data := range testDatas {
for i := uint(0); i < data.Stack.capacity; i++ {
Push(data.Stack, data.ArrayIn[i])
}
for i := uint(0); i < data.Stack.capacity; i++ {
expected := data.ArrayOut[i]
actual := Pop(data.Stack)
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Stack: Expected: %d, Actual: %d", expected, actual)
}
}
}
}
gitextract_9396xqnw/
├── .gitignore
├── .travis.yml
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── appveyor.yml
├── array-minimum-distance/
│ ├── README.md
│ ├── array-minimum-distance.go
│ └── array-minimum-distance_test.go
├── array-rotation/
│ ├── README.md
│ ├── array-rotation.go
│ └── array-rotation_test.go
├── array-smallest-missing-number/
│ ├── README.md
│ ├── array-smallest-missing-number.go
│ └── array-smallest-missing-number_test.go
├── binary-search-tree-1-insertion/
│ ├── binary-search-tree-1-insertion.go
│ └── binary-search-tree-1-insertion_test.go
├── binary-tree-1-introduction/
│ ├── binary-tree-1-introduction.go
│ └── binary-tree-1-introduction_test.go
├── binary-tree-2-traversals-in-pre-post-order/
│ └── binary-tree-traversals-2-in-pre-post-order.go
├── binary-tree-2-traversals-level-order/
│ └── binary-tree-traversals-2-level-order.go
├── binary-tree-3-doubly-linked-list/
│ └── binary-tree-3-doubly-linked-list.go
├── binary-tree-4-delete/
│ └── binary-tree-4-delete.go
├── binary-tree-5-find-min-max/
│ └── binary-tree-5-find-min-max.go
├── coverage.txt
├── linked-list-1-introduction/
│ ├── README.md
│ └── linked-list-1-introduction.go
├── linked-list-2-inserting-a-node/
│ ├── README.md
│ └── linked-list-2-inserting-a-node.go
├── linked-list-3-deleting-a-node/
│ ├── README.md
│ └── linked-list-3-deleting-a-node.go
├── linked-list-circular-1-introduction/
│ └── README.md
├── linked-list-circular-2-traversal/
│ ├── README.md
│ └── linked-list-circular-2-traversal.go
├── linked-list-circular-singly-1-insertion/
│ ├── README.md
│ └── linked-list-circular-singly-1-insertion.go
├── linked-list-find-length/
│ ├── README.md
│ └── linked-list-find-length.go
├── linked-list-merge-two-sorted/
│ ├── README.md
│ ├── linked-list-merge-two-sorted.go
│ └── linked-list-merge-two-sorted.go_test.go
├── linked-list-reverse/
│ ├── README.md
│ ├── linked-list-reverse.go
│ └── linked-list-reverse_test.go
├── merge-sort/
│ ├── README.md
│ ├── merge-sort.go
│ └── merge-sort_test.go
├── quick-sort/
│ ├── README.md
│ ├── quick-sort.go
│ └── quick-sort_test.go
└── stack/
├── stack.go
└── stack_test.go
SYMBOL INDEX (170 symbols across 31 files)
FILE: array-minimum-distance/array-minimum-distance.go
constant MaxUint (line 12) | MaxUint = ^uint(0)
constant MinUint (line 13) | MinUint = 0
constant MaxInt (line 14) | MaxInt = int(MaxUint >> 1)
constant MinInt (line 15) | MinInt = -MaxInt - 1
function Abs (line 17) | func Abs(x int) int {
function minDist (line 27) | func minDist (arr []int, n, x, y int) int {
function main (line 41) | func main() {
FILE: array-minimum-distance/array-minimum-distance_test.go
function TestAbs (line 15) | func TestAbs(t *testing.T) {
function TestMinDist (line 37) | func TestMinDist(t *testing.T) {
FILE: array-rotation/array-rotation.go
function leftRotate (line 12) | func leftRotate(arr []int, d int, n int){
function leftRotateByOne (line 18) | func leftRotateByOne(arr []int, n int){
function printArray (line 27) | func printArray(arr []int, size int){
function main (line 37) | func main() {
FILE: array-rotation/array-rotation_test.go
function TestLeftRotate (line 15) | func TestLeftRotate(t *testing.T) {
FILE: array-smallest-missing-number/array-smallest-missing-number.go
function findFirstMissing (line 13) | func findFirstMissing(arr []int, start, end int) int {
function main (line 37) | func main() {
FILE: array-smallest-missing-number/array-smallest-missing-number_test.go
function TestFindFirstMissing (line 15) | func TestFindFirstMissing(t *testing.T) {
FILE: binary-search-tree-1-insertion/binary-search-tree-1-insertion.go
type Node (line 12) | type Node struct
method Init (line 19) | func (n *Node) Init(data int) *Node {
function New (line 27) | func New(data int) *Node {
function Search (line 32) | func Search(root *Node, key int) *Node {
function PrintInOrder (line 49) | func PrintInOrder(root *Node) {
function Insert (line 58) | func Insert(node *Node, key int) *Node {
function main (line 75) | func main() {
FILE: binary-search-tree-1-insertion/binary-search-tree-1-insertion_test.go
function TestBinarySearchTreeNew (line 15) | func TestBinarySearchTreeNew(t *testing.T) {
FILE: binary-tree-1-introduction/binary-tree-1-introduction.go
type Node (line 12) | type Node struct
method Init (line 19) | func (n *Node) Init(data int) *Node {
function New (line 27) | func New(data int) *Node {
function main (line 31) | func main() {
FILE: binary-tree-1-introduction/binary-tree-1-introduction_test.go
function TestBinaryTreeNew (line 15) | func TestBinaryTreeNew(t *testing.T) {
FILE: binary-tree-2-traversals-in-pre-post-order/binary-tree-traversals-2-in-pre-post-order.go
type Node (line 12) | type Node struct
method Init (line 19) | func (n *Node) Init(data int) *Node {
function New (line 27) | func New(data int) *Node {
function PrintPostOrder (line 31) | func PrintPostOrder(node *Node) {
function PrintInOrder (line 47) | func PrintInOrder(node *Node) {
function PrintPreOrder (line 63) | func PrintPreOrder(node *Node) {
function main (line 79) | func main() {
FILE: binary-tree-2-traversals-level-order/binary-tree-traversals-2-level-order.go
type Node (line 12) | type Node struct
method Init (line 19) | func (n *Node) Init(data int) *Node {
function New (line 27) | func New(data int) *Node {
function GetHeight (line 34) | func GetHeight(node *Node) int {
function PrintGivenLevel (line 52) | func PrintGivenLevel(root *Node, level int) {
function PrintLevelOrder (line 67) | func PrintLevelOrder(root *Node) {
function main (line 81) | func main() {
FILE: binary-tree-3-doubly-linked-list/binary-tree-3-doubly-linked-list.go
type Node (line 12) | type Node struct
method Init (line 19) | func (n *Node) Init(data int) *Node {
function New (line 27) | func New(data int) *Node {
function InOrder (line 32) | func InOrder(root *Node) {
function FixPrevPtr (line 48) | func FixPrevPtr(root *Node) {
function FixNextPtr (line 63) | func FixNextPtr(root *Node) *Node {
function BTToDLL (line 81) | func BTToDLL(root *Node) *Node {
function PrintList (line 89) | func PrintList(root *Node) {
function main (line 96) | func main() {
FILE: binary-tree-4-delete/binary-tree-4-delete.go
type Node (line 12) | type Node struct
method Init (line 19) | func (n *Node) Init(data int) *Node {
function New (line 27) | func New(data int) *Node {
function DeleteTree (line 34) | func DeleteTree(node *Node) {
function main (line 50) | func main() {
FILE: binary-tree-5-find-min-max/binary-tree-5-find-min-max.go
constant MaxUint (line 12) | MaxUint = ^uint(0)
constant MinUint (line 13) | MinUint = 0
constant MaxInt (line 14) | MaxInt = int(MaxUint >> 1)
constant MinInt (line 15) | MinInt = -MaxInt - 1
type Node (line 17) | type Node struct
method Init (line 24) | func (n *Node) Init(data int) *Node {
function New (line 32) | func New(data int) *Node {
function FindMin (line 37) | func FindMin(root *Node) int {
function FindMax (line 59) | func FindMax(root *Node) int {
function main (line 80) | func main() {
FILE: linked-list-1-introduction/linked-list-1-introduction.go
type Node (line 12) | type Node struct
method Init (line 18) | func (n *Node) Init() *Node {
method Next (line 29) | func (n *Node) Next() *Node {
method Back (line 34) | func (n *Node) Back() *Node {
function New (line 24) | func New() *Node {
function printList (line 43) | func printList(n *Node){
function main (line 50) | func main() {
FILE: linked-list-2-inserting-a-node/linked-list-2-inserting-a-node.go
type Node (line 12) | type Node struct
method Init (line 18) | func (n *Node) Init() *Node {
method Next (line 29) | func (n *Node) Next() *Node {
method Back (line 34) | func (n *Node) Back() *Node {
function New (line 24) | func New() *Node {
function Push (line 42) | func Push(head_ref **Node, new_data int) {
function InsertAfter (line 59) | func InsertAfter(prev_node *Node, new_data int) {
function Append (line 79) | func Append(head_ref **Node, new_data int) {
function printList (line 108) | func printList(n *Node){
function main (line 115) | func main() {
FILE: linked-list-3-deleting-a-node/linked-list-3-deleting-a-node.go
type Node (line 12) | type Node struct
method Init (line 18) | func (n *Node) Init() *Node {
method Next (line 29) | func (n *Node) Next() *Node {
method Back (line 34) | func (n *Node) Back() *Node {
function New (line 24) | func New() *Node {
function Push (line 42) | func Push(head_ref **Node, new_data int) {
function DeleteNodeWithData (line 59) | func DeleteNodeWithData(head_ref **Node, delete_data int) {
function DeleteNodeWithPosition (line 88) | func DeleteNodeWithPosition(head_ref **Node, delete_position int){
function printList (line 125) | func printList(n *Node){
function main (line 132) | func main() {
FILE: linked-list-circular-2-traversal/linked-list-circular-2-traversal.go
type Node (line 12) | type Node struct
method Init (line 18) | func (n *Node) Init() *Node {
method Next (line 29) | func (n *Node) Next() *Node {
method Back (line 34) | func (n *Node) Back() *Node {
function New (line 24) | func New() *Node {
function printList (line 43) | func printList(n *Node) {
function Push (line 50) | func Push(head_ref **Node, new_data int) {
function main (line 72) | func main() {
FILE: linked-list-circular-singly-1-insertion/linked-list-circular-singly-1-insertion.go
type Node (line 12) | type Node struct
method Init (line 18) | func (n *Node) Init() *Node {
method Next (line 29) | func (n *Node) Next() *Node {
method Back (line 34) | func (n *Node) Back() *Node {
function New (line 24) | func New() *Node {
function AddToEmpty (line 43) | func AddToEmpty(last *Node, data int) *Node {
function AddBegin (line 62) | func AddBegin(last *Node, data int) *Node {
function AddEnd (line 81) | func AddEnd(last *Node, data int) *Node {
function AddAfter (line 101) | func AddAfter(last *Node, data int, item int) *Node {
function Traverse (line 137) | func Traverse(last *Node){
function printList (line 157) | func printList(n *Node){
function main (line 164) | func main() {
FILE: linked-list-find-length/linked-list-find-length.go
type Node (line 12) | type Node struct
method Init (line 18) | func (n *Node) Init() *Node {
method Next (line 29) | func (n *Node) Next() *Node {
method Back (line 34) | func (n *Node) Back() *Node {
function New (line 24) | func New() *Node {
function Push (line 42) | func Push(head_ref **Node, new_data int) {
function GetCount (line 59) | func GetCount(head *Node) int {
function printList (line 72) | func printList(n *Node){
function main (line 79) | func main() {
FILE: linked-list-merge-two-sorted/linked-list-merge-two-sorted.go
type Node (line 12) | type Node struct
method Init (line 18) | func (n *Node) Init() *Node {
method Next (line 29) | func (n *Node) Next() *Node {
method Back (line 34) | func (n *Node) Back() *Node {
function New (line 24) | func New() *Node {
function Push (line 42) | func Push(head_ref **Node, new_data int) {
function MoveNode (line 71) | func MoveNode(dest_ref **Node, source_ref **Node) {
function SortedMerge (line 88) | func SortedMerge(a *Node, b *Node) *Node {
function printList (line 121) | func printList(n *Node) {
function GetDataList (line 129) | func GetDataList(n *Node) []int {
function main (line 138) | func main() {
FILE: linked-list-merge-two-sorted/linked-list-merge-two-sorted.go_test.go
function TestMergeTwoSortedNew (line 15) | func TestMergeTwoSortedNew(t *testing.T) {
FILE: linked-list-reverse/linked-list-reverse.go
type Node (line 12) | type Node struct
method Init (line 18) | func (n *Node) Init() *Node {
method Next (line 29) | func (n *Node) Next() *Node {
method Back (line 34) | func (n *Node) Back() *Node {
function New (line 24) | func New() *Node {
function Push (line 42) | func Push(head_ref **Node, new_data int) {
function Reverse (line 59) | func Reverse(head_ref **Node) {
function printList (line 75) | func printList(n *Node){
function main (line 82) | func main() {
FILE: linked-list-reverse/linked-list-reverse_test.go
function TestLinkedListReverse (line 15) | func TestLinkedListReverse(t *testing.T) {
FILE: merge-sort/merge-sort.go
function Merge (line 12) | func Merge(arr []int, l, m, r int) {
function MergeSort (line 61) | func MergeSort(arr []int, l, r int) {
function PrintArray (line 74) | func PrintArray(A []int, size int) {
function main (line 81) | func main() {
FILE: merge-sort/merge-sort_test.go
function TestMergeSort (line 15) | func TestMergeSort(t *testing.T) {
FILE: quick-sort/quick-sort.go
function Swap (line 12) | func Swap(a *int, b *int) {
function Partition (line 18) | func Partition(arr []int, start, end int) int {
function QuickSort (line 43) | func QuickSort(arr []int, start, end int) {
function PrintArray (line 54) | func PrintArray(arr []int, size int) {
function main (line 61) | func main() {
FILE: quick-sort/quick-sort_test.go
function TestQuickSort (line 15) | func TestQuickSort(t *testing.T) {
FILE: stack/stack.go
constant MaxUint (line 12) | MaxUint = ^uint(0)
constant MinUint (line 13) | MinUint = 0
constant MaxInt (line 14) | MaxInt = int(MaxUint >> 1)
constant MinInt (line 15) | MinInt = -MaxInt - 1
type Stack (line 17) | type Stack struct
method Init (line 24) | func (s *Stack) Init(capacity uint) *Stack {
function New (line 32) | func New(capacity uint) *Stack {
function IsFull (line 37) | func IsFull(stack *Stack) bool {
function IsEmpty (line 42) | func IsEmpty(stack *Stack) bool {
function Push (line 46) | func Push(stack *Stack, item int) {
function Pop (line 54) | func Pop(stack *Stack) int {
function main (line 63) | func main() {
FILE: stack/stack_test.go
function TestStack (line 15) | func TestStack(t *testing.T) {
Condensed preview — 52 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (94K chars).
[
{
"path": ".gitignore",
"chars": 275,
"preview": "# 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"
},
{
"path": ".travis.yml",
"chars": 162,
"preview": "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 "
},
{
"path": "CONTRIBUTING.md",
"chars": 0,
"preview": ""
},
{
"path": "LICENSE",
"chars": 1070,
"preview": "MIT License\n\nCopyright (c) 2017 Furkan Türkal\n\nPermission is hereby granted, free of charge, to any person obtaining a c"
},
{
"path": "README.md",
"chars": 3608,
"preview": "<h1 align=\"center\">Data Structures with Go</h1>\n\n[](http"
},
{
"path": "appveyor.yml",
"chars": 142,
"preview": "version: \"{build}\"\nskip_tags: true\nclone_depth: 1\n\nenvironment:\n GOVERSION: 1.11\n\nbuild: false\ndeploy: false\n\ninstall:\n"
},
{
"path": "array-minimum-distance/README.md",
"chars": 1247,
"preview": "<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 arr"
},
{
"path": "array-minimum-distance/array-minimum-distance.go",
"chars": 1098,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "array-minimum-distance/array-minimum-distance_test.go",
"chars": 1353,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "array-rotation/README.md",
"chars": 1419,
"preview": "<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 `ro"
},
{
"path": "array-rotation/array-rotation.go",
"chars": 1071,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "array-rotation/array-rotation_test.go",
"chars": 1166,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "array-smallest-missing-number/README.md",
"chars": 2187,
"preview": "<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 a"
},
{
"path": "array-smallest-missing-number/array-smallest-missing-number.go",
"chars": 958,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "array-smallest-missing-number/array-smallest-missing-number_test.go",
"chars": 1043,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "binary-search-tree-1-insertion/binary-search-tree-1-insertion.go",
"chars": 2170,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "binary-search-tree-1-insertion/binary-search-tree-1-insertion_test.go",
"chars": 1017,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "binary-tree-1-introduction/binary-tree-1-introduction.go",
"chars": 1448,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "binary-tree-1-introduction/binary-tree-1-introduction_test.go",
"chars": 1104,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "binary-tree-2-traversals-in-pre-post-order/binary-tree-traversals-2-in-pre-post-order.go",
"chars": 2011,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "binary-tree-2-traversals-level-order/binary-tree-traversals-2-level-order.go",
"chars": 2045,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "binary-tree-3-doubly-linked-list/binary-tree-3-doubly-linked-list.go",
"chars": 2474,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "binary-tree-4-delete/binary-tree-4-delete.go",
"chars": 1403,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "binary-tree-5-find-min-max/binary-tree-5-find-min-max.go",
"chars": 2047,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "coverage.txt",
"chars": 10871,
"preview": "mode: atomic\nData-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:13.54,14.15 1 11\nDat"
},
{
"path": "linked-list-1-introduction/README.md",
"chars": 1668,
"preview": "<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,"
},
{
"path": "linked-list-1-introduction/linked-list-1-introduction.go",
"chars": 1429,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "linked-list-2-inserting-a-node/README.md",
"chars": 2649,
"preview": "<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 "
},
{
"path": "linked-list-2-inserting-a-node/linked-list-2-inserting-a-node.go",
"chars": 2927,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "linked-list-3-deleting-a-node/README.md",
"chars": 1279,
"preview": "<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 d"
},
{
"path": "linked-list-3-deleting-a-node/linked-list-3-deleting-a-node.go",
"chars": 3109,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "linked-list-circular-1-introduction/README.md",
"chars": 0,
"preview": ""
},
{
"path": "linked-list-circular-2-traversal/README.md",
"chars": 0,
"preview": ""
},
{
"path": "linked-list-circular-2-traversal/linked-list-circular-2-traversal.go",
"chars": 1716,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "linked-list-circular-singly-1-insertion/README.md",
"chars": 0,
"preview": ""
},
{
"path": "linked-list-circular-singly-1-insertion/linked-list-circular-singly-1-insertion.go",
"chars": 3060,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "linked-list-find-length/README.md",
"chars": 786,
"preview": "<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 functi"
},
{
"path": "linked-list-find-length/linked-list-find-length.go",
"chars": 1761,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "linked-list-merge-two-sorted/README.md",
"chars": 1411,
"preview": "<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()` funct"
},
{
"path": "linked-list-merge-two-sorted/linked-list-merge-two-sorted.go",
"chars": 3184,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "linked-list-merge-two-sorted/linked-list-merge-two-sorted.go_test.go",
"chars": 1280,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "linked-list-reverse/README.md",
"chars": 664,
"preview": "<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 n"
},
{
"path": "linked-list-reverse/linked-list-reverse.go",
"chars": 1887,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "linked-list-reverse/linked-list-reverse_test.go",
"chars": 1143,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "merge-sort/README.md",
"chars": 1917,
"preview": "<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.c"
},
{
"path": "merge-sort/merge-sort.go",
"chars": 1822,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "merge-sort/merge-sort_test.go",
"chars": 1021,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "quick-sort/README.md",
"chars": 1773,
"preview": "<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.c"
},
{
"path": "quick-sort/quick-sort.go",
"chars": 1491,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "quick-sort/quick-sort_test.go",
"chars": 1021,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "stack/stack.go",
"chars": 1568,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
},
{
"path": "stack/stack_test.go",
"chars": 1136,
"preview": "// ====================================================\n// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal\n// Th"
}
]
About this extraction
This page contains the full source code of the Dentrax/Data-Structures-with-Go GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 52 files (83.1 KB), approximately 26.5k tokens, and a symbol index with 170 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.