This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
A classic node class for use in a tree | |
''' | |
class Node: | |
def __init__(self,data): | |
self.left = None | |
self.right = None | |
self.data = data | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Discovers if a 'key' is present in the binary tree | |
''' | |
def find(self, root, key): | |
if root is None or root.data is key: | |
return root | |
elif key < root.data: | |
return self.find(root.left, key) | |
else: | |
return self.find(root.right, key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Pre Order Traversal of the tree | |
''' | |
def PreOrder(self, root): | |
if root is None: | |
pass | |
else: | |
print(root.data), | |
self.PreOrder(root.left) | |
self.PreOrder(root.right) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Post Order Traversal | |
''' | |
def PostOrder(self, root): | |
if root is None: | |
pass | |
else: | |
self.PostOrder(root.left) | |
self.PostOrder(root.right) | |
print(root.data), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
In Order Traversal of the tree | |
''' | |
def InOrder(self, root): | |
if root is None: | |
pass | |
else: | |
self.InOrder(root.left) | |
print(root.data), | |
self.InOrder(root.right) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Level Order Traversal | |
''' | |
def LevelOrder(self, root): | |
if root is None: | |
pass | |
else: | |
q = Queue.Queue() | |
currLevelCount = 1 | |
nextLevelCount = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
void printArray(int * array, int size){ | |
int curr; | |
for(curr = 0; curr < size; curr++){ | |
printf("%d, ", array[curr]); | |
} | |
printf("\n"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
img = imread(imgFile); | |
colorTransform = makecform('srgb2lab'); | |
img = double(applycform(img, colorTransform)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Runs MergeSort algorithm on a slice single | |
func MergeSort(slice []int) []int { | |
if len(slice) < 2 { | |
return slice | |
} | |
mid := (len(slice)) / 2 | |
return Merge(MergeSort(slice[:mid]), MergeSort(slice[mid:])) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Merges left and right slice into newly created slice | |
func Merge(left, right []int) []int { | |
size, i, j := len(left)+len(right), 0, 0 | |
slice := make([]int, size, size) | |
for k := 0; k < size; k++ { | |
if i > len(left)-1 && j <= len(right)-1 { | |
slice[k] = right[j] | |
j++ |