binary-search-tree

Printing out a binary search tree with slashes

我的未来我决定 提交于 2019-12-13 06:03:14
问题 http://pastebin.com/dN9a9xfs That's my code to print out the elements of a binary search tree. The goal is to display it in level order, with slashes connecting the parent to each child. So for instance, the sequence 15 3 16 2 1 4 19 17 28 31 12 14 11 0 would display after execution as: 15 / \ 3 16 / \ \ 2 4 19 / \ / \ 1 12 17 28 / / \ \ 0 11 14 31 I've been working on it for a long time now, but I just can't seem to get the spacing/indentation right. I know I wrote the proper algorithm for

Deleting an node in BST

可紊 提交于 2019-12-13 05:18:33
问题 This is not an homework. I am just totally blocked on this. I know what to do but I am having difficulty manipulating the tree. please help. I am trying to delete and node from an BST. I am able to lookup and find the parent and store it an tree. package com.test.binarytree; public class BinaryTreeDelete { private Node root; //create null binary tree public BinaryTreeDelete(){ root = null; } //delete public void delete(int target){ root = delete(root, target); } public Node delete(Node node,

How to know if a binary tree is ordered scheme

左心房为你撑大大i 提交于 2019-12-13 04:59:29
问题 A binary tree is ordered if all its children are under the left branch that the data of the root and branch of the right age and, in turn, binary trees of the left and right branches are also sorted. Write the procedure (ordered-btree? btree) receiving a binary tree as argument and returns True if ordered and false if not. How can I do this? 回答1: There are several ways to solve this problem. I'll assume that there are no repeated elements in the tree and that you have written helper

Pseudo code to check if binary tree is a binary search tree - not sure about the recursion

浪子不回头ぞ 提交于 2019-12-13 04:38:51
问题 I have homeowork to write pseudo code to check if a valid binary tree is a search binary tree. I created an array to hold the in-order values of the tree. if the in-order values are in decreasing order it means it is indeed BST. However I've got some problem with the recursion in the method InOverArr. I need to update the index of the array in order to submit the values to the array in the order they are at the tree. I'm not sure the index is really updated properly during the recursion.. is

How to check if my binary search Tree is complete? (Haskell)

人盡茶涼 提交于 2019-12-13 03:15:36
问题 data BTree a = Nil | Node a (BTree a) (BTree a) deriving Show I learned about two binary search trees. One is perfect the other one is complete. A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all leaves are at same level. A Binary Tree is complete Binary Tree if all levels are completely filled except possibly the last level and the last level has all keys as left as possible The code to check if a binary tree is perfect is pretty easy isPerfect ::

Right Threading a Binary Tree

喜夏-厌秋 提交于 2019-12-13 03:09:31
问题 I'm having a hell of a time trying to figure this one out. Everywhere I look, I seem to be only running into explanations on how to actually traverse through the list non-recursively (the part I actually understand). Can anyone out there hammer in how exactly I can go through the list initially and find the actual predecessor/successor nodes so I can flag them in the node class? I need to be able to create a simple Binary Search Tree and go through the list and reroute the null links to the

What is the optimal way to find common nodes in two BST

纵然是瞬间 提交于 2019-12-13 03:06:46
问题 I am looking for the most optimal in terms of complexity(space and time). My approach until now is: Traverse one tree in-order and for each nodeId, search that nodeId in second tree. Node structure: struct node{ long long nodeId; node *left; node *right; }; Please let me know if any doubts about the question. 回答1: Assuming one tree is n long and the other is m long, your approach is O(n*m) long, while you could do it in O(n+m) . Let us say you have to pointers to where you are in each tree (

Height of binary search tree iteratively

╄→尐↘猪︶ㄣ 提交于 2019-12-12 18:17:19
问题 I was trying out an iterative method to find the height/depth of a binary search tree. Basically, I tried using Breadth First Search to calculate the depth, by using a Queue to store the tree nodes and using just an integer to hold the current depth of the tree. Each node in the tree is queued, and it is checked for child nodes. If child nodes are present, then the depth variable is incremented. Here is the code: public void calcDepthIterative() { Queue<TreeNode> nodeQ = new LinkedList

c++ pointers and pointer to a reference

ⅰ亾dé卋堺 提交于 2019-12-12 14:55:42
问题 Im trying to create a Binary search tree. I've used recursive procedures to insert nodes into the tree. The code is as follows. void BST :: insertRoot(Node* node, int data) { if (node == NULL) this -> root = new Node(data); else insertOthers(node, data); } void BST :: insertOthers(Node* node, int data) { if(node == NULL) { node = new Node(data); return; } if(data < node->getData()) insertOthers(node->getLeft(), data); else insertOthers(node->getRight(), data); } In this code only one node is

Prolog binary search tree test - unwanted parents' parent node comparison

白昼怎懂夜的黑 提交于 2019-12-12 13:28:28
问题 I'm a Prolog rookie, please keep that in mind. I try to write a predicate to determine if some given term is a binary search tree. I figured out this code: is_btree(nil). is_btree(node(N,L,R)) :- number(N), is_btree(L), is_btree(R), small(N, R), big(N, L). small(N, nil). small(N, node(M,L,R)) :- N < M, small(N, L), small(N, R). big(N, nil). big(N, node(M,L,R)) :- N > M, big(N, L), big(N, R). It works quite fine until I test a graph that has a node on the right side which passes the condition