binary-tree

Delete operation in Array Binary Tree

痴心易碎 提交于 2019-12-11 02:56:51
问题 I know you guys are going to complain about having this question being asked again and again. Sorry, but I have not found my answer in Google/Stackoverflow/Forums... etc I am creating an Array Binary Tree (it's not a search one) in Java. 1) My node has the attributes: parent, left and right. Which are the number of the indexes of the parent, left child and right child. My professor told me to do it like this, I don't know why because you can find the indexes of the parent and children with a

ArrayList Based Binary Tree - Java

主宰稳场 提交于 2019-12-11 01:09:54
问题 I am currently in the process of implementing an ArrayList based binary tree in Java . I am trying to figure out how this would be done, but I am running into a wall. There are a bunch of methods in a class I am supposed to implement, but each time I try something, it doesn't seem to work. We have Position objects that are identified by Position<E> . In this class we have an array list that is private , and a root variable , both accessible only by this class , so the size() method , and the

C++ Binary Tree error: request for member (X) in (Y) which is of non-class type (Z)

南笙酒味 提交于 2019-12-11 00:33:42
问题 Hey all, so I am trying to build a simple binary tree that has two keys and evaluates the sum for its sorting. Here is what it's looking like: struct SumNode { int keyA; int keyB; SumNode *left; SumNode *right; }; class SumBTree { public: SumBTree(); ~SumBTree(); void insert(int, int); SumNode *search(int, int); SumNode *search(int); void destroy_tree(); private: SumNode *root; void insert(int,int, SumNode*); SumNode *search(int,int, SumNode*); SumNode *search(int, SumNode*); void destroy

How can i traverse a binary tree from right to left in java?

情到浓时终转凉″ 提交于 2019-12-10 23:33:17
问题 I want to traverse a binary tree from right to left and add to a queue every item with same last name. I have correctly implement a Queue List class and a Tree Node class but, i get a null pointer exception when i try to find something. (Of course i have written an insertion method for the binary tree). public class ST { private TreeNode root; private int size; private Queue q; public Queue searchByLastName(String last_name) { searchByLastNameRec(this.root, last_name); return q; } private

creating a new tree from the existing trees as left and right

大憨熊 提交于 2019-12-10 23:04:17
问题 My code is similar to the one given in this thread. template<class T> class BinarySearchTree { private: struct tree_node { tree_node* left; tree_node* right; T data; tree_node( const T & thedata, tree_node * l = NULL, tree_node * r = NULL ) : data( thedata ), left( l ), right( r ) { } }; tree_node* root; public: BinarySearchTree() { root = NULL; } } In my main program, there a need for this: I have two trees: BinarySearchTree<T> tree1; BinarySearchTree<T> tree2; I need to create a new tree

Enumerate search trees

会有一股神秘感。 提交于 2019-12-10 21:37:21
问题 According to this question the number of different search trees of a certain size is equal to a catalan number. Is it possible to enumerate those trees? That is, can someone implement the following two functions: Node* id2tree(int id); // return root of tree int tree2id(Node* root); // return id of tree (I ask because the binary code for the tree (se one of the answers to this question) would be a very efficient code for representing arbitrarily large integers of unknown range, i.e, a

Binary Tree Get Bottom extreme Left or Right

时光总嘲笑我的痴心妄想 提交于 2019-12-10 18:47:58
问题 I have a table that stores a binary tree as follows: Id ParentId Level Placement 47 -1 0 0 23 47 1 0 86 47 1 1 5 23 2 0 29 23 2 1 68 86 2 0 8 5 3 1 31 29 3 1 67 68 3 0 . . . Using MSSQL now, I need to get BottomLeft of a given id, in this example, for 47 its bottom left is 5 and I need to get the bottom right of a given id, in this example for 47 its bottom right is 86 Bottom left or right is not the lowest level left or right, but extreme outside left or right leaf. How do I write an sql

Right view of binary tree without using queue in Java

我是研究僧i 提交于 2019-12-10 18:38:27
问题 HERE is the c++ implementation for right view of binary tree without using queue. When I try to convert it to Java, it is not working. Here is my Java code: (I think most likely it is because I have not understood the algorithm properly and handling of maxLevel pointers/reference) public static void rightView(TreeNode tNode){ int maxLevel = 0; rViewUtil(tNode, 1,maxLevel); } public static void rViewUtil(TreeNode tNode, int level, int maxLevel){ if(tNode==null) return; if(maxLevel < level){

Is Binary Search tree balanced?

家住魔仙堡 提交于 2019-12-10 18:35:30
问题 This has already been discussed here, but I have an implementation below (which was never discussed in the thread), public boolean isBalanced(BSTNode node) { if(maxHeight() > (int)(Math.log(size())/Math.log(2)) + 1) return false; else return true; } where maxHeight() returns the maximum height of the tree. Basically I am checking if maxHeight > log(n), where n is the number of elements in the tree. Is this a correct solution? 回答1: This solution is not correct. A balanced tree is balanced if

Level-order traversal of a binary tree

谁都会走 提交于 2019-12-10 17:47:43
问题 I want to perform level-order traversal of a binary tree. Hence, for a given tree, say: 3 / \ 2 1 / \ \ 4 6 10 the output would be: 3 2 1 4 6 10 I understand that I could use some sort of queue, but what is the algorithm to do this in C recursively? Any help appreciated. 回答1: The graph algorithm is called Breadth First Search, it uses a queue to perform the level-order traversal, here is the pseudo-code void breadth_first(Node root) { Queue q; q.push(root); breadth_first_recursive(q) } void