binary-tree

Non-recursive add function in a binary tree using c++

断了今生、忘了曾经 提交于 2019-12-06 14:46:03
I am writing an Add function to add nodes to a binary tree non recursively. I have run into the problem of only being able to produce one level deep binary tree. I debugged it and I know where the problem is but can't figure out how to fix it. Maybe fresh pair of eyes will see something i dont... The problem is that my temp node is being reset to the root value every new function call and hence, adding nodes linearly. Anyways, here is the function: void BinaryTreeNonRec::add(int num){ treeNode *temp, *parent; if (root==NULL) { root = new treeNode; root->data=num; root->left=0; root->right=0;

Tree iterator, can you optimize this any further?

南楼画角 提交于 2019-12-06 14:39:44
问题 As a follow up to my original question about a small piece of this code I decided to ask a follow up to see if you can do better then what we came up with so far. The code below iterates over a binary tree (left/right = child/next ). I do believe there is room for one less conditional in here (the down boolean). The fastest answer wins! The cnt statement can be multiple statements so lets make sure this appears only once The child() and next() member functions are about 30x as slow as the

Binary Tree from inorder and postorder

一曲冷凌霜 提交于 2019-12-06 13:49:27
I have just started studying Binary Tree. Is there an algorithm to find out the binary tree structure,given the Inorder and Postorder OR Inorder and Preorder? I have been trying to do it manually,but it never comes out correct.For eg.-These two are valid Inorder and Postorder traversal of a given tree: Inorder: D B F E A G C L J H K Postorder : D F E B G L J K H C A Clearly A is the root as it is the last element in Postorder. Now looking in Inorder,the left subtree becomes: {D B F E} and right subtree becomes: {G C L J H K}. The root of right subtree would be the second last element in

Creating a Binary Tree for a Knockout Tournament

人盡茶涼 提交于 2019-12-06 13:45:44
I am trying to create a binary tree for use in a knockout tournament. The tree consists of TNodes with Left and Right pointers. This is the code that I have come up with (below); however, it runs into difficulties with the pointers in the CreateTree section. Once this creates an empty tree of large enough size, I need to add the names on the Memo1.List to the bottoms of the tree so I can pair them up for matches. How would I do this? Type TNodePtr = ^TNode; TNode = Record Data:String; Left:TNodePtr; Right:TNodePtr; end; Type TTree = Class Private Root:TNodePtr; Public Function GetRoot:TNodePtr

Modifying terminal node in ctree(), partykit package

岁酱吖の 提交于 2019-12-06 12:34:18
问题 I have a dependent variable to classify by a decision tree. It's composed by three categories of frequences: 738 (19%), 426 (15%) and 1800 (66%). As you imagine the predicted category is always the third one, but the purpose of the tree is descriptive so it does not actually matter. The thing is, when plotting a tree by the ctree() function (package partykit ) the terminal nodes display histograms showing the probability of occurrence of the three classes. I need to modify this output: I

Understanding the logic in iterative Postorder traversal implementation on a Binary tree

假如想象 提交于 2019-12-06 12:30:24
I was trying to understand how it is so intuitive to implement postorder traversal using 2 stacks. How did someone come up with it, is it just an observation or some particular way of thinking which helps one come up with such methods. If yes then please explain how to think in the right direction. Let me explain how I stumbled on the solution: You start off with this simple observation: prima facie, the pre-order and post-order traversal differ only in their order of operations : preOrder(node): if(node == null){ return } visit(node) preOrder(node.leftChild) preOrder(node.rightChild)

Generate All Possible Trees

南楼画角 提交于 2019-12-06 10:27:44
问题 Given the following data type definition: data FormTree = Empty | Node FormTree FormTree deriving Show I want to write a function which generates an infinite list containing all possible trees sorted after length e.g. the amount of nodes. The following code almost does what I need but it only descends the tree on the right side by inserting additional nodes every time but I need it to alternate between both sides. allPossibleTrees :: [FormTree] allPossibleTrees = Empty : [Node x y | x <-

Generic binary tree node destructor issue

淺唱寂寞╮ 提交于 2019-12-06 09:25:38
I've been working on an assignment and now I'm stuck with buggy destructors. I have to create a generic binary tree with all the usual member functions and some special operators. There's also a restriction: everything must work iteratively so no nasty recursive hacks this time. There is obviously something very wrong with the destructor of BinTreeNode class because if I delete the node like this: BinTreeNode<int> * node = new BinTreeNode<int>(); delete node; I can still access its data: node->getData(); //should fail miserably so deletion has no effect but I have no usable idea how I should

Construct binary tree from inorder and level traversal

谁说我不能喝 提交于 2019-12-06 08:25:35
Need help finding a way to construct a binary tree given the inorder and level traversal. Is it possible to do it using recursion since the level traversal has to be done by using a queue? Here's how you can approach this problem. It's easier to think of how to approach each step by looking from reverse: 8 / \ 4 9 / \ \ 2 6 10 / 1 You have the following: Inorder: 1 2 4 6 8 9 10 Level: 8 4 9 2 6 10 1 Level 1 - Root A level traversal is a left to right, top to down traversal of the tree (like breadth-first search). In this example, you know that 8 will be the root node. Now looking at the

Can a red node have just 1 black child in a red-black tree?

二次信任 提交于 2019-12-06 07:45:23
The rules for a Red-Black Tree: Every node is either red or black. The root is black. Every leaf (NIL) is black. If a node is red, then both its children are black. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes. Rule 4 mentions that red nodes need both black childs but what if there is just one child to begin with? Is there an argument to prove or disprove this? No,a red node cannot have one child,consider the following cases:- 1.If the single child it has is red...this cannot happen because no two consecutive nodes can be red. 2.If