infix : Your tree « Collections « Ruby

Ruby
1. ActiveRecord
2. Array
3. CGI
4. Class
5. Collections
6. Database
7. Date
8. Design Patterns
9. Development
10. File Directory
11. GUI
12. Hash
13. Language Basics
14. Method
15. Network
16. Number
17. Rails
18. Range
19. Reflection
20. Statement
21. String
22. Threads
23. Time
24. Tk
25. Unit Test
26. Windows Platform
27. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Ruby » Collections » Your tree 
infix

class Tree


  attr_accessor :left
  attr_accessor :right
  attr_accessor :data

  def initialize(x=nil)
    @left = nil
    @right = nil
    @data = x
  end

  def insert(x)
    list = []
    if @data == nil
      @data = x
    elsif @left == nil
      @left = Tree.new(x)
    elsif @right == nil
      @right = Tree.new(x)
    else
      list << @left
      list << @right
      loop do
        node = list.shift
        if node.left == nil
          node.insert(x)
          break
        else
          list << node.left
        end
        if node.right == nil
          node.insert(x)
          break
        else
          list << node.right
        end
      end
    end
  end

  def traverse()
    list = []
    yield @data
    list << @left if @left != nil
    list << @right if @right != nil
    loop do
      break if list.empty?
      node = list.shift
      yield node.data
      list << node.left if node.left != nil
      list << node.right if node.right != nil
    end
  end

  def insert(x)
    if @data == nil
      @data = x
    elsif x <= @data
      if @left == nil
        @left = Tree.new x
      else
        @left.insert x
      end
    else
      if @right == nil
        @right = Tree.new x
      else
        @right.insert x
      end
    end
  end

  def inorder()
    @left.inorder {|y| yield yif @left != nil
    yield @data
    @right.inorder {|y| yield yif @right != nil
  end

  def preorder()
    yield @data
    @left.preorder {|y| yield yif @left != nil
    @right.preorder {|y| yield yif @right != nil
  end

  def postorder()
    @left.postorder {|y| yield yif @left != nil
    @right.postorder {|y| yield yif @right != nil
    yield @data
  end

  def search(x)
    if self.data == x
      return self
    elsif x < self.data
      return left != nil ? left.search(x: nil
    else
      return right != nil ? right.search(x: nil
    end
  end


  def to_s
    "[" +
    if left then left.to_s + "," else "" end +
    data.inspect +
    if right then "," + right.to_s else "" end "]"
  end

  def to_a
    temp = []
    temp << left.to_a if left
    temp << data
    temp << right.to_a if right
    temp
  end

  def infix()
    if @left != nil
      flag = %w[* / + -].include? @left.data
      yield "(" if flag
      @left.infix {|y| yield y}
      yield ")" if flag
    end
    yield @data
    if @right != nil
      flag = %w[* / + -].include? @right.data
      yield "(" if flag
      @right.infix {|y| yield yif @right != nil
      yield ")" if flag
    end
  end

end

def addnode(nodes)
  node = nodes.shift
  tree = Tree.new node
  if %w[* / + -].include? node
    tree.left  = addnode nodes
    tree.right = addnode nodes
  end
  tree
end


prefix = %w* + 32 21 45 72 23 11 ]

tree = addnode prefix

str = ""
tree.infix {|x| str += x}
# str is now "(32+(21*45))*(72-(23+11))"

 
Related examples in the same category
1. Create Your tree
2. inorder / preorder / postorder
3. search a tree
4. to string
5. to array
6. Writing an Iterator Over a Data Structure
7. Loop through a tree
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.