binary-tree

How to insert element in binary tree by recursion?

筅森魡賤 提交于 2019-12-11 05:49:15
问题 void Btree<T>::InsertNode2(T data, BtreeNode* root) { if (root==NULL) { root = new BtreeNode (data); return ; } if (data <= root->data) InsertNode2(data, root->leftchild); else InsertNode2(data, root->rightchild); } Why isn't it right? The root can't be assigned correctly. After calling the function it's still NULL. 回答1: It almost correct. Just, as said by @DragonMoon, all you need to do is to pass root by reference void Btree<T>::InsertNode2(T data, BtreeNode &* root) { if (root==NULL) {

How binary search tree is created?

自古美人都是妖i 提交于 2019-12-11 05:33:36
问题 Suppose i am having an array say 1 5 4 6 8 9 10 22 17 7 9 3 I want to create a binary search tree from this array. I need algorithm to understand that. I have read rest other things related to BST like inorder traversal preorder postorder , tree walk , insertion deletion etc Book has not provided how to create BST. Need help here 回答1: if you do not care about the tree being balanced it is simple: put the first element of the tree as the head. iterate over the array. if an element is bigger

How to return a list of values through recursion in java?

血红的双手。 提交于 2019-12-11 05:21:23
问题 Returning a single value through recursion works just fine. But what if I want to return a list of values that recursion goes through each call. Here's my code. public void inOrder(Node focusNode) { /* ArrayList<Integer> tempList = new ArrayList<Integer>(); */ if ( focusNode != null) { inOrder(focusNode.getLeftNode()); System.out.println(focusNode); /* tempList.add(focusNode.getElement()); */ inOrder(focusNode.getRightNode()); } /* int[] elems = new int[tempList.toArray().length]; int i = 0;

How to sort IP addresses in a trie table?

六月ゝ 毕业季﹏ 提交于 2019-12-11 05:07:55
问题 I want to make a bit of code to have a small "routing table" in my Go program. I'm using left-leaning red-black trees with the http://github.com/petar/GoLLRB package and basically it seems to work after fussing with it for a bit, however I suspect that I'm not sorting the IP prefixes correctly when I create the tree. The "lessThan" function I used experimentally is func lessRoute(a, b interface{}) bool { aNet := a.(Route).Net bNet := b.(Route).Net for i, a := range aNet.IP { if a < bNet.IP[i]

Given a pre-order binary tree visit construct a binary search tree with the same pre-order visit. (if possible)

不羁的心 提交于 2019-12-11 04:49:23
问题 Im trying to solve this problem:" A binary tree is given, check his pre-order visit and build a binary search tree with the same pre-order visit. Demonstrate if it is always possible, if not give an example when this is not possible." Any help? I need to write pseudocode and give time complexity but i have a lot of doubts about building a binary-search-tree with the same pre-order visit for every possible binary tree. 回答1: If you are using the classic algorithm for inserting in a binary

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

Steps to compress a file using Huffman Code

给你一囗甜甜゛ 提交于 2019-12-11 04:08:23
问题 I know there are many questions involving Huffman Code, including another one from myself, but I am wondering what would be the best way to actually encode a text file. Decompression seems trivial; traversing the tree, going left at 0 and right on 1, printing the character. Though, how does one go about compression? Somehow store the bit representation of the character in it's node the tree? Search the tree for the character each time it is encountered and trace the steps? Does it matter

Counting Treaps

こ雲淡風輕ζ 提交于 2019-12-11 03:48:29
问题 Consider the problem of counting the number of structurally distinct binary search trees: Given N, find the number of structurally distinct binary search trees containing the values 1 .. N It's pretty easy to give an algorithm that solves this: fix every possible number in the root, then recursively solve the problem for the left and right subtrees: countBST(numKeys) if numKeys <= 1 return 1 else result = 0 for i = 1 .. numKeys leftBST = countBST(i - 1) rightBST = countBST(numKeys - i) result

Inserting into a Binary Tree in Scheme

青春壹個敷衍的年華 提交于 2019-12-11 03:35:31
问题 I am wondering how to insert elements from a list into a binary search tree. I am wondering why the following code does not work as I expected. The output is '((4 1 5 13 6) () ()) My next problem is going to be sorting the elements in the list, but for now I just want to insert them. Is my output correct for the problem I stated? My code is as follows: ( define ( make-tree value left right ) ( list value left right )) ( define ( value tree ) ( car tree )) ( define ( left tree ) ( cadr tree ))

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