binary-search-tree

Return parent of node in Binary Tree

痞子三分冷 提交于 2019-12-07 20:36:32
I'm writing a code to return the parent of any node, but I'm getting stuck. I don't want to use any predefined ADTs. //Assume that nodes are represented by numbers from 1...n where 1=root and even //nos.=left child and odd nos=right child. public int parent(Node node){ if (node % 2 == 0){ if (root.left==node) return root; else return parent(root.left); } //same case for right } But this program is not working and giving wrong results. My basic algorithm is that the program starts from the root checks if it is on left or on the right . If it's the child or if the node that was queried else ,

Method to Display a Splay Tree

心已入冬 提交于 2019-12-07 16:03:26
I have built a splay tree and I am trying to print it out reverse in order so that when you turn your head to the left you can see the tree in a normal manner. I have written the following code and it outputs the tree somewhat correctly but it adds extra spaces in on the rightmost node and it doesn't add spaces for all of the child nodes that should be placed below the root node: public void printReverseInOrder() { if (root != null) { reverseInOrder(root, 0); } else { System.out.println(); } } public void reverseInOrder(BSTnode h, int indent) { if (h != null) { for (int i = 0; i < indent; i++)

Algorithmic improvement for finding minimum sum in a Binary Search Tree

南楼画角 提交于 2019-12-07 12:39:08
问题 I wrote the following function to find out the minimum sum of any path in a Binary Search Tree: int minSumPath(TreeNode* root) { if(root==NULL) return 0; int sum = root->value; if(root->left!=NULL && root->right!=NULL) sum += min(minSumPath(root->left),minSumPath(root->right)); else if(root->left==NULL) sum += minSumPath(root->right); else sum += minSumPath(root->left); return sum; } While the above code generates the correct output, I feel that I am not leveraging the fact that it is a

delete node in binary search tree python

a 夏天 提交于 2019-12-07 05:42:04
问题 The code below is my implement for my binary search tree, and I want to implement delete method to remove the node. Below is my implementation, but when I perform bst = BSTRee() bst.insert(5) bst.insert(11) bst.insert(3) bst.insert(4) bst.insert(12) bst.insert(2) bst.delete(3) when I call delete method, it did nothing. Can someone help me to fix it. The link below is my code on github. Thank you so much for your help. https://github.com/hly189/sort/blob/master/tree/BST.py class BSTreeNode def

Recursive insertion of BST

拈花ヽ惹草 提交于 2019-12-07 04:58:49
问题 I have made a function for insertion in BST using loops and it is working perfectly fine. Now, when iam writing to do it using recursion i don't know why it's not working properly, however the logic is correct according to me. It seems that no newnode is being added to the BST tree and head of the tree after coming out of the insertion function is again becoming NULL. #include <iostream> using namespace std; class node{ public: int data; node *right; node *left; node(){ data=0; right=NULL;

In Big-O notation for tree structures: Why do some sources refer to O(logN) and some to O(h)?

余生颓废 提交于 2019-12-06 19:01:05
问题 In researching complexity for any algorithm that traverses a binary search tree, I see two different ways to express the same thing: Version #1: The traversal algorithm at worst case compares once per height of the tree; therefore complexity is O(h) . Version #2: The traversal algorithm at worst case compares once per height of the tree; therefore complexity is O(logN) . It seems to me that the same logic is at work, yet different authors use either logN or h . Can someone explain to me why

Printing Successor and Predecessor in a BST

[亡魂溺海] 提交于 2019-12-06 17:30:30
My problem is as follows: I have a binary search tree with keys: a1<a2<...<an , the problem is to print all the (a_i, a_i+1) pairs in the tree (where i={1,2,3,...}) using a recursive algorithm in O(n) time without any global variable and using O(1) extra space. An example: Let the keys be: 1,2, ..., 5 Pairs that will be printed: (1,2) (2,3) (3, 4) (4, 5) So you can't do inorder traversal in the tree and find the successor/predecessor for each node. Because that would take O(nh) time and if the tree is balanced, it will be O(nlgn) for the whole tree. Although you are right that finding an in

Haskell label a binary tree through depth-first in-order traversal

╄→尐↘猪︶ㄣ 提交于 2019-12-06 16:10:43
问题 I need to label a binary tree through depth-first in-order traversal and I figured therefore I'd need to first go through the left branches of the tree and label those, then do the same for the right branches. My binary tree only stores values at the internal nodes (not at end nodes / leaves): label :: MonadState m Int => Tree a -> m (Tree (Int, a)) label (Branch l x r) = do n <- get l' <- label l r' <- label r return (Branch l' (n, x) r') label (Leaf) = return Leaf *EDIT: Need to use the

Understanding stack unwinding in recursion (tree traversal)

本秂侑毒 提交于 2019-12-06 12:07:42
I am writing a program to traverse a binary search tree.Here's my code: Main.java public class Main { public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(); binaryTree.add(50); binaryTree.add(40); binaryTree.add(39); binaryTree.add(42); binaryTree.add(41); binaryTree.add(43); binaryTree.add(55); binaryTree.add(65); binaryTree.add(60); binaryTree.inOrderTraversal(binaryTree.root); } } Node.java public class Node { int data; Node left; Node right; Node parent; public Node(int d) { data = d; left = null; right = null; } } BinaryTree.java public class BinaryTree { Node

How can a node X without right child have a successor?

喜你入骨 提交于 2019-12-06 11:42:13
I have a trouble understanding what's a successor of a node X whenever it doesn't have a right child. From what I had understood, if a node X had no right child, then it would not have a successor. But my textbook says the following: If the right sub-tree of node X is empty and X has a successor Y ... How can X have a successor when it has not right child? The successor is simply the next element in the ordered sequence; it doesn't necessarily have to be a child element. For example, the successor of 5 below is 7 : 7 / \ 5 8 Manali The successor of a node X is the smallest greater element S in