binary-tree

Converting an infix expression (with parentheses) into a binary tree

给你一囗甜甜゛ 提交于 2020-01-02 03:17:06
问题 As part of a Java assignment, I have to take an input arithmetic expression and store it in a binary tree. I have done everything necessary for the assignment except for the part where I read in the string of the expression and store it in the binary tree. I have created a class called BinaryTree. Its only field is a treenode called root. This treenode is defined as an innerclass in BinaryTree. It has 3 fields, a generic data field, and two children (left and right) that are type BinaryTree.

How to search and sort BST by name(string)? Printing by queue and indented?

五迷三道 提交于 2020-01-01 19:58:08
问题 I have to write a program that reads a .txt file into the tree and then it allows to perform specific operations with it. I'm stuck on the part where I need to sort tree by names and search by name as well, any input would be awesome. So, my input file is in the format : 3800 Lee, Victor; 2.8 3000 Brown, Joanne; 4.0 So, my binary tree is in the format of: typedef struct { int id; char name[MAX_NAME_LEN]; float gpa; } STUDENT; typedef struct node { STUDENT* dataPtr; struct node* left; struct

Saving a Binary tree to a file [closed]

我的梦境 提交于 2020-01-01 12:37:11
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I have a non-balanced (not binary-search) binary tree Need to incode (and later decode) it to txt file. How can I do it in efficient way? I found this link which talks about similar (same) problem,but it is obvious for me 回答1: Please look at this on LeetCode. I like this solution because it's relatively

how to determine a balanced or perfectly balanced Binary search tree ( just from the picture )

南笙酒味 提交于 2020-01-01 11:20:11
问题 I am not sure how to determine if a tree is balanced, perfectly balanced, or neither if I have it as a picture not a code For example if I have this tree How can I check if it's balanced, perfectly balanced, or unbalanced? and can someone give me an example of a perfectly balanced tree? [o] / \ [b] [p] \ / \ [d] [m] [r] Clearly I can tell that the tree is unbalanced if it was something like this: [b] \ [d] \ [r] \ [c] However, if it was something very similar to the one above I don't know how

Binary Tree implementation C++

笑着哭i 提交于 2020-01-01 06:30:11
问题 Binary Tree insertion: #include "stdafx.h" #include <iostream> using namespace std; struct TreeNode { int value; TreeNode* left; TreeNode* right; }; struct TreeType { TreeNode* root; void insert(TreeNode* tree, int item); void insertItem(int value) { insert(root, value); } }; void TreeType::insert(TreeNode* tree, int number) { if (tree == NULL) { tree = new TreeNode; tree->left = NULL; tree->right = NULL; tree->value = number; cout << "DONE"; } else if (number < tree->value) { insert(tree-

Proof that a binary tree with n leaves has a height of at least log n

≡放荡痞女 提交于 2020-01-01 03:34:08
问题 I've been able to create a proof that shows the maximum total nodes in a tree is equal to n = 2^(h+1) - 1 and logically I know that the height of a binary tree is log n (can draw it out to see) but I'm having trouble constructing a formal proof to show that a tree with n leaves has "at least" log n. Every proof I've come across or been able to put together always deals with perfect binary trees, but I need something for any situation. Any tips to lead me in the right direction? 回答1: Lemma :

Finding the common ancestor in a binary tree

一世执手 提交于 2020-01-01 03:12:06
问题 This question was asked to me in an interview: I have a binary tree and I have to find the common ancestor (parent) given two random nodes of that tree. I am also given a pointer to the root node. My answer is: Traverse the tree separately for both the nodes till you reach the node that is expected. Parallel while traversing store the element and the next address in a linked list. Then we have two linked lists with us. So try comparing the two linked lists and the last common node in both the

Finding the common ancestor in a binary tree

血红的双手。 提交于 2020-01-01 03:11:11
问题 This question was asked to me in an interview: I have a binary tree and I have to find the common ancestor (parent) given two random nodes of that tree. I am also given a pointer to the root node. My answer is: Traverse the tree separately for both the nodes till you reach the node that is expected. Parallel while traversing store the element and the next address in a linked list. Then we have two linked lists with us. So try comparing the two linked lists and the last common node in both the

Want to save binary tree to disk for “20 questions” game

两盒软妹~` 提交于 2020-01-01 00:23:12
问题 In short, I'd like to learn/develop an elegant method to save a binary tree to disk (a general tree, not necessarily a BST). Here is the description of my problem: I'm implementing a game of "20-questions". I've written a binary tree whose internal nodes are questions and leaves are answers. The left child of a node is the path you'd follow if somebody answered "yes" to your current question, while the right child is a "no" answer. Note this is not a binary search tree, just a binary tree

balancing an AVL tree (C++)

我的梦境 提交于 2019-12-31 09:21:24
问题 I'm having the hardest time trying to figure out how to balance an AVL tree for my class. I've got it inserting with this: Node* Tree::insert(int d) { cout << "base insert\t" << d << endl; if (head == NULL) return (head = new Node(d)); else return insert(head, d); } Node* Tree::insert(Node*& current, int d) { cout << "insert\t" << d << endl; if (current == NULL) current = new Node(d); else if (d < current->data) { insert(current->lchild, d); if (height(current->lchild) - height(current-