binary-search-tree

Balanced Binary Search Tree

こ雲淡風輕ζ 提交于 2019-12-01 14:40:34
I need to build a balanced binary search tree. So far my program inserts the numbers from 1 to 26, but my program does not build it into a balanced binary search tree. If anyone could look at my code and help me out it would be much appreciated. public class TreeNode { TreeNode leftTreeNode, rightTreeNode;// the nodes int data; //int size; public TreeNode(){//Constructer leftTreeNode = null; rightTreeNode = null; } public TreeNode(int newData){//Constructer with new Data coming in for comparison leftTreeNode = null; rightTreeNode = null; data = newData; } public TreeNode getLeft(){ return

Binary Search Tree Inorder Traversal

天大地大妈咪最大 提交于 2019-12-01 14:31:26
I am confused by this code: void in_order_traversal_iterative(BinaryTree *root) { stack<BinaryTree*> s; BinaryTree *current = root; while (!s.empty() || current) { if (current) { s.push(current); current = current->left; } else { current = s.top(); s.pop(); cout << current->data << " "; current = current->right; } } } We set a pointer to point to root. Then if it exists, then push the current (which is root currently) into the stack. I do not see why we push the whole tree into the stack initially, instead of just the value of the data the node holds. Am I missing something completely or not

Deletion in binary search tree

最后都变了- 提交于 2019-12-01 13:10:30
问题 So when I delete in binary search tree, do I need to have like 7 different cases i.e. Left Leaf; Right Leaf; Left child with only left child. //i.e the node to be deleted is the left child of it's parent and it has only left child. Left Child with only right child. Right child with only left child. Right child with only right child. Node to be deleted has both the children i.e. right and left. Now when this code is using if-else it gets pretty nasty.. is there any other way of doing this.

Binary Search Tree Inorder Traversal

倖福魔咒の 提交于 2019-12-01 12:11:31
问题 I am confused by this code: void in_order_traversal_iterative(BinaryTree *root) { stack<BinaryTree*> s; BinaryTree *current = root; while (!s.empty() || current) { if (current) { s.push(current); current = current->left; } else { current = s.top(); s.pop(); cout << current->data << " "; current = current->right; } } } We set a pointer to point to root. Then if it exists, then push the current (which is root currently) into the stack. I do not see why we push the whole tree into the stack

search in a binary tree

烂漫一生 提交于 2019-12-01 11:05:38
I have written the following function to search for a value in a binary tree storing integer values (the function is part of a larger program): bool tree::search(int num) //the function belongs to class 'tree' { node *temp=head; //'head' is pointer to root node while(temp!=NULL) { if(temp->data==num) break; if(num>temp->data) temp=temp->right; if(num<temp->data) temp=temp->left; } if(temp==NULL) return false; else if(temp->data==num) return true; } The problem is: when I search for a value present in the tree, it runs fine. But if I search for a value not present in the tree, the program just

search in a binary tree

喜夏-厌秋 提交于 2019-12-01 09:22:48
问题 I have written the following function to search for a value in a binary tree storing integer values (the function is part of a larger program): bool tree::search(int num) //the function belongs to class 'tree' { node *temp=head; //'head' is pointer to root node while(temp!=NULL) { if(temp->data==num) break; if(num>temp->data) temp=temp->right; if(num<temp->data) temp=temp->left; } if(temp==NULL) return false; else if(temp->data==num) return true; } The problem is: when I search for a value

Binary Tree - Counting nodes on a level

会有一股神秘感。 提交于 2019-12-01 09:20:34
I'm writing a binary tree class, and I'm stuck on a levelCount method, where I need to count the number of nodes on a level of the tree. The class and method look something like this: public class ConsTree<T> extends BinaryTree<T> { BinaryTree<T> left; BinaryTree<T> right; T data; public int levelCount(int level) { } } So the idea is that each tree has a tree to its left, a tree to its right, and data. There is an abstract class binarytree and subclasses ConsTree and EmptyTree. I think I need to use a breadth first search and count the number of nodes once I get to that level, but I'm stuck on

Using unique_ptr instead of shared_ptr in BST

浪尽此生 提交于 2019-12-01 08:33:19
I am trying to implement BST with unique_ptr . I got a working program for shared_ptr . How do I go about using unique_ptr instead to enforce the single ownership semantics of the BinarySearchTree? When I replace shared_ptr with unique_ptr , I get compilation errors beyond my understanding. #include <iostream> #include <memory> template<class T> class BinarySearchTree{ struct TreeNode; typedef std::shared_ptr<TreeNode> spTreeNode; struct TreeNode{ T data; spTreeNode left; spTreeNode right; TreeNode(const T & value):data(value),left(nullptr),right(nullptr){} }; spTreeNode root; bool insert

Generic binary search tree in C

血红的双手。 提交于 2019-12-01 08:21:45
I have the implemented a binary search tree but I also want to make it generic. The code is the following: typedef struct treeNode { int data; struct treeNode *left; struct treeNode *right; } treeNode; and the functions: treeNode* FindMin(treeNode *node) { if(node==NULL) { /* There is no element in the tree */ return NULL; } if(node->left) /* Go to the left sub tree to find the min element */ return FindMin(node->left); else return node; } treeNode * Insert(treeNode *node,int data) { if(node==NULL) { treeNode *temp; temp = (treeNode *)malloc(sizeof(treeNode)); temp -> data = data; temp -> left

Rails BST timezone implementation

戏子无情 提交于 2019-12-01 08:20:37
问题 Does anyone know how i can use BST for config.time_zone in my rails config/environment.rb file? At the moment i have left it as UTC and i'm thinking of adding BST to the list of supported timezones, and then extending the Time class to respect this (> last sunday of X month + 1hr) Where can i find the list of supported timezones? Searching around i have found alot of complaints but not alot of answers. this will be up on guthub asap. Cheers, Dougle 回答1: rake time:zones:all You'll probably see