binary-search-tree

Inserting word from a text file into a tree in C

徘徊边缘 提交于 2019-12-10 18:29:00
问题 I have been encountering a weird problem for the past 2 days and I can't get to solve it yet. I am trying to get words from 2 texts files and add those words to a tree. The methods I choose to get the words are refereed here: Splitting a text file into words in C. The function that I use to insert words into a tree is the following: void InsertWord(typosWords Words, char * w) { int error ; DataType x ; x.word = w ; printf(" Trying to insert word : %s \n",x.word ); Tree_Insert(&(Words-

ScalaCheck generate BST

孤街浪徒 提交于 2019-12-10 15:48:46
问题 I'm trying to create a Gen for BST with ScalaCheck, but when I call .sample method it gives me java.lang.NullPointerException. Where do I wrong? sealed trait Tree case class Node(left: Tree, right: Tree, v: Int) extends Tree case object Leaf extends Tree import org.scalacheck._ import Gen._ import Arbitrary.arbitrary case class GenerateBST() { def genValue(left: Tree, right: Tree): Gen[Int] = (left, right) match { case (Node(_, _, min), Node(_, _, max)) => arbitrary[Int].suchThat(x => x > min

represent binary search trees in python

夙愿已清 提交于 2019-12-10 13:36:44
问题 how do i represent binary search trees in python? 回答1: class Node(object): def __init__(self, payload): self.payload = payload self.left = self.right = 0 # this concludes the "how to represent" asked in the question. Once you # represent a BST tree like this, you can of course add a variety of # methods to modify it, "walk" over it, and so forth, such as: def insert(self, othernode): "Insert Node `othernode` under Node `self`." if self.payload <= othernode.payload: if self.left: self.left

BST : inOrder successor and predecessor

最后都变了- 提交于 2019-12-10 11:48:08
问题 Do they look correct? I implemented them and was looking to review them Node predecessor(Node node) { if ((node.left == null) && (node.right==null)) { return node; } if (node.right != null) { return predecessor(node.right); } if (node.left != null) { return predecessor(node.left); } } Node successor(Node node) { if ((node.left == null) && (node.right==null)) { return node; } if (node.left != null) { return successor(node.left); } if (node.right != null) { return successor(node.right); } } 回答1

Printing Successor and Predecessor in a BST

*爱你&永不变心* 提交于 2019-12-10 11:39:58
问题 My problem is as follows: I have a binary search tree with keys: a1<a2<...<an , the problem is to print all the (a_i, a_i+1) pairs in the tree (where i={1,2,3,...}) using a recursive algorithm in O(n) time without any global variable and using O(1) extra space. An example: Let the keys be: 1,2, ..., 5 Pairs that will be printed: (1,2) (2,3) (3, 4) (4, 5) So you can't do inorder traversal in the tree and find the successor/predecessor for each node. Because that would take O(nh) time and if

Can a red node have just 1 black child in a red-black tree?

痞子三分冷 提交于 2019-12-10 11:31:51
问题 The rules for a Red-Black Tree: Every node is either red or black. The root is black. Every leaf (NIL) is black. If a node is red, then both its children are black. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes. Rule 4 mentions that red nodes need both black childs but what if there is just one child to begin with? Is there an argument to prove or disprove this? 回答1: No,a red node cannot have one child,consider the following cases:-

Counting the nodes in a binary search tree

丶灬走出姿态 提交于 2019-12-10 11:29:28
问题 I need to create a recursive method that takes as a parameter the root node of a binary search tree. This recursive method will then return the int value of the total number of nodes in the entire binary search tree. This is what I have so far: public class BinarySearchTree<E> extends AbstractSet<E> { protected Entry<E> root; //called by the main method public int nodes() { return nodes(root); } //nodes() will count and return the nodes in the binary search tree private int nodes(Entry<E>

Breadth First Traversal With Binary Search Tree C++

六眼飞鱼酱① 提交于 2019-12-10 10:47:37
问题 Maybe fast/simple Question. I have a a Binary Tree Implemented already, Then I was hoping to convert binary search tree into an array or at least print it out as if in an array. Where I am having trouble with is how to get the NULL/flags in there '\0'. for example lets say I have a tree like: 10 / \ 6 12 / \ \ 1 8 15 \ 4 And I want it to print how its supposed to print. Like: [10,6,12,1,8,\0,15,\0,4,\0,\0,\0,\0,\0,\0] ^Something Like this^ I don't know if I counted the NULL correctly. Or

How to convert python list of tuples into tree?

大兔子大兔子 提交于 2019-12-10 10:27:09
问题 I have a list of tuples like list_of_tuples = [(number, name, id, parent_id), (number, name, id, parent_id), ] I am trying to sort it into an ordered structure like: { parent: [(id, name), (id, name)], parent: {parent: [(id, name)] { So, any node could have a parent and/or children I tried with: tree = defaultdict(lambda: [None, ()]) ancestors = set([item[3] for item in list_of_tuples]) for items in list_of_tuples: children_root = {} descendants = [] number, name, id, parent = items if parent

Python: Create a Binary search Tree using a list

隐身守侯 提交于 2019-12-10 08:32:18
问题 The objective of my code is to get each seperate word from a txt file and put it into a list and then making a binary search tree using that list to count the frequency of each word and printing each word in alphabetical order along with its frequency. Each word in the can only contain letters, numbers, -, or ' The part that I am unable to do with my beginner programming knowledge is to make the Binary Search Tree using the list I have (I am only able to insert the whole list in one Node