Skip to content

Instantly share code, notes, and snippets.

Sylvain Saurel ssaurel

View GitHub Profile
@ssaurel
ssaurel / TwentyQuestions.java
Created Apr 12, 2019
TwentyQuestions Game implementation on the SSaurel's Channel
View TwentyQuestions.java
package com.ssaurel.twentyquestions;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
@ssaurel
ssaurel / BuildUI.java
Created Apr 12, 2019
Build UI method for the Twenty Questions game on SSaurel's Channel
View BuildUI.java
private void buildUI() {
// we build the UI
JFrame frame = new JFrame("Twenty Questions on SSaurel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
// add buttons
JPanel buttonsPanel = new JPanel();
yesButton = new JButton("Yes");
@ssaurel
ssaurel / Tree.java
Created Apr 12, 2019
Tree implementation for the Twenty Questions game on the SSaurel's Channel
View Tree.java
package com.ssaurel.twentyquestions;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
// we represent the tree for Twenty Question game
public class Tree {
public TreeNode root; // root of our tree
@ssaurel
ssaurel / TreeNode.java
Created Apr 12, 2019
TreeNode implementation for the Twenty Questions tutorial on the SSaurel's Channel
View TreeNode.java
package com.ssaurel.twentyquestions;
// We will use a Binary Tree to represent the questions
// for guessing what user thinks about
// So we need to represent tree nodes
public class TreeNode {
enum Type {
ANSWER, QUESTION // nodes are answer or question
}
@ssaurel
ssaurel / Tree.java
Last active Apr 9, 2019
Tree Traversal Algorithms Implementations in Java on the SSaurel's Channel
View Tree.java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Tree {
// Root, Left, Right
public static < T > void preOrderTraverse(Node < T > node) {
if (node == null)
return;
@ssaurel
ssaurel / LevelOrderTraverse.java
Created Apr 9, 2019
Level Order Traversal Algorithm in Java for the SSaurel's Channel
View LevelOrderTraverse.java
// Level traversal (Breadth-first search)
public static <T> void levelOrderTraverse(Node<T> node) {
if (node == null)
return;
Queue<Node<T>> queue = new LinkedList<>();
// we add start node
queue.add(node);
// iterate while queue not empty
@ssaurel
ssaurel / IterativePostOrderTraverse.java
Created Apr 9, 2019
Iterative Post Order Traversal Algorithm on the SSaurel's Channel
View IterativePostOrderTraverse.java
public static <T> void iterativePostOrderTraverse(Node<T> node) {
if (node == null)
return;
// We create two stacks
Stack<Node<T>> nodeStack1 = new Stack<>();
Stack<Node<T>> nodeStack2 = new Stack<>();
// We push root to first stack
nodeStack1.push(node);
@ssaurel
ssaurel / IterativeInOrderTraverse.java
Created Apr 9, 2019
Iterative In Order Traversal in Java on the SSaurel's Channel
View IterativeInOrderTraverse.java
public static <T> void iterativeInOrderTraverse(Node<T> node) {
if (node == null)
return;
// We create an empty stack
Stack<Node<T>> nodeStack = new Stack<>();
Node<T> currentNode = node;
// We traverse the tree
while (currentNode != null || nodeStack.size() > 0) {
@ssaurel
ssaurel / PostOrderTraverse.java
Created Apr 9, 2019
Tree Post Order Traversal Implementation in Java for the SSaurel's Channel
View PostOrderTraverse.java
// Left, Right, Root
public static <T> void postOrderTraverse(Node<T> node) {
if (node == null)
return;
postOrderTraverse(node.left);
postOrderTraverse(node.right);
System.out.print(node.data + " ");
}
@ssaurel
ssaurel / InOrderTraverse.java
Created Apr 9, 2019
Tree In Order Traversal Algorithm on the SSaurel's Channel
View InOrderTraverse.java
// Left, Root, Right
public static <T> void inOrderTraverse(Node<T> node) {
if (node == null)
return;
inOrderTraverse(node.left);
System.out.print(node.data + " ");
inOrderTraverse(node.right);
}
You can’t perform that action at this time.