binary-tree

Algorithm of combining two binary trees?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 02:06:19
问题 For example: two trees : 8 9 5 7 4 20 30 become one tree? 回答1: Without more details/constraints, the simplest solution is to take a leaf node of either tree, remove it, and use it as the root to the newly created three. In your example: 30 8 9 5 7 4 20 This works because your trees don’t appear to follow any particular order, don’t appear to be balanced, nor have any other constraints. Since any leaf node will do, this is an O ( n ) operation in the worst case (traverse one of the trees in

Algorithm of combining two binary trees?

风格不统一 提交于 2019-12-02 01:33:39
For example: two trees : 8 9 5 7 4 20 30 become one tree? Without more details/constraints, the simplest solution is to take a leaf node of either tree, remove it, and use it as the root to the newly created three. In your example: 30 8 9 5 7 4 20 This works because your trees don’t appear to follow any particular order, don’t appear to be balanced, nor have any other constraints. Since any leaf node will do, this is an O ( n ) operation in the worst case (traverse one of the trees in any order until we encounter the first leaf, remove it, add child links to both trees’ roots). The easiest way

Constructing full binary tree given only postorder?

本秂侑毒 提交于 2019-12-02 00:46:01
I'm trying to construct a full binary tree (full meaning that every non leaf node has two leaf nodes connecting to it, i.e. node->right and node->left are != NULL ) given only the postorder traversal of the tree. In addition, I am given whether or not the node in the postorder traversal is a leaf node or not. The given postorder traversal looks like this: 2(1.000000e+00) 3(1.000000e+00) (1.000000e+00 1.000000e+00) 1(1.000000e+00) (1.000000e+00 2.000000e+00) for example, where a line of the format "%d(%le)" is a leaf node and "(%le %le)" is a non-leaf node. Normally you can't construct a tree

How to implement a Complete Binary Tree using recursion without comparing the value of the node?

℡╲_俬逩灬. 提交于 2019-12-01 14:52:52
public void recurInsert(BinaryTree.Node root, BinaryTree.Node newNode, int height) { if (newNode == null) { System.out.println("InsertNode is empty, please create new one"); return; } else{ if (height == 1) { if (root == null) return; else if (root.leftChild == null) { root.leftChild = newNode; System.out.println("left" + newNode.data); } else { root.rightChild = newNode; System.out.println("right" + newNode.data); } } else{ if (root.leftChild != null) recurInsert(root.leftChild, newNode, height-1); //else (root.rightChild != null) // recurInsert(root.rightChild, newNode, height-1); if (root

How to implement a Complete Binary Tree using recursion without comparing the value of the node?

余生颓废 提交于 2019-12-01 13:34:38
问题 public void recurInsert(BinaryTree.Node root, BinaryTree.Node newNode, int height) { if (newNode == null) { System.out.println("InsertNode is empty, please create new one"); return; } else{ if (height == 1) { if (root == null) return; else if (root.leftChild == null) { root.leftChild = newNode; System.out.println("left" + newNode.data); } else { root.rightChild = newNode; System.out.println("right" + newNode.data); } } else{ if (root.leftChild != null) recurInsert(root.leftChild, newNode,

How to add elements in Binary search tree iteratively?

跟風遠走 提交于 2019-12-01 13:00:53
public void Insert(int value) { if (value < Data) { if (LeftNode == null) { LeftNode = new TreeNode(value); } else { LeftNode.Insert(value); } } else if (value > Data) { if (RightNode == null) { RightNode = new TreeNode(value); } else { RightNode.Insert(value); } } } I wrote method to add element in BST recursively, It checks for value to add less than or greater than and add it in its proper place, but I want to know how iterative method works? I need iterative add method for my BST. You can find a implementation in Java at wikipedia, what is very similar C# http://en.wikipedia.org/wiki

Generic data structure libraries for C?

旧时模样 提交于 2019-12-01 10:53:24
Which libraries do you guys use for generic data structures like linked list, binary tree etc.? What are the most common, efficient libraries? Can you name some? Mitch Wheat GDSL - The Generic Data Structures Library Gnulib - The GNU Portability Library GLib SGLIB In C....if you're on linux http://library.gnome.org/devel/glib/2.22/ not sure about windows.....I don't use it :P glib contains a lot of data structures. 来源: https://stackoverflow.com/questions/2159555/generic-data-structure-libraries-for-c

Micro optimisations iterating through a tree in C#

≡放荡痞女 提交于 2019-12-01 10:26:54
I'm working on a massive number crunching project. I've been optimising everything since the start as I knew it would be important. Doing performance analysis my code spends almost 40% of it's life in one function - the binary tree iterator. public ScTreeNode GetNodeForState(int rootIndex, float[] inputs) { 0.2% ScTreeNode node = RootNodes[rootIndex].TreeNode; 24.6% while (node.BranchData != null) { 0.2% BranchNodeData b = node.BranchData; 0.5% node = b.Child2; 12.8% if (inputs[b.SplitInputIndex] <= b.SplitValue) 0.8% node = b.Child1; } 0.4% return node; } Do any C# optimisation experts have

Generic data structure libraries for C?

谁都会走 提交于 2019-12-01 09:29:39
问题 Which libraries do you guys use for generic data structures like linked list, binary tree etc.? What are the most common, efficient libraries? Can you name some? 回答1: GDSL - The Generic Data Structures Library Gnulib - The GNU Portability Library GLib SGLIB 回答2: In C....if you're on linux http://library.gnome.org/devel/glib/2.22/ not sure about windows.....I don't use it :P 回答3: glib contains a lot of data structures. 来源: https://stackoverflow.com/questions/2159555/generic-data-structure

Big O of finding out if a binary tree is balanced (From CTCI Book)

依然范特西╮ 提交于 2019-12-01 09:10:44
In Cracking the Coding Interview 6th Edition there's a question (4.4) where you're suppose to find out if a binary tree is balanced, where balanced in this case means if any side is deeper than the other by more than 1. I solved this recursively like this: def isBalanced(root): return abs(getDepth(root.left) - getDepth(root.right)) > 1 def getDepth(node): if node is None: return 0 return 1 + max([getDepth(node.left), getDepth(node.right)]) So to walk through it. It recursively check each side of each node and pass it up to the root, if the root then have a higher difference between the left