binary-search-tree

Binary Search Tree Deletion (Inorder Pred method) C++

…衆ロ難τιáo~ 提交于 2019-12-24 10:24:34
问题 Ok so I thought it was fixed, but I'm getting totally inconsistent results. I rewrote it kind of from scratch to start fresh and here are my results. I get no errors, no crashing, it just doesn't remove them. It just totally messes up the tree and gives me a ton more leaves, and mixes everything up. Not sure where else to go template <class T> void BST<T>::remove(struct Node<T>*& root, const T& x) { Node<T>* ptr = root; bool found = false; Node<T>* parent; while (ptr != NULL && !found) { if

Tricky Segmentation faults with BST recursion in C

偶尔善良 提交于 2019-12-24 09:35:32
问题 I'm trying to add strings to a Binary Search Tree using a recursive insert method (the usual for BSTs, IIRC) so I can later print them out using recursion as well. Trouble is, I've been getting a segmentation faults I don't really understand. Related code follows (this block of code is from my main function): #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> // Stores the size of the C-strings we will use; // Standardized to 100 (assignment specifications say //

Binary search tree level order with parent node

梦想的初衷 提交于 2019-12-24 09:24:35
问题 Hi I am trying to print out level order of a binary search tree in the format (node element, parent node element) . I am currently using queues to get the level order but I am having a hard time getting the parent node. Is it possible to do with queues? If so how would I go about doing it? If not what is a more optimal way of doing it? Thank you! For example with the following tree: 6 / \ 5 7 Level 0: (6, null) Level 1: (5, 6) (7, 6) 回答1: So unfortunately there isn't a way of doing this

How to return value from recursive function in python?

六月ゝ 毕业季﹏ 提交于 2019-12-24 08:30:19
问题 I'm working with binary tree in python. I need to create a method which searches the tree and return the best node where a new value can be inserted. But i'm have trouble returning a value from this recursive function. I'm a total newbie in python. def return_key(self, val, node): if(val < node.v): if(node.l != None): self.return_key(val, node.l) else: print node.v return node else: if(node.r != None): #print node.v self.return_key(val, node.r) else: print node.v return node Printing node.v

Unable to remove object after using del statement in python

╄→尐↘猪︶ㄣ 提交于 2019-12-24 07:47:49
问题 class BinaryNode: def __init__(self, value): self.data = value self.left = None self.right = None def contains(root, value): if root is None: return False if value == root.data: return True if value < root.data: return contains(root.left, value) else: return contains(root.right, value) def insert(root, value): if root is None: root = BinaryNode(value) else: if value > root.data: if root.right is None: root.right = BinaryNode(value) else: return insert(root.right, value) else: if root.left is

Construction of BST from given Postorder Traversal

不问归期 提交于 2019-12-24 07:06:21
问题 I have postorder array of a BST tree size n how do i show there is only one BST that can be constructed form it. I know I can rebuild the tree if I add nodes from right to left but how do I show there is only one right tree? I have tried saying there are two possible trees and tried showing it is not possible but got stuck 回答1: It is possible only because it is a BST. Recall that for a Binary tree to be a valid Binary Search Tree: -Left subtrees' values must be less than root's value -Right

Find keys closest to an integer argument in Haskell tree

房东的猫 提交于 2019-12-24 04:26:07
问题 There is a plenty of solutions how to find closest lower and upper keys in binary tree in imperative languages, but a lack of same questions for doing it in purely functional style like that of Haskell. I'm curious to learn how it's possible to walk around a binary serch tree ahead of meeting both closest keys. There is a function and some pattern matches I've so far: data TreeMap v = Leaf | Node { pair::(Integer, v), l::TreeMap v, r::TreeMap v} deriving (Show, Read, Eq, Ord) closestLess ::

Pseudo Code and conditions for deleting a Node in Binary Search Tree

你。 提交于 2019-12-24 00:42:26
问题 I'm trying to write a function to remove a node from a binary tree. I haven't coded the function yet, and I am trying to think about the different conditions I should consider for removing a node. I am guessing that the possible conditions are: The node has no children The node has one child The node has 2 children In each of these cases what would be the algorithm to perform a delete function? 回答1: This is something you would find in any standard textbook about algorithms, but let's suppose

How does this BST node-deletion algorithm work?

狂风中的少年 提交于 2019-12-23 16:43:50
问题 I'm trying to follow the BST algorithm in "Data Structures and Algorithms" by Granville Barnett, but I don't understand the node-deletion algorithm it describes below. Section 3.3 (p. 22) Removing a node from a BST is fairly straightforward, with four cases to consider: the value to remove is a leaf node; or the value to remove has a right subtree, but no left subtree; or the value to remove has a left subtree, but no right subtree; or the value to remove has both a left and right subtree in

For a complete binary tree with n nodes, how many nodes are leaf nodes?

痞子三分冷 提交于 2019-12-23 12:12:33
问题 One of the answers in our powerpoint says it is n/2 leaves, but I am seeing another answer which says (n+1)/2. I was wondering which one is correct if any, and why? 回答1: In the simplest case a binary tree with a root node, a left and a right has 3 nodes, two of which are leaf nodes. It's (n+1)/2 . 回答2: If your total number nodes are n , and i are the total number of internal nodes ,i.e., whose degrees are 1. If the tree considered is a binary tree, then this relation holds true. 2i + 3 = n .