binary-tree

Deleting leftmost leaf in a binary tree

前提是你 提交于 2019-12-11 10:54:06
问题 I wish to delete the leftmost leaf in a BT( not BST!). I tried to delete it using Reverse level order traversal but once I find the leftmost node and delete it and try returning, I cannot handle the extra recursive calls. Basically with the current code, my entire tree gets deleted recursively, but I just want it to delete leftmost leaf. I tried breaking the recursion using exit() and my entire program stopped. On first call only 5 should be deleted. On next call 25 should be deleted . On

Recursive Tree Traversal Method With Return Type Array

随声附和 提交于 2019-12-11 10:46:30
问题 Is there a way to recursively traverse a tree and return an array that is scoped to that recursive method? So I recently answered someone else's question on this topic. That question can be found here: SO Question. My solution uses an array outside of the scope of the recursion, and therefore the method cannot (or at least probably should not) return the array. However, is there a way to write a recursive method for traversing trees such that it returns an array? Even writing an initial

Deleting leaves from a binary tree

一曲冷凌霜 提交于 2019-12-11 08:52:11
问题 public void deleteLeaves(BSTNode<T> p){ //deletes leaves if(p.left != null){ if(isLeaf(p.left)) p.left = null; } else if(p.right != null){ if(isLeaf(p.right)) p.right = null; } else{ deleteLeaves(p.right); deleteLeaves(p.left); } } I cannot seem to figure out as to why it is not correctly deleting the leaves. It seems to only delete the final leaf of the tree. Any advice that helps would greatly be appreciated. 回答1: Your if-statements are a bit messed up; Only one of the branches will be

How to parse and print a tree in python

▼魔方 西西 提交于 2019-12-11 07:58:50
问题 Currently I have data in the following format A A -> B -> C -> D -> Z A -> B -> O A -> X This is stored in a list [line1,line2, and so forth] Now I want to print this in the following manner A |- X |- B |- O |- C |- D |- Z I'm new to python so. I was thinking of finding '->' in each element in array and replacing with space. I don't know to go forward. 回答1: Here is a little code to get you started (add your own beautifications as needed): >>> def make_links(data): 'Create a dictionary mapping

delphi save and load Binary Tree

醉酒当歌 提交于 2019-12-11 07:45:10
问题 please , suppose i have this Binary Tree structure : Type TPMyTree = ^TMyTree; TMyTree = Record ID:Integer; FullName:String[50];//<-------- fixed addrs:String[50] // <----------- fixed LeftT:TPMyTree; RightT:TPMyTree; end; How could i save and load it from a Stream ? 回答1: When working with streams, the most complex issue is dealing with variable-length data. In your case that's the FullName and the addrs , because those fields are of type String . The easiest solution is to use Delphi's

C++ Binary Tree Traverse and Function Pointer Parameter

旧巷老猫 提交于 2019-12-11 07:43:59
问题 What I want to do is to traverse the node in order, so that I can print the node in binary tree in order. void inorder_traverse(node* root, function<void(node*)> visit) { if (root == nullptr) return; cout << root->data << ", "; inorder_traverse(root->left, visit); visit(root); inorder_traverse(root->right, visit); } I saw this code for inorder traversing a binary tree. Traversing goes all the nodes so I thought I could print all the data of all visited nodes using traversing function. Would

Why is add in unbalanced Binary Search Tree O(n)?

空扰寡人 提交于 2019-12-11 06:54:56
问题 This is the implementation of add in Binary Search Tree from BST Add private IntTreeNode add(IntTreeNode root, int value) { if (root == null) { root = new IntTreeNode(value); } else if (value <= root.data) { root.left = add(root.left, value); } else { root.right = add(root.right, value); } return root; } I understand why this runs in O(log n). Here's how I analyze it. We have a tree size of n. How many cuts of 2, or half cut, will reduce this tree down to a size of 1. So we have the

How do I correct an implicit conversion error in BST C# Code?

烈酒焚心 提交于 2019-12-11 06:33:48
问题 I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to 'int' // on findmin Cannot implicitly convert type x.Program.TreeNode' to 'int' // on findmax and is my main correct or missing somethin? and how i can count the nodes,leaves and get the hight (need only hints) 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

Haskell: Turning a tree into a map

大憨熊 提交于 2019-12-11 05:57:20
问题 Basically I want to turn a BST tree into a map where the nodes are the keys and the number of occurrences of the nodes are the values. So if I inputted this: toMap (leaf 13) I would get > [(13,1)] Here is what I have so far: data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show) leaf x = Node x Empty Empty toMap' :: Int -> Tree a -> ([(a, Int)], Int) toMap' a Empty = ([], a) toMap' a (Node x xl xr) = ((x, a): xl' ++ xr', k) where (xl', i) = toMap' (a+1) xl (xr', k) = toMap' (i) xr

C binary search tree implementation - insert

独自空忆成欢 提交于 2019-12-11 05:52:31
问题 I am trying to create a program which which takes a list of words as an input, and sorts them into a binary tree, in order to allow them to be found, e.g. like a dictionary. This is what I have done so far, but am getting a segmentation error with newEl -> el = input; I know this is because it is trying to point to a NULL el, when the tree is first created, but i'm not sure what the best way to improve my code would be. Anyone have any ideas? Thanks. struct node *tra(struct node * start, Type