binary-tree

Binary Search Tree Array Imp. C++

天大地大妈咪最大 提交于 2020-01-25 13:04:04
问题 I'm just having trouble with Inserting into the array...And having the children branch off from the root, or "parent".. I've been trying to insert data into an array based implementation of a BST: BST::BST(int capacity) : items(new item[capacity]), size(0) { // define the constructor to the BST Class. } void BST::insert (const data& aData) { if ( items[root].empty ) // make the first data the root. { items[root].theData = aData; items[root].empty = false; size++; } else if ( items[root]

How to print elements from a binary tree ignoring all the repeating ones?

一笑奈何 提交于 2020-01-25 11:53:09
问题 public void printTree(node root) { if(root != null) { printTree(root.left); System.out.print(root.word + " " + root.line+" "); String tempStr=root.word; int tempLn=root.line; //don't know how to use it printTree(root.right); } } Assume that the tree is already sorted in a lexicographic order. For example, the file is like this: aaa zzz the the the the and the output should be like this: aaa line: 1 the line: 3 3 3 3 zzz line: 2 My code now displays the same words for many times. I don't know

Binary Tree root is null

拈花ヽ惹草 提交于 2020-01-24 19:01:05
问题 I'm trying to create a binary search tree but it doesn't seem to be working. I debugged it, and it says the root is null. I don't understand why it is null. I set it to null initially in the constructor, but then when I call the insert() method, it is no longer null, right? Can someone help me understand this. Thanks. #include "stdafx.h" #include <iostream> using namespace std; struct node { public: int value; node * left; node * right; }; class bTree { public: node * root; public: bTree();

Red-black tree access by ordinal index

坚强是说给别人听的谎言 提交于 2020-01-23 11:53:15
问题 I have a red-black tree (binary tree, all the leaves are within 2 levels). I can navigate through nodes: to left, right or parent. I know the entire count of nodes. I have to find the N-th smallest element in a tree. Is there any way to do this faster than in O(n)? Any ideas of optimizing access by index? 回答1: In each node X you should store, how many nodes are in subtree with X as a root. count(LEAF) = 1 count(NODE) = count(NODE->LEFT) + count(NODE->RIGHT) + 1 During each insert/delete you

Red-black tree access by ordinal index

人盡茶涼 提交于 2020-01-23 11:52:53
问题 I have a red-black tree (binary tree, all the leaves are within 2 levels). I can navigate through nodes: to left, right or parent. I know the entire count of nodes. I have to find the N-th smallest element in a tree. Is there any way to do this faster than in O(n)? Any ideas of optimizing access by index? 回答1: In each node X you should store, how many nodes are in subtree with X as a root. count(LEAF) = 1 count(NODE) = count(NODE->LEFT) + count(NODE->RIGHT) + 1 During each insert/delete you

How store binary tree nodes in firestore

本小妞迷上赌 提交于 2020-01-22 02:48:08
问题 I want to create a binary tree in react.I am using react-d3-tree component for displaying the tree. For react-d3-tree the data should be of the format const myTreeData = [ { name: 'Top Level', attributes: { keyA: 'val A', keyB: 'val B', keyC: 'val C', }, children: [ { name: 'Level 2: A', attributes: { keyA: 'val A', keyB: 'val B', keyC: 'val C', }, }, { name: 'Level 2: B', }, ], }, ]; How to store data on firestore so that I can retrieve it and get it as the above array format? 回答1: You just

Average height of a binary search tree

我们两清 提交于 2020-01-21 03:28:06
问题 How do you compute the average height of a binary search tree when adding 1000 random ints? What is the average height? 回答1: You can compute the height of a binary tree using this recursive definition: height(empty) = 0 height(tree) = 1 + max(height(tree.left), height(tree.right)) One way to empirically measure the average height of such a tree is to repeatedly create an empty tree and add 1000 random items to it. Measure the height of each trial using the above function, and average them. I

How to construct a binary tree using a level order traversal sequence

一笑奈何 提交于 2020-01-21 02:28:09
问题 How to construct a binary tree using a level order traversal sequence, for example from sequence {1,2,3,#,#,4,#,#,5}, we can construct a binary tree like this: 1 / \ 2 3 / 4 \ 5 where '#' signifies a path terminator where no node exists below. Finally I implement Pham Trung's algorithm by c++ struct TreeNode { TreeNode *left; TreeNode *right; int val; TreeNode(int x): left(NULL), right(NULL), val(x) {} }; TreeNode *build_tree(char nodes[], int n) { TreeNode *root = new TreeNode(nodes[0] - '0'

Inorder tree traversal: Which definition is correct?

假装没事ソ 提交于 2020-01-20 14:22:42
问题 I have the following text from an academic course I took a while ago about inorder traversal (they also call it pancaking) of a binary tree (not BST): Inorder tree traversal Draw a line around the outside of the tree. Start to the left of the root, and go around the outside of the tree, to end up to the right of the root. Stay as close to the tree as possible, but do not cross the tree. (Think of the tree — its branches and nodes — as a solid barrier.) The order of the nodes is the order in

Inorder tree traversal: Which definition is correct?

。_饼干妹妹 提交于 2020-01-20 14:22:19
问题 I have the following text from an academic course I took a while ago about inorder traversal (they also call it pancaking) of a binary tree (not BST): Inorder tree traversal Draw a line around the outside of the tree. Start to the left of the root, and go around the outside of the tree, to end up to the right of the root. Stay as close to the tree as possible, but do not cross the tree. (Think of the tree — its branches and nodes — as a solid barrier.) The order of the nodes is the order in