Graphs and Tress Representation

Trees

Parent Child Pointers

class Node:
    def __init__(self, value):
        self.val = value
        self.left = None
        self.right = None
type Node struct {
    Val int
    Left *Node
    Right *Node
}

func GetNode(val int) *Node {
    var node *Node
    node = new(Node)
    node.Val = val
    node.Left = nil
    node.Right = nil
    return node
}

Array Representation (Binary trees)

tree = [10,20,30,40,50,60]
tree := []int{10,20,30,40,50,60}

Adjacency List

tree = {
    'A': ['B','C']
    'B': ['D','E']
    'C': ['F']
}
tree = map[rune][]rune{
    'A': {'B','C'}
    'B': {'D','E'}
    'C': {'F'}
}

Adjacency set

tree = {
    'A': {'B','C'},
    'B': {'D'},
    'C': {}
}
tree map[rune]map[rune]interface{}{
	'A': {'B': struct{}{},'C':struct{}{}},
    'B': {'D':struct{}{}},
    'C': {}
}

Graphs

Adjacency List

graph = {
    'A':['B','C'],
    'B':['A','D'],
    'C':['A','D'],
    'D':['B','C']
}
graph := map[rune][]rune{
    'A':{'B','C'},
    'B':{'A','D'},
    'C':{'A','D'},
    'D':{'B','C'},
}

Adjacency set

graph = {
    'A': {'B','C'},
    'B': {'A','D'},
    'C': {'A'}
    'D': {'B'}
}
graph map[rune]map[rune]interface{}{
	'A': {'B': struct{}{},'C':struct{}{}},
    'B': {'A': struct{}{},'D':struct{}{}},
    'C': {'A':struct{}{}},
    'D': {'B':struct{}{}},
}

Adjacency matrix

matrix = [
    [0,1,1,0],
    [1,0,0,1],
    [1,0,0,1],
    [0,1,1,0]
]
matrix = [][]int{
    {0,1,1,0},
    {1,0,0,1},
    {1,0,0,1},
    {0,1,1,0}
}

Edge List

edges = [
    ('A','B'),
    ('A','C'),
    ('B','D'),
    ('C','D'),
]
edges := [][]rune{
    {'A','B'},
    {'A','C'},
    {'B','D'},
    {'C','D'},
}

Hash Maps of Hash Maps (Weighted Graphs)

graph = {
    'A': {'B': 3, 'C': 1},
    'B': {'C': 7},
    'C': {}
}
graph := map[rune]map[rune]interface{}{
	'A': {'B': struct{}{},'C':struct{}{}},
    'B': {'A': struct{}{},'D':struct{}{}},
    'C': {'A':struct{}{}},
}