binary-search-tree

Delete node from BST in C

笑着哭i 提交于 2019-12-01 07:40:45
问题 i was trying to understand this function founded online for deleting a node from a BST. There are some things i can't understand This is the code : struct Node* Delete(struct Node *root, int data) { if (root == NULL) { return NULL; } if (data > root->data) { // data is in the left sub tree. root->left = Delete(root->left, data); } else if (data > root->data) { // data is in the right sub tree. root->right = Delete(root->right, data); } else { // case 1: no children if (root->left == NULL &&

A red black tree with the same key multiple times: store collections in the nodes or store them as multiple nodes?

浪子不回头ぞ 提交于 2019-12-01 07:37:22
Apparently you could do either, but the former is more common. Why would you choose the latter and how does it work? I read this: http://www.drdobbs.com/cpp/stls-red-black-trees/184410531 ; which made me think that they did it. It says: insert_always is a status variable that tells rb_tree whether multiple instances of the same key value are allowed. This variable is set by the constructor and is used by the STL to distinguish between set and multiset and between map and multimap. set and map can only have one occurrence of a particular key, whereas multiset and multimap can have multiple

Generic binary search tree in C

白昼怎懂夜的黑 提交于 2019-12-01 06:02:17
问题 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=

Using unique_ptr instead of shared_ptr in BST

纵然是瞬间 提交于 2019-12-01 05:34:40
问题 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;

How does this inorder traversal algorithm work?

早过忘川 提交于 2019-12-01 05:06:03
问题 I don't have much experience with recursion, so I'm having a hard time determining exactly how this algorithm works: public static void inorder(Node<?> n) { if (n != null) { inorder(n.getLeft()); System.out.print(n.data + " "); inorder(n.getRight()); } } I know that it visits the left and right child nodes of every node in the tree, but I just can't get my head around why exactly it works. 回答1: I'll try to give it a shot. Imagine a tree a b c d e f g Each letter represents a Node object. What

A red black tree with the same key multiple times: store collections in the nodes or store them as multiple nodes?

风流意气都作罢 提交于 2019-12-01 04:48:28
问题 Apparently you could do either, but the former is more common. Why would you choose the latter and how does it work? I read this: http://www.drdobbs.com/cpp/stls-red-black-trees/184410531; which made me think that they did it. It says: insert_always is a status variable that tells rb_tree whether multiple instances of the same key value are allowed. This variable is set by the constructor and is used by the STL to distinguish between set and multiset and between map and multimap. set and map

Find median in binary search tree

倖福魔咒の 提交于 2019-12-01 04:34:50
Write the implementation of the function T ComputeMedian() const that computes the median value in the tree in O(n) time. Assume that the tree is a BST but is not necessarily balanced. Recall that the median of n numbers is defined as follows: If n is odd, the median is x such that the number of values smaller than x is equal to the number of values greater than x. If n is even, then one plus the number of values smaller than x is equal to the number of values greater than x. For example, given the numbers 8, 7, 2, 5, 9, the median is 7, because there are two values smaller than 7 and two

Binary Tree - Counting nodes on a level

本秂侑毒 提交于 2019-12-01 04:23:20
问题 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

Find median in binary search tree

混江龙づ霸主 提交于 2019-12-01 02:15:13
问题 Write the implementation of the function T ComputeMedian() const that computes the median value in the tree in O(n) time. Assume that the tree is a BST but is not necessarily balanced. Recall that the median of n numbers is defined as follows: If n is odd, the median is x such that the number of values smaller than x is equal to the number of values greater than x. If n is even, then one plus the number of values smaller than x is equal to the number of values greater than x. For example,

First Common Ancestor of a Binary Tree

假如想象 提交于 2019-11-30 23:50:32
If I have a binary search tree like this then what will be lowest common ancestor of nodes 6 and 1? According to the Wikipedia definition of the Lowest common ancestor I correct myself: The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with n nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants ( where we allow a node to be a descendant of itself ). So yes going by this definition the correct answer would be 6 . If this is an interview question would be good