binary-tree

Algorithm for creating Iterator for BinaryTree class

☆樱花仙子☆ 提交于 2019-12-14 03:57:34
问题 I want to add Bi-Directional Iterator (like Iterator exported by std::set) in my Parametrized BinaryTree class but I'm unable to comeup with any algorithm. Simply structure of Binary tree node is , it contains three pointers , left , right , parent: 回答1: With the given structure you want to proceed like this: To start the iteration you would find the left-most node. To go to the next node the operation depends on where you currently are: If your node has a right child you go to this child and

most efficient way to delete an entire binary search tree

北慕城南 提交于 2019-12-14 03:27:05
问题 I would like to know the most efficient way to delete an entire binary search tree. also not to let any memory leak so having to check if all the nodes in the sub trees are deleted first. I cant think of any efficient way besides post order traversal, deleting as you go. any suggestions or ideas? 回答1: Just set your root node as null . Let the garbage collector to do its job. 回答2: Deleting all leaves from a binary tree thread discusses about deleting all children of binary tree. public static

BInary search tree transversals

梦想的初衷 提交于 2019-12-14 02:12:47
问题 I am confused on the recursion in binary search tree transversals, I'm just lost since i need to return a list at the end and don't get how to save the values.It adds values like this shown below and I don't get what data stype is used to save the values like that also I don't think I'm moving through the tree correctly here is my code for , not sure if my unittest is correct either def inorder(self): print("IN INORDER_______________________________") print("Printing self.value" + str(self._

error: field ‘children’ has incomplete type ‘Node [2]

本秂侑毒 提交于 2019-12-13 22:01:33
问题 When I run this code, i get the error: "Field 'children' has incomplete type 'Node[0]'". I'm coding in C++ and I want to create a Node class which creates in itself two other Node objects and so on, until it reaches the maxDepth. The full error I get: 18:24:16 **** Incremental Build of configuration Debug for project Tests **** make all Building file: ../main.cpp Invoking: Cross G++ Compiler g++ -std=c++0x -O3 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "..

Understanding how to calculate the depth of a Binary Tree

落花浮王杯 提交于 2019-12-13 19:25:53
问题 I am going through the Career Cup guide as a crash course through basic CS principals and stuck on the example which calculates the minimum/maximum depth of a Binary Tree. Since this is the same problem I'm having with almost every example I thought I'd post a question here. The instructions are to implement a method that will check to see if a tree is balanced. In order to do this, you need to compare the minimum depth with the maximum depth, and ensure their difference is no greater than 1.

Is a list implementation of binary tree scalable?

百般思念 提交于 2019-12-13 16:14:53
问题 I am writing a simple codec. The tree will be precomputed and there wont be any changes once its build. It will just be searched. All the leaf nodes of a balanced binary tree are the signal values and internal nodes are the approximated compressed representations. Is a list implementation using stl vector scalable if I have a large value of leaf nodes? Currently I dont know how large large is. List implementation e.g. 1,2,3,4,5,6,7 if I have 4 leaf nodes then children of root(1)-> 2,3 2->4,5

Why can't I write my binary tree inside a file? [duplicate]

泪湿孤枕 提交于 2019-12-13 10:07:27
问题 This question already has answers here : What is a NullPointerException, and how do I fix it? (12 answers) Closed 2 years ago . Inside my displayStudent() method in my BinaryTree.java file, I'm trying to write my Binary Tree with students on them into a file. The code I have is throwing me an error. I've tried many different ways to write this file in with no success hence why I'm here. Here's my Node.java file: class Node { Student data; Faculty data2; Node left; Node right; public Node

Trim the tree at the specified depth. In Prolog

微笑、不失礼 提交于 2019-12-13 08:21:58
问题 Arguments: arbitrary binary tree; necessary depth; result tree. Result: ?- pred(s(f(b(m,k),a),t(a,g)),2,X). X = s(f,t) yes ?- pred(s(f(b(m,k),a),t(a,g)),3,X). X = s(f(b,a),t(a,g)) yes ?- Can someone help me with it? 回答1: Let's rearrange your examples: ?- pred( s( f(b(m,k),a), t(a,g)), 3, X). X = s(f(b,a),t(a,g)) yes ?- pred( s( f(b(m,k),a), t(a,g)), 2, X). X = s(f,t) yes ?- pred( s( f(b(m,k),a), t(a,g)), 1, X). X = s yes ?- pred( s( f(b(m,k),a), t(a,g)), 0, X). no Now it's clear what needs to

BinarySearchTree remove method malfunctioning when removing integers

萝らか妹 提交于 2019-12-13 08:03:45
问题 I wrote the BinaryTree structure from scratch, which included a remove(Object obj) method to remove elements from said tree. It seems to function correctly for every type except an Integer, and I can't figure out why. public BinaryTree<E> remove(Object obj) { try{ E value = (E)obj; int cmp = value.compareTo(this.value); if(cmp == 0) { List<BinaryTree<E>> kids = children(); if(kids.size() == 0) { return new EmptyBinarySearchTree<E>(); } if(kids.size() == 1) { return kids.get(0); } //2 children

Why won't my binary tree display what's currently in it?

跟風遠走 提交于 2019-12-13 07:57:56
问题 I'm trying to display() my binary tree with what's inside already but for some reason it's not working. I'm getting nothing as an output. Maybe my logic for the methods is off? I'm not quite sure. Here's my Node.java file: (content missing) Here's my BinaryTree.java file: public class BinaryTree { public static Node root; public BinaryTree() { this.root = null; } public void insert(Node n, Student s) { if(n != null) { while(true) { if(s.getLastName().compareTo(root.data) < 0) { insert(n.left