Archive

Tag Archives: nicholas

1. Open a terminal window.

2. At the input prompt you will see this structure:
“`
nicholas@computer-name:~$ _
“`

3. So you have to edit the hostname file:
“`
sudo nano /etc/hostname
“`

4. When prompted, enter the administrator password and hit Enter.

5. The hostname file will open, showing the current computer name. Replace the name with the desired new name.

6. Hit Ctrl+X to save and exit.

7. New name will show when you open a new terminal window.

Unable to install Homebrew with this?

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

What’s happened

-e:70: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
Password:
==> Downloading and installing Homebrew...
remote: Counting objects: 192274, done.
remote: Compressing objects: 100% (52298/52298), done.
remote: Total 192274 (delta 138823), reused 192246 (delta 138803)
Receiving objects: 100% (192274/192274), 38.59 MiB | 189 KiB/s, done.
Resolving deltas: 100% (138823/138823), done.
From https://github.com/Homebrew/homebrew
 * [new branch]      master     -> origin/master
error: unable to unlink old 'Library/Aliases/0mq' (Permission denied)
error: unable to unlink old 'Library/Aliases/4store' (Permission denied)
error: unable to unlink old 'Library/Aliases/Secret Rabbit Code' (Permission denied)
error: unable to unlink old 'Library/Aliases/ag' (Permission denied)
error: unable to unlink old 'Library/Aliases/alut' (Permission denied)
error: unable to unlink old 'Library/Aliases/android' (Permission denied)
error: unable to unlink old 'Library/Aliases/apache-activemq' (Permission denied)
error: unable to unlink old 'Library/Aliases/apache-fop' (Permission denied)
error: unable to unlink old 'Library/Aliases/aws-as' (Permission denied)
error: unable to unlink old 'Library/Aliases/aws-mon' (Permission denied)
error: unable to unlink old 'Library/Aliases/beanstalkd' (Permission denied)
error: unable to unlink old 'Library/Aliases/bjam' (Permission denied)
error: unable to unlink old 'Library/Aliases/boehmgc' (Permission denied)
error: unable to unlink old 'Library/Aliases/boost-jam' (Permission denied)
error: unable to create symlink Library/Aliases/boot2docker-cli (Permission denied)
error: unable to unlink old 'Library/Aliases/bzr' (Permission denied)
error: unable to unlink old 'Library/Aliases/cowthink' (Permission denied)
error: unable to unlink old 'Library/Aliases/cpanm' (Permission denied)
error: unable to unlink old 'Library/Aliases/csvfix' (Permission denied)
error: unable to unlink old 'Library/Aliases/ctags-exuberant' (Permission denied)
error: unable to unlink old 'Library/Aliases/db' (Permission denied)
error: unable to unlink old 'Library/Aliases/dbus' (Permission denied)
error: unable to unlink old 'Library/Aliases/dejagnu' (Permission denied)
error: unable to unlink old 'Library/Aliases/eyeD3' (Permission denied)
error: unable to unlink old 'Library/Aliases/fastcgi' (Permission denied)
error: unable to unlink old 'Library/Aliases/fishfish' (Permission denied)
error: unable to unlink old 'Library/Aliases/fluidsynth' (Permission denied)
error: unable to unlink old 'Library/Aliases/gearmand' (Permission denied)
error: unable to unlink old 'Library/Aliases/git-tig' (Permission denied)
error: unable to unlink old 'Library/Aliases/gnu-scientific-library' (Permission denied)
error: unable to unlink old 'Library/Aliases/google-go' (Permission denied)
error: unable to unlink old 'Library/Aliases/gperftools' (Permission denied)
error: unable to unlink old 'Library/Aliases/gpg' (Permission denied)
error: unable to unlink old 'Library/Aliases/gpg2' (Permission denied)
error: unable to unlink old 'Library/Aliases/gs' (Permission denied)
error: unable to unlink old 'Library/Aliases/gtk' (Permission denied)
error: unable to unlink old 'Library/Aliases/gtypist' (Permission denied)
error: unable to unlink old 'Library/Aliases/hashdeep' (Permission denied)
error: unable to unlink old 'Library/Aliases/heroku' (Permission denied)
error: unable to unlink old 'Library/Aliases/hg' (Permission denied)
error: unable to unlink old 'Library/Aliases/htop' (Permission denied)
error: unable to unlink old 'Library/Aliases/hudson' (Permission denied)
error: unable to unlink old 'Library/Aliases/ipsum' (Permission denied)
error: unable to unlink old 'Library/Aliases/jocr' (Permission denied)
error: unable to unlink old 'Library/Aliases/lcms' (Permission denied)
error: unable to unlink old 'Library/Aliases/lcms2' (Permission denied)
error: unable to unlink old 'Library/Aliases/leg' (Permission denied)
error: unable to unlink old 'Library/Aliases/libcryptopp' (Permission denied)
error: unable to unlink old 'Library/Aliases/libgd' (Permission denied)
error: unable to unlink old 'Library/Aliases/libgeoip' (Permission denied)
error: unable to unlink old 'Library/Aliases/libjpeg' (Permission denied)
error: unable to unlink old 'Library/Aliases/libjpeg-turbo' (Permission denied)
error: unable to unlink old 'Library/Aliases/libjpg' (Permission denied)
error: unable to unlink old 'Library/Aliases/liblabjackusb' (Permission denied)
error: unable to unlink old 'Library/Aliases/libmad' (Permission denied)
error: unable to unlink old 'Library/Aliases/libmcrypt' (Permission denied)
error: unable to unlink old 'Library/Aliases/libnettle' (Permission denied)
error: unable to unlink old 'Library/Aliases/liboggz' (Permission denied)
error: unable to unlink old 'Library/Aliases/libqrencode' (Permission denied)
error: unable to unlink old 'Library/Aliases/libtag' (Permission denied)
error: unable to unlink old 'Library/Aliases/libtasn' (Permission denied)
error: unable to unlink old 'Library/Aliases/libtcnative' (Permission denied)
error: unable to unlink old 'Library/Aliases/littlecms' (Permission denied)
error: unable to unlink old 'Library/Aliases/mc' (Permission denied)
error: unable to unlink old 'Library/Aliases/mediainfo' (Permission denied)
error: unable to unlink old 'Library/Aliases/mongo' (Permission denied)
error: unable to unlink old 'Library/Aliases/mosh' (Permission denied)
error: unable to unlink old 'Library/Aliases/mp4box' (Permission denied)
error: unable to unlink old 'Library/Aliases/mpich' (Permission denied)
error: unable to unlink old 'Library/Aliases/myrepos' (Permission denied)
error: unable to unlink old 'Library/Aliases/node.js' (Permission denied)
error: unable to unlink old 'Library/Aliases/nodejs' (Permission denied)
error: unable to unlink old 'Library/Aliases/npm' (Permission denied)
error: unable to unlink old 'Library/Aliases/nsis' (Permission denied)
error: unable to unlink old 'Library/Aliases/o-caml' (Permission denied)
error: unable to unlink old 'Library/Aliases/ocaml' (Permission denied)
error: unable to unlink old 'Library/Aliases/ocio' (Permission denied)
error: unable to unlink old 'Library/Aliases/offlineimap' (Permission denied)
error: unable to unlink old 'Library/Aliases/ooc' (Permission denied)
error: unable to unlink old 'Library/Aliases/openmpi' (Permission denied)
error: unable to unlink old 'Library/Aliases/openocd' (Permission denied)
error: unable to unlink old 'Library/Aliases/pgrep' (Permission denied)
error: unable to unlink old 'Library/Aliases/pipeviewer' (Permission denied)
error: unable to unlink old 'Library/Aliases/pkgconfig' (Permission denied)
error: unable to unlink old 'Library/Aliases/pkill' (Permission denied)
error: unable to unlink old 'Library/Aliases/pocketsphinx' (Permission denied)
error: unable to unlink old 'Library/Aliases/postgres' (Permission denied)
error: unable to create symlink Library/Aliases/pt (Permission denied)
error: unable to unlink old 'Library/Aliases/qt4' (Permission denied)
error: unable to unlink old 'Library/Aliases/shell-fm' (Permission denied)
error: unable to unlink old 'Library/Aliases/slang' (Permission denied)
error: unable to unlink old 'Library/Aliases/sphinxbase' (Permission denied)
error: unable to unlink old 'Library/Aliases/sqlite3' (Permission denied)
error: unable to unlink old 'Library/Aliases/stax-sdk' (Permission denied)
error: unable to unlink old 'Library/Aliases/style' (Permission denied)
error: unable to unlink old 'Library/Aliases/svn' (Permission denied)
error: unable to unlink old 'Library/Aliases/tinyfugue' (Permission denied)
error: unable to create symlink Library/Aliases/twemproxy (Permission denied)
error: unable to unlink old 'Library/Aliases/twolame' (Permission denied)
error: unable to unlink old 'Library/Aliases/unix2dos' (Permission denied)
error: unable to unlink old 'Library/Aliases/urxvt' (Permission denied)
error: unable to unlink old 'Library/Aliases/usb-multiplex-daemon' (Permission denied)
error: unable to create symlink Library/Aliases/vid.stab (Permission denied)
error: unable to unlink old 'Library/Aliases/wxwidgets' (Permission denied)
error: unable to unlink old 'Library/Aliases/xmlsec1' (Permission denied)
error: unable to unlink old 'Library/Aliases/zmq' (Permission denied)
error: unable to unlink old 'Library/Contributions/brew_bash_completion.sh' (Permission denied)
error: unable to unlink old 'Library/Contributions/brew_fish_completion.fish' (Permission denied)
error: unable to unlink old 'Library/Contributions/brew_zsh_completion.zsh' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-aspell-dictionaries.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-beer.rb' (Permission denied)
error: unable to create file Library/Contributions/cmd/brew-bundle-dir.rb (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-bundle.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-cleanup-installed' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-gist-logs.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-graph' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-man' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-profile.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-pull.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-server' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-services.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-switch.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-tap-readme.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-test-bot.rb' (Permission denied)
error: unable to create file Library/Contributions/cmd/brew-versions.rb (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-which.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/git' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/public/bootstrap.min.css' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/public/glyphicons-halflings-white.png' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/public/glyphicons-halflings.png' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/svn' (Permission denied)
error: unable to unlink old 'Library/Contributions/example-formula.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/manpages/brew.1.md' (Permission denied)
error: unable to unlink old 'Library/ENV/3.2.6' (Permission denied)
error: unable to unlink old 'Library/ENV/4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/ant' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/apr-1-config' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/bsdmake' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/c++' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/c89' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/c99' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/cc' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/clang' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/clang++' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/cpp' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.3' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.4' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.5' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.6' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.7' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.8' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.9' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.3' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.4' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.5' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.6' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.7' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.8' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.9' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/git' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gmake' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/i686-apple-darwin11-llvm-g++-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/i686-apple-darwin11-llvm-gcc-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/ld' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/llvm-g++' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/llvm-g++-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/llvm-gcc' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/llvm-gcc-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/make' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/mig' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/pod2man' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/sed' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/svn' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/xcrun' (Permission denied)
fatal: cannot create directory at 'Library/ENV/pkgconfig/10.10': Permission denied
Failed during: git reset --hard origin/master

Reason

Probably you have multiple users on your computer. And you uninstalled the old Homebrew and trying to re-install it.

Solution

Theorotically you can remove the directory /usr/local/Library but this will happens again when you try to use Homebrew with another user. It is better to change the permission of directory with this:

sudo chmod -R 775 /usr/local/Library

and after a successful install, you better fix the permission for another directory. This directory may appear after you run some ruby commands.

sudo chmod -R 775 /usr/local/Cellar

 

Question

Design an algorithm to verify that a tree is a universal value binary tree. Universal value binary tree means all value in that tree is the same.

Solution

There is two approach for this problem. One is with recursive function and another is with iterative function. For this problem, iterative function makes simpler answer. However, we have to learn using recursive function because in production code recursive function uses memory more efficiently while compared to iterative function.

Sample

# node structure
class Node:
    value = None
    left = None
    right = None

    # constructor
    def __init__(self, value, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right

# iterative way to solve this problem
def iteratively_verify_universal_value_binary_tree(root):
    stack = [root]
    while len(stack) > 0:
        node = stack.pop()
        if node.value != root.value: return False
        if node.left: stack.append(node.left)
        if node.right: stack.append(node.right)
    return True

# recursive method to solve this problem
def recursively_verify_universal_value_binary_tree(node, root_value=None):
    if not node: return True
    if not root_value: root_value = node.value  # set root value to compared with
    left_is_universal = recursively_verify_universal_value_binary_tree(node.left, root_value)  # get left result
    right_is_universal = recursively_verify_universal_value_binary_tree(node.right, root_value)  # get right result
    this_is_universal = node.value == root_value  # get this result
    return left_is_universal and right_is_universal and this_is_universal  # combine result

# trees
universal_value_tree = Node(1, Node(1, Node(1), Node(1)), Node(1, Node(1), Node(1)))
non_universal_value_tree = Node(1, Node(1, Node(1), Node(2)), Node(1, Node(1), Node(1)))

# testing
print iteratively_verify_universal_value_binary_tree(universal_value_tree)  # true
print iteratively_verify_universal_value_binary_tree(non_universal_value_tree)  # false
print recursively_verify_universal_value_binary_tree(universal_value_tree)  # true
print recursively_verify_universal_value_binary_tree(non_universal_value_tree)  # false

 

Question

This is an actual question I encountered in an Amazon phone interview in November 2013. You are going to design the money changing algorithm for a vending machine. That is, after any purchase, the machine makes change to users with a combination of coins. And the machine only have 3 types of coins: nickel (5 cents), dime (10 cents) and quarter (25 cents). Coins with higher values are preferred in the change.

Solution

Well this is actually a simple question, I could just fill the sum from the highest value coin to the lowest. However I chose to use a simplified version of knapsack algorithm. Whatever, they both works.

Sample

# coin values
coins = [25, 10, 5]

# simple solution for this problem
def simple_solution(sum):
    combination = []
    for coin in coins:
        for i in xrange(sum / coin): combination.append(coin)
        sum %= coin
    return combination

# result: [25, 25, 10, 5]
print simple_solution(65)

 

it’s been a while since the last post. After coming to new york, I have been very busy on my study, personal projects and (of course) playing around. studying in new york university is harsh, but not as hard as i think. I met nice people, hanged out with my beloved new friends. I even found people having great interests in technologies and software development.

I am trying to work on instamusic as long as i have time. I also shared my project of javascript injector with my friend, and hoping to expand service and functions that it can have. I am grateful to see that there are still many people using my applications and sending emails to me. thank you very much and i will try my best to make something new.

with love, a photo in central park is nice.

peace.

DSC053452

Question

Define a stack structure with “min” function — a function to get the minimum value within the stack. The time complexity of min, push and pop functions must be O(1).

Solution

結合鍊錶一起做。首先我做插入以下數字: 10, 7, 3, 3, 8, 5, 2, 6

0: 10 -> NULL (MIN=10, POS=0)
1: 7  -> [0]  (MIN=7,  POS=1) // 用數組表示堆棧,第0個元素表示棧底
2: 3  -> [1]  (MIN=3,  POS=2)
3: 3  -> [2]  (MIN=3,  POS=3)
4: 8  -> NULL (MIN=3,  POS=3) // 技巧在這裡,因為8比當前的MIN大,所以彈出8不會影響當前的MIN
5: 5  -> NULL (MIN=3,  POS=3)
6: 2  -> [2]  (MIN=2,  POS=6) // 如果2出棧了,那麼3就是MIN
7: 6  -> [6]

出棧的話採用類似方法修正。

所以,藉助輔助棧,保存最小值,且隨時更新輔助棧中的元素。push第一個元素進A,也把它push進B,當向Apush的元素比B中的元素小, 則也push進B,即更新B。否則,不動B,保存原值。向棧A push元素時,順序由下至上。輔助棧B中,始終保存著最小的元素。

然後,當pop A中的元素小於B中棧頂元素時,則也要pop B中棧頂元素。

Samples

The following is an optimized implementation.  Repeated minimum value will not be stored through comparing values while pushing or popping values.

#include <vector>
#include <cassert>

using namespace std;

/**
 * Class defining a new type of stack supporting any datatype.
 */
template <typename T>
class StackWithMin
{
private:
    vector<T> dataStack; // stacks to store data
    vector<size_t> minStack; // stack to store position of minimum value

public:

    /**
     * Push data to the stack at the same time update the minimum stack if the
     * new data is smaller then the current minimum value. Throw exception if
     *  stack is empty.
     */
    void push(T data) {
        dataStack.push_back(data);
        if (minStack.empty() || data < dataStack[minStack.back()])
            minStack.push_back(dataStack.size()-1);
    }

    /**
     * Pop data to the stack at the same time pop from the min stack if it is
     * popping the minimum value. Throw exception if stack is empty.
     */
    void pop() {
        assert(!dataStack.empty());
        if (dataStack.back() == dataStack[minStack.back()])
            minStack.pop_back();
        dataStack.pop_back();
    }

    /**
     * return the current minimum value.
     */
    T min() {
        assert(!dataStack.empty() && !minStack.empty());
        return dataStack[minStack.back()];
    }
};

/**
 * Main program
 */
int main()
{
    StackWithMin<int> stack = StackWithMin<int>();
    stack.push(10);
    stack.push(7);
    stack.push(3);
    stack.push(3);
    stack.push(8);
    stack.push(5);
    stack.push(2);
    stack.push(6);
    printf("Minimum value: %dn", stack.min());
}
import java.util.Stack;

public class Pactice02 {

    public static void main(String[] args) {
        System.out.println("Hello World!");
        AdvancedStack stack = new AdvancedStack();
        stack.push(10);
        stack.push(7);
        stack.push(3);
        stack.push(3);
        stack.push(8);
        stack.push(5);
        stack.push(2);
        stack.push(6);
        System.out.println("Minimum value: "+ stack.getMinimum());
        stack.pop();
        System.out.println("Minimum value: "+ stack.getMinimum());
        stack.pop();
        System.out.println("Minimum value: "+ stack.getMinimum());
        stack.pop();
        System.out.println("Minimum value: "+ stack.getMinimum());
    }

    public static class AdvancedStack extends Stack {

        private Stack mMinimumStack = new Stack();

        @Override
        public E push(E item) {
            if (mMinimumStack.empty() || item.doubleValue()  0) {
                return mMinimumStack.peek();
            }
            return null;
        }
    }
}

Questiontree

Convert binary search tree into doubly linked list. It’s required not to create any new node, but only turning pointers.

Solution

The following shows the concept of this question.

        8
      /   
    6       0       ->     5 = 6 = 7 = 8 = 9 = 0 = 1
   /      / 
  5   7   9    1

First, since node for both binary tree and doubly linked list have left and right node, they can use the same node structure. Then, looking the doubly linked list as a normal linked list, it is actually a in-order depth-first traversal result.

Sample Codes

 

# node structure
class Node:
    value = None
    left = None
    right = None

    # constructor
    def __init__(self, value, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right

# what to do with a node.
def visit(node):
    global node_temp
    global node_root
    if not node_temp: node_root = node  # head
    else: node_temp.right = node  # make right of temp to this
    node.left = node_temp  # make temp to this left
    node_temp = node  # next node

# bfs traversal
def depth_first_recursive_traversal_in_order(node):
    if node is None: return
    depth_first_recursive_traversal_in_order(node.left)
    visit(node)
    depth_first_recursive_traversal_in_order(node.right)

# a binary tree and display how was it before reflection
node_5 = Node(5)
node_7 = Node(7)
node_9 = Node(9)
node_1 = Node(1)
node_6 = Node(6, node_5, node_7)
node_0 = Node(0, node_9, node_1)
node_8 = Node(8, node_6, node_0)

# conversion
node_root = node_8
node_temp = None
depth_first_recursive_traversal_in_order(node_root)

# answer should be: 5 6 7 8 9 0 1
node_temp = node_root
while node_temp:
    print node_temp.value,
    node_temp = node_temp.right
public class InterviewPractice1
{
    // global
    static Node node_root = null;
    static Node node_temp = null;

    // node structure
    private static class Node
    {
        private int value;
        private Node left;
        private Node right;

        public Node(int value, Node left, Node right)
        {
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    // visit
    private static void visit(Node node)
    {
        if (node_temp == null) node_root = node; // set head
        else node_temp.right = node; // make right of temp to this node
        node.left = node_temp; // make temp to the left of this node
        node_temp = node; // next node

    }

    // depth first, recursive
    private static void depth_first_recursive_traversal_in_order(Node node)
    {
        if (node == null) return;
        depth_first_recursive_traversal_in_order(node.left);
        visit(node);
        depth_first_recursive_traversal_in_order(node.right);
    }

    // main
    public static void main(String[] args)
    {
        // tree
        Node node_5 = new Node(5, null, null);
        Node node_7 = new Node(7, null, null);
        Node node_9 = new Node(9, null, null);
        Node node_1 = new Node(1, null, null);
        Node node_6 = new Node(6, node_5, node_7);
        Node node_0 = new Node(0, node_9, node_1);
        Node node_8 = new Node(8, node_6, node_0);

        // run
        node_root = node_8;
        depth_first_recursive_traversal_in_order(node_root);

        // the result should be 5 6 7 8 9 0 1
        node_temp = node_root;
        while (node_temp != null) {
            System.out.print(node_temp.value + " ");
            node_temp = node_temp.right;
        }
    }
}
#include <iostream>

struct Node
{
    int value;
    Node *left;
    Node *right;

    Node(int value, Node *left = NULL, Node *right = NULL)
    {
        this->value = value;
        this->left = left;
        this->right = right;
    }
};

Node *node_root = NULL;
Node *node_temp = NULL;

void visit(Node *node)
{
    if (node_temp == NULL) node_root = node;  // set head
    else node_temp->right = node;  // make right of them to this node
    node->left = node_temp;  // make temp to the left of this node
    node_temp = node;  // next node
}

void depth_first_recursive_traversal_in_order(Node *node)
{
    if (!node) return;
    depth_first_recursive_traversal_in_order(node->left);
    visit(node);
    depth_first_recursive_traversal_in_order(node->right);
}

int main()
{
    // tree
    Node *node_5 = new Node(5);
    Node *node_7 = new Node(7);
    Node *node_9 = new Node(9);
    Node *node_1 = new Node(1);
    Node *node_6 = new Node(6, node_5, node_7);
    Node *node_0 = new Node(0, node_9, node_1);
    Node *node_8 = new Node(8, node_6, node_0);

    // conversion
    node_root = node_8;
    node_temp = NULL; // null
    depth_first_recursive_traversal_in_order(node_8);

    // answer should be: 5 6 7 8 9 0 1
    node_temp = node_root;
    while (node_temp) {
        printf("%d ", node_temp->value);
        node_temp = node_temp->right;
    }
}
%d bloggers like this: