Coder Social home page Coder Social logo

gotree's Introduction

gotree Build Status GoDoc Project Stats

gotree provides tree visualizing tools and algorithm implementations.

Getting Started

// to install, in the command line
mkdir $HOME/go
export GOPATH=$HOME/go
go get github.com/gyuho/gotree

// to include, in the code
import "github.com/gyuho/gotree"

// to call the function, in the code
[package_name].[function]

// to execute
go install
// or
go build

↑ top

Package Hierarchy

tree/     # Tree Data Structure
  
  bst/    # Binary Search Tree
  bstviz/ # Visualization (Graphviz)
  
  avl/    # AVL Tree
  avlviz/ # Visualization (Graphviz)


example/  # Example Code

↑ top

What is Tree? (YouTube Clips)

↑ top

Example : Binary Search Tree

tr := bst.NewTree(5)
for i := 0; i < 10; i++ {
	if i != 5 {
		tr = tr.Insert(int64(i))
	}
}
Show(tr, "tree1.dot")

tree01


tr := bst.NewTree(5)
tr.Inserts(7, 8, 5, 4, 2, 1, 6, 3)
Show(tr, "tree2.dot")

tree02


tr := bst.NewTree(5)
tr.Inserts(7, 8, 5, 4, 2, 1, 6, 3)
tr.Delete(int64(6))
Show(tr, "tree3.dot")

tree03


tr := bst.NewTree(5)
tr.Inserts(7, 8, 4, 2, 1, 3)
tr.Delete(int64(7))
Show(tr, "tree4.dot")

tree04


tr := bst.NewTree(5)
tr.Inserts(7, 8, 3, 4, 2, 1, 6)
tr = tr.Delete(int64(5))
Show(tr, "tree5.dot")

tree05

↑ top


AVL tree is a self-balancing binary search tree.

For lookup-intensive applications, AVL trees are faster than red-black trees because they are more rigidly balanced. Similar to red-black trees, AVL trees are height-balanced. Both are in general not weight-balanced

It is basically a Binary Search Tree (BST) with additional balancing property:

Height of the Left Sub-Tree and Height of the Right Sub-Tree differ by at most 1

Balance(Tree) = Height(Left) - Height(Right) = -1, 0, 1

For example,

  1
 / \
    2
   / \
  3   4

The node 2 is balanced, but the node 1 is NOT balanced because the Height(Left) is 0 and Height(Right) is 2

Insertion
  1. Insert into Left-Sub of Left-Child
  2. Insert into Right-Sub of Right-Child
  3. Insert into Left-Sub of Right-Child
  4. Insert into Right-Sub of Left-Child
Rotation for Re-balancing
  1. LL Rotation
  2. RR Rotation
  3. LR Rotation
  4. RL Rotation

↑ top


Rebalance (Rearrange)

LL Rotation

Unbalanced!

    4
   /
  3
 /
2

then

   3
  / \
 2   4

↑ top


RR Rotation

Unbalanced!

    6
     \
      7
       \
        8

then

   7
  / \
 6   8

↑ top


LR Rotation

Unbalanced!

    4
   /
  2
   \
    3

then

   3
  / \
 2   4

↑ top


RL Rotation

Unbalanced!

    6
     \
      8
     /
    7

then

   7
  / \
 6   8

↑ top


Determine which rotation to use
  • Height(Unbalanced-Node) is:
    • Positive: Left-Child (Example Height = 2)
      • If Height(Left-Child) is:
        • Positive: LL Rotation (Example Height = 1)
        • Negative: LR Rotation (Example Height = -1)
    • Negative: Right-Child (Example Height = 2)
      • If Height(Right-Child) is:
        • Positive: RL Rotation (Example Height = 1)
        • Negative: RR Rotation (Example Height = -1)

↑ top

Example : AVL Tree

func Test_avlviz(test *testing.T) {
  tr := avl.NewTree(4)
  tr.BalanceInsert(6)
  tr.BalanceInsert(5)
  avlviz.Show(tr, "avl-before.dot")

  tr.BalanceRL(5)
  avlviz.Show(tr, "avl-after.dot")
}

Before avl-before

After avl-after

↑ top


func Test_Show25(test *testing.T) {
  // Left Left Case
  tr1 := avl.NewTree(13)
  tr1.TreeInserts(5, 17, 3, 10, 4, 2)
  Show(tr1, "avl_balanced_25.dot")

  // Left Right Case
  tr2 := avl.NewTree(13)
  tr2.TreeInserts(5, 17, 3, 10, 12, 9)
  Show(tr2, "avl_balanced_26.dot")

  // Right Right Case
  tr3 := avl.NewTree(7)
  tr3.TreeInserts(4, 12, 8, 15, 17, 13)
  Show(tr3, "avl_balanced_27.dot")

  // Right Left Case
  tr4 := avl.NewTree(7)
  tr4.TreeInserts(4, 12, 9, 15, 8, 10)
  Show(tr4, "avl_balanced_28.dot")
}

avl_balanced_25

avl_balanced_26

avl_balanced_27

avl_balanced_28

↑ top

To-Do-List

Non-Committal on a Timeline

  • Tree Deletion
  • More Tree Data Structures

↑ top

Other

↑ top

gotree's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.