binary-search-tree

How to delete all nodes of a Binary Search Tree

蹲街弑〆低调 提交于 2019-12-11 04:45:50
问题 I am trying to write a code to delete all nodes of a BST (each node has only three attributes, left, right and data, there are no parent pointers). The following code is what I have come up with, it deletes only the right half of the tree, keeping the left half intact. How do I modify it so that the left half is deleted as well (so that ultimately I am left with only the root node which has neither left or right subtrees)? def delete(root): global last if root: delete(root.left) delete(root

How to create a binary search tree that includes all numbers from 1 to n

青春壹個敷衍的年華 提交于 2019-12-11 04:44:36
问题 Im trying to create a Binary search tree that includes all numbers from 1 to n. an example would be from 1 to 5 would be something like root: 3 root.left: 2 root.left.left = 1 root.right = 4 root.right.right = 5 This tree happens to be not very balanced, but I would prefer a method that produces as balanced of a tree as possible. I am trying to create my own data structure for this, so I basically just wrote a Node class: private class BinaryNode{ int data; BinaryNode left; BinaryNode right;

Implementing hashCode for a BST

青春壹個敷衍的年華 提交于 2019-12-11 03:54:34
问题 In Java to compare two objections for equality, you must implement both the equals method and the hashCode method. I need to compare two BSTs for equality. How do I implement the hashCode method in such a case? Now, implementing the hashCode on the Node class is simple enough: imagine my data is int . But I cannot just add the values of the node to check if the trees are equal. So how do I do it? Has anyone done this successfully? I am thinking of many different things that I can do, but I am

Search, Insert, Delete and DeleteLast operations in logN

我们两清 提交于 2019-12-11 03:36:27
问题 What can be best suited data structure that supports the following operations in O(log n) time: search(x) finds the element with key x insert(x) insert an element with a key x delete(x) delete an element with a key x deleteLast() removes the most recently inserted element I know that a binary search tree can handle first three operations pretty good. But how to handle fourth query. If BST is not a good solution than also tell what can be best suited data structure for handling all four queries

Find parent of node in binary search tree

旧巷老猫 提交于 2019-12-11 03:34:30
问题 I have trouble with a task to find a parent of a specific node in a binary search tree. The solution should be straightforward, but I don't know why my code is not working...I tried different approaches and also searched on the web for any solutions but found nothing. I appreciate any help!! typedef struct Treenode{ int key; struct Treenode* lChild; struct Treenode* rChild; }node; node * getParent(node *root, int key){ if (root == NULL) return NULL; else if (root->rChild->key == key || root-

Why can't I find _left and _right in BinarySearchTree?

有些话、适合烂在心里 提交于 2019-12-11 03:12:13
问题 I'm having a problem with the following code snippet: using System; using System.Collections.Generic; using System.Text; namespace trees_by_firas { class Program { static void Main(string[] args) { BinarySearchTree t = new BinarySearchTree(); t.insert(ref t.root, 10); t.insert(ref t.root, 5); t.insert(ref t.root, 6); t.insert(ref t.root, 17); t.insert(ref t.root, 2); t.insert(ref t.root, 3); BinarySearchTree.print(t.root); Console.WriteLine("--------------------"); Console.WriteLine(t.FindMax

What is the runtime of my algorithm?

岁酱吖の 提交于 2019-12-11 02:47:25
问题 I'm writing an algorithm that will first take a config file of various endpoints and their associated method like the following: /guest guestEndpoint /guest/lists listEndpoint /guest/friends guestFriendsEndpoint /guest/X/friends guetFriendsEndpoint /guest/X/friends/X guestFriendsEndpoint /X/guest guestEndpoint /X/lists listEndpoint /options optionsEndpoint X here represents a wildcard, so any string would match with this. The algorithm would take this as input and build a tree with each node

Fold for Binary Tree

匆匆过客 提交于 2019-12-10 19:12:32
问题 I have to make an implementation of a Binary Tree instantiate a typeclass: class Set s where add :: (Eq a) => a -> s a -> s a remove :: (Eq a) => a -> s a -> s a exists :: (Eq a) => a -> s a -> Bool fold :: (a -> b -> b) -> s a -> b -> b data BTree k v = Empty | Node k v (BTree k v) (BTree k v) deriving (Show) All went well until I had to implement a fold for a binary tree. The issue I'm having is that I don't really know how to keep the type declaration of my function with a signature like

How to display binary search tree in console properly?

[亡魂溺海] 提交于 2019-12-10 18:37:18
问题 I'm trying to display a BST in console. That's my code (it's a modified version of code found here: Printing Level Order Binary Search Tree Formatting): string printLevel(node *root, int level, string gap) { if (!root) { return gap + "-" + gap; } if (level==1) { stringstream out; out<<root->value; return gap + out.str() + gap; } else if (level>1) { string leftStr = printLevel(root->left, level-1, gap); string rightStr = printLevel(root->right, level-1, gap); return leftStr + " " + rightStr; }

Augmenting data structure without wasting memory

南楼画角 提交于 2019-12-10 18:30:53
问题 I have a class Tree that I'd like to augment into more specialized data structures, such as Order_tree and Interval_tree . These augmentations require additions to the Node , such as size information, and minor alterations to some algorithms. I'd like to know the best way to implement augmentations in C++ in terms of performance, readability, and maintainability. The trees should not be used in a polymorphic manner. What I've attempted so far is publicly inheriting Tree , and then overloading