binary-search-tree

What are “marker for NULL pointers” in binary tree?

廉价感情. 提交于 2019-12-11 10:05:29
问题 I was going through this and this post about binary search tree implementation. I saw that a binary search tree is represented as (for example) : 1 5 7 10 40 50 I was trying to learn about the serialization or de-serialization of the same here. The blog post is making me crazy with those -1 s which they're calling markers for NULL pointers . And they're representing the tree as: 20 8 4 -1 -1 12 10 -1 -1 14 -1 -1 -1 Confusion What are those -1 s? My final goal is to store and read a binary

Inorder Tree Traversal algorithm for a Binary Search Tree using Stack

僤鯓⒐⒋嵵緔 提交于 2019-12-11 08:30:20
问题 My inputs results 24, 4, 2, 3, 9, 10, 32 , and I am getting following result 2, 3, 4, 24 . I am using a stack . When I have manually checked this program the node is not going through else if at 4 on stack, even if has right sub tree. public void inorderNonRcursive(Node root){ Stack s = new Stack(); Node node = root; Node k; int c=0; if(node != null) { s.push(node); } while(!s.stack.isEmpty()) { //node=(Node) s.stack.get(s.stack.size()-1); System.out.println("first condition" + (node.getleft(

Save/load contents of binary search tree

走远了吗. 提交于 2019-12-11 08:08:07
问题 So I've looked around a lot for some information on how to take elements from a binary search tree and write them to a file, and then later read them back into the tree as a type of "load" function. I've found a few ways that people go about it, but I am having issues implementing those suggestions such as the one seen here, into my program itself. So I have several sort methods, inOrder, postOrder, and preOrder, but don't necessarily need to write to the file every time one of those methods

Why is add in unbalanced Binary Search Tree O(n)?

空扰寡人 提交于 2019-12-11 06:54:56
问题 This is the implementation of add in Binary Search Tree from BST Add private IntTreeNode add(IntTreeNode root, int value) { if (root == null) { root = new IntTreeNode(value); } else if (value <= root.data) { root.left = add(root.left, value); } else { root.right = add(root.right, value); } return root; } I understand why this runs in O(log n). Here's how I analyze it. We have a tree size of n. How many cuts of 2, or half cut, will reduce this tree down to a size of 1. So we have the

C - freeing memory of a binary tree using post-order traversal

送分小仙女□ 提交于 2019-12-11 06:07:28
问题 I want to remove a binary tree using post-order traversal . Meaning the left part of the tree should be removed first then the right one then remove the whole tree and free memory in a second function that follows after. I'm not allowed to changed the arguments of the function and can only play with the inside of it: #include <stdio.h> #include <stdlib.h> #include <string.h> #include "telefonbuch.h" static inline bstree * create_node(unsigned long phone, char * name) { bstree * newNode =

C binary search tree implementation - insert

独自空忆成欢 提交于 2019-12-11 05:52:31
问题 I am trying to create a program which which takes a list of words as an input, and sorts them into a binary tree, in order to allow them to be found, e.g. like a dictionary. This is what I have done so far, but am getting a segmentation error with newEl -> el = input; I know this is because it is trying to point to a NULL el, when the tree is first created, but i'm not sure what the best way to improve my code would be. Anyone have any ideas? Thanks. struct node *tra(struct node * start, Type

Ordered Binary Tree of Strings

本小妞迷上赌 提交于 2019-12-11 05:46:31
问题 These values are entered into an ordered binary tree: Mercury , Venus , Earth , Mars , Jupiter , Saturn and Uranus . The resulting binary tree is supposed to be this. Mercury / \ Earth Venus \ / Jupiter Saturn \ \ Mars Uranus Is there any reason for this order? Shouldn't Jupiter be under the Venus branch? 回答1: By "ordered binary tree", I assume you mean a binary search tree. As long as the tree satisfies the following criteria: 1. The key in a node is greater than (or equal to) any key stored

How binary search tree is created?

自古美人都是妖i 提交于 2019-12-11 05:33:36
问题 Suppose i am having an array say 1 5 4 6 8 9 10 22 17 7 9 3 I want to create a binary search tree from this array. I need algorithm to understand that. I have read rest other things related to BST like inorder traversal preorder postorder , tree walk , insertion deletion etc Book has not provided how to create BST. Need help here 回答1: if you do not care about the tree being balanced it is simple: put the first element of the tree as the head. iterate over the array. if an element is bigger

Check if a tree is a Binary Search Tree (BST)

ぐ巨炮叔叔 提交于 2019-12-11 05:17:27
问题 I am trying to solve a binary search tree problem, but I can't pass all of the test cases. I need to return true if the tree is a binary search tree, otherwise, I need to return false. Can anyone tell me what I am doing wrong? ''' class node: def __init__(self, data): self.data = data self.left = None self.right = None ''' def checkBST(root): if root.left == None and root.right == None: return True if root == None: return True if root.left != None: if root.left.data < root.data: return True

Given a pre-order binary tree visit construct a binary search tree with the same pre-order visit. (if possible)

不羁的心 提交于 2019-12-11 04:49:23
问题 Im trying to solve this problem:" A binary tree is given, check his pre-order visit and build a binary search tree with the same pre-order visit. Demonstrate if it is always possible, if not give an example when this is not possible." Any help? I need to write pseudocode and give time complexity but i have a lot of doubts about building a binary-search-tree with the same pre-order visit for every possible binary tree. 回答1: If you are using the classic algorithm for inserting in a binary