binary-tree

First Common Ancestor of a Binary Tree

折月煮酒 提交于 2019-12-12 07:19:46
问题 If I have a binary search tree like this then what will be lowest common ancestor of nodes 6 and 1? 回答1: According to the Wikipedia definition of the Lowest common ancestor I correct myself: The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with n nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants ( where we allow a node to be a descendant of itself ). So

C binary tree sort - extending it

雨燕双飞 提交于 2019-12-12 06:38:53
问题 I need some help in C Help me to extend the binary tree sort on C. I need to return a sorted array in sort function. here it is: #include <stdio.h> #include <stdlib.h> struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void insert ( struct btreenode **, int ) ; void inorder ( struct btreenode * ) ; int* sort(int *array, int arr_size) { struct btreenode *bt ; int i; bt = NULL; for ( i = 0 ; i <= 9 ; i++ ) insert ( &bt, array[i] ) ; inorder ( bt ) ; /

How can I parse character in a File in Squeak4.1?

ⅰ亾dé卋堺 提交于 2019-12-12 06:13:34
问题 friend, suppose I have a file test.txt, the content of file is "1+2*3", if the fomular directly expressed in Squeak's Workspace, print it will result 9 , What I want to get is 7 then I read the file content 1+2*3 from a file. code like this and it works well ReadFrom "read the equation from ./formular.txt" | fileContents | fileContents := FileStream readOnlyFileNamed: 'test.txt' do: [:f | f contents ]. ^fileContents. but how can I store the 5 caracters of string "1+2*3" into a collection ,

Complexity of a nested binary search tree

Deadly 提交于 2019-12-12 06:10:18
问题 Does anyone know how to calculate the complexity of a nested binary search tree? I have implemented a nested binary search tree to a depth of 3 BSTs. EDIT: I apologize for the confusion, I had meant that each node of the BST would point to the root node of another BST. The complexity I was asking for was time complexity of search, update, and delete (basic operations). I had assume that since the time complexity of a BST was O(log(n)), the time complexity of a nested BST in terms of search,

tree-equal function not giving the correct output

怎甘沉沦 提交于 2019-12-12 05:31:24
问题 I am writing code to test if two trees are equal (in data and structure) in Scheme, and I have to assume that I only have at most two children for each node. My code is as follows: (define (make-tree value left right) (list value left right)) (define (value tree) (car tree)) (define (left tree) (car (cdr tree))) (define (right tree) (car (cdr (cdr tree)))) (define (tree-equal? T1 T2) (if (and (null? T1) (null? T2)) #t (if (= (value T1) (value T2)) (tree-equal? (left T1) (left T2)) (tree-equal

Retrieve all the leaf nodes of java TreeMap

一世执手 提交于 2019-12-12 04:36:32
问题 Is there a way to retrieve all the leaf nodes of a Java TreeMap ? I have a TreeMap like this TreeMap<String, FooBar> myTree = new TreeMap<String, FooBar>(new Comparator<String>() { public int compare(String o1, String o2) { int b1 = Integer.parseInt(o1, 2); int b2 = Integer.parseInt(o2, 2); return (b1 > b2 ? 1 : (b1 == b2 ? 0 : -2)); } }); How do I get all the leaf nodes of this tree? 回答1: This doesn't make much sense to me. The elements stored at the leaves are dependent on how the

Is there a way to convert from a general tree to binary SEARCH tree?

霸气de小男生 提交于 2019-12-12 03:58:02
问题 I know how to convert from a general tree to a binary tree just fine, a a / | \ / b c d -> b \ c \ d I was just asked how to convert from a general tree to a binary search tree though. My thoughts are that the person who asked me either didn't mean binary search tree (I asked him, he said he did), or he's misunderstanding something from his class notes. In any case, has anyone heard of doing this? General tree to binary search tree? The answer I gave him was first convert to a binary tree

converting sorted linked list to balanced binaryTree not returning correctly?

馋奶兔 提交于 2019-12-12 03:45:42
问题 Here are key methods I wrote for converting linkedList to Balanced BinarySearch Tree . I get BST but it is not balanced. why is it so? public static Node headNode; public static IntTreeNode convertLinkedListToBST(Node node){ int len = getCount(node); headNode = node; return convertLinkedListToBSThelper(node, 0, len-1); } //http://www.programcreek.com/2013/01/leetcode-convert-sorted-list-to-binary-search-tree-java/ public static IntTreeNode convertLinkedListToBSThelper(Node node, int start,

How to print out a binary tree in order?

做~自己de王妃 提交于 2019-12-12 03:34:03
问题 I'm struggling with the printing of a binary tree that I coded. It's a little program where you can type in different states and then it orders them alphabetically in the binary tree and afterwards it should print them out again, in alphabetic order. How should I start with the listing function? I read in the internet that a recursive algorithm would be the best, but I don't really know how to do that with my setup. #include <iostream> #include <stdlib.h> #include <string> using namespace std

Building parse tree from prefixed expression in c

坚强是说给别人听的谎言 提交于 2019-12-12 02:15:43
问题 I'm trying to create a binary tree like this: link The input will be entered by the user as prefix and read as a string, then put in a binary tree. This is what I have so far: struct node{ char val; struct node *left; struct node *right; }; typedef struct node root; typedef root *tree; In main: void main(){ int i; tree tr; char* s; s=input(); //input function tr=create_empty_tree(); for(i=0;s[i]!='\0';i++){ tr=add_root(s[i],ab); } convert_infix(tr); } and this is the part I've been struggling