binary-tree

Implementing Binary Tree in Ruby

非 Y 不嫁゛ 提交于 2019-12-05 11:54:19
I've been trying to implement BinaryTree class in Ruby, but I'm getting the stack level too deep error, although I don't seem to be using any recursion in that particular piece of code: 1. class BinaryTree 2. include Enumerable 3. 4. attr_accessor :value 5. 6. def initialize( value = nil ) 7. @value = value 8. @left = BinaryTree.new # stack level too deep here 9. @right = BinaryTree.new # and here 10. end 11. 12. def empty? 13. ( self.value == nil ) ? true : false 14. end 15. 16. def <<( value ) 17. return self.value = value if self.empty? 18. 19. test = self.value <=> value 20. case test 21.

JAVA: binary trees

别等时光非礼了梦想. 提交于 2019-12-05 10:09:18
Here I am trying to practice making binary trees so that I can do different operations with them. import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { } } //Building Binary Trees class bTree { static class Node { //remember to initilize a root String value; Node left, right; Node(String value, Node left, Node right) { this.value = value; this.left = left; this.right = right; } Node(String value) //THIS IS A SIBLING CONSTRUCTOR { this(value, null, null); } Node root = new Node("ROOT"); Node lefty = new Node("LEFT0"); Node righty = new Node("RIGHT0

How can I calculate the level of a node in a perfect binary tree from its depth-first order index?

梦想与她 提交于 2019-12-05 09:15:33
I have a perfect binary tree, i.e. each node in the tree is either a leaf node, or has two children, and all leaf nodes are on the same level. Each node has an index in depth-first order. (E.g. in a tree with 3 levels the root node has index 0, the first child has 1, the first child of the first child has 2, the second child of the first child has 3, the second child has 4, the first child of the second child has 5, the second child of the second child has index 6. 0 / \ 1 4 / \ / \ 2 3 5 6 ) I know the size of tree (number of nodes/maximum level), but only the index of a particular node, and

Find a loop in a binary tree

时光毁灭记忆、已成空白 提交于 2019-12-05 07:02:29
How to find a loop in a binary tree? I am looking for a solution other than marking the visited nodes as visited or doing a address hashing. Any ideas? As mentioned already: A tree does not (by definition) contain cycles (loops). To test if your directed graph contains cycles (references to nodes already added to the tree) you can iterate trough the tree and add each node to a visited-list (or the hash of it if you rather prefer) and check each new node if it is in the list. Plenty of algorithms for cycle-detection in graphs are just a google-search away. Suppose you have a binary tree but you

Converting an infix expression (with parentheses) into a binary tree

拜拜、爱过 提交于 2019-12-05 06:25:00
As part of a Java assignment, I have to take an input arithmetic expression and store it in a binary tree. I have done everything necessary for the assignment except for the part where I read in the string of the expression and store it in the binary tree. I have created a class called BinaryTree. Its only field is a treenode called root. This treenode is defined as an innerclass in BinaryTree. It has 3 fields, a generic data field, and two children (left and right) that are type BinaryTree. I'm having a very difficult time defining an algorithm for reading in an expression such as (5*(2+3)^3)

Binary Tree insertion (in order sorted)

爷,独闯天下 提交于 2019-12-05 05:44:53
问题 I've scoured the internet for help on this problem but I need help. This isn't exactly an ordinary insertion problem for a binary tree, since we don't get to work directly with the tree structure itself. My prof wrote that himself and has given us functions we can use to write functions pertaining to binary trees. As such, I can't use nodes and pointers and things. Also this is in C++. Anyway, so here is the description of the recursive function I have to write ( along with my starting

Print a tree in sorted order using heap properties (Cormen)

烂漫一生 提交于 2019-12-05 05:13:47
I am refreshing on algorithm theory (from Cormen). There is an exercise in the chapter for binary tries that asks: Can the min-heap property be used to print out the keys of an n-node tree in sorted order in O(n) time? Show how, or explain why not. I thought yes it is possible. In the min heap the element in a node is smaller than both its children. So the root of the heap is always the smaller element of all the n elements and the left child of the root is the smaller than all the elements in the left subtree and the right child of the root is the smaller than all the elements in the right

Is there an existing solution for these particular multithreaded data structure requirements?

为君一笑 提交于 2019-12-05 02:16:52
I've had the need for a multi-threaded data structure that supports these claims: Allows multiple concurrent readers and writers Is sorted Is easy to reason about Fulfilling multiple readers and one writer is a lot easier, but I really would wan't to allow multiple writers. I've been doing research into this area, and I'm aware of ConcurrentSkipList (by Lea based on work by Fraser and Harris) as it's implemented in Java SE 6. I've also implemented my own version of a concurrent Skip List based on A Provably Correct Scalable Concurrent Skip List by Herlihy, Lev, Luchangco and Shavit. These two

BST with duplicates

百般思念 提交于 2019-12-04 21:47:38
问题 I know that, BST does not allow duplicates. For example, if I have a word "RABSAB". The Binary search tree for the above string is: R /\ A S \ B What if we wanted to include the duplicates in the tree. How the tree gonna change? I was asked this question in an interview. They asked me to draw: a binary tree an unbalanced Binary Search Tree a binary search tree without duplicates a binary search tree with duplicates Any Help is appreciated! PS: Help me by drawing the related trees 回答1: Rule to

Binary Trees Count Number of Leaves

拜拜、爱过 提交于 2019-12-04 21:47:05
Suppose you already have the basic binary tree procedures isempty(bt), root(bt), left(bt), and right(bt). Write a procedure isLeaf(bt) that returns true if the binary tree bt is a leaf node and false if it is not. This is what I have: proc isLeaf(bt) if (isEmpty(bt)) error('The binary tree is empty.'); elseif (left(bt) < right(bt)) return true; else return false; Then write a procedure numLeaves(bt) that returns the number of leaves in the binary tree bt. This is what I have: proc numLeaves(bt) if (isEmpty(bt)) error ('The binary tree is empty.'); elseif (count left(bt) + right(bt)); return