Binary search tree questions. Please help =)


 
Thread Tools Search this Thread
Top Forums Programming Binary search tree questions. Please help =)
# 1  
Old 12-11-2013
Binary search tree questions. Please help =)

I have some questions about certain placement of child nodes since I'm just learning BSTs and it's quite confusing even after reading some sources and doing some online insertion applets. Let's say I want to add nodes 5,7,3,4 to an empty basic BST.

Image


Ok I understand that the left child must be less than the parent AND less than or equal to the right child from that same parent. I follow it until we add the 4 node. How do we determine that the insertion of 4 goes to the bottom right leaf position of 3 instead of the bottom left leaf position? Also, doing a AVL insertion of nodes 5,18,3,7,11 yielded some surprising position placements. Inserting the fourth node, 7, went down through 18 instead of 3. Is there a particular reason why? Assuming that is the correct way, inserting 11 would switch the 11 and 18 spots, but wouldn't having 18 as the parent node, 7 as left child, and 11 as right child adhere to the principle of left child smaller than parent and smaller or equal to right child? I'm confused! I would appreciate any help. Thank you!

insert 7

Image

insert 11

Image
# 2  
Old 12-20-2013
Quote:
Originally Posted by Jill Ceke
I have some questions about certain placement of child nodes since I'm just learning BSTs and it's quite confusing even after reading some sources and doing some online insertion applets. Let's say I want to add nodes 5,7,3,4 to an empty basic BST.

Image
What language are you using...
Quote:
Originally Posted by Jill Ceke
Ok I understand that the left child must be less than the parent AND less than or equal to the right child from that same parent. I follow it until we add the 4 node. How do we determine that the insertion of 4 goes to the bottom right leaf position of 3 instead of the bottom left leaf position?
By traversing the binary tree and comparing the value at each node with 4...and in C there is a thing called recursion which you can use for traversing the tree from the top down.
Quote:
Originally Posted by Jill Ceke
Also, doing a AVL insertion of nodes 5,18,3,7,11 yielded some surprising position placements. Inserting the fourth node, 7, went down through 18 instead of 3. Is there a particular reason why?
Yes because that is the way b-tree works. The value 7 is first compared to the root node ie 5 and since 7 > 5 the right hand side of the tree is chosen. Thereafter 7 is compared to the right child ie 18 and since 7 < 18 its gets installed in the empty slot for the left child and so on and so forth...
Quote:
Originally Posted by Jill Ceke
Assuming that is the correct way, inserting 11 would switch the 11 and 18 spots,
No it would not since in b-tree only new nodes are installed but the occupied nodes are not replaced...
Quote:
Originally Posted by Jill Ceke
but wouldn't having 18 as the parent node, 7 as left child, and 11 as right child adhere to the principle of left child smaller than parent and smaller or equal to right child?
Yes your understanding is correct...
Quote:
Originally Posted by Jill Ceke
I'm confused! I would appreciate any help. Thank you!

insert 7

Image

insert 11

Image
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Problem in printing binary tree using php and mysql

Database Structure Root Table ID Root_ Node Level 1 A 0 2 B 1 3 C 1 Child Table ID Left_Node Right_Node Root_Node Root_ID 1 B C A 1 ... (1 Reply)
Discussion started by: Deepak Tiwari
1 Replies

2. UNIX for Dummies Questions & Answers

Binary search and replace

Hello again. I have two problems - is it possible to solve them? 1. I want to replace a few bytes after specific hex-string. i.e.: I want to replace two bytes after AA AB AC: AA AB AC 00 00 AA AA AA so the expected result should be: AA AB AC FF FF AA AA AA 2. I want to replace three bytes... (9 Replies)
Discussion started by: useretail
9 Replies

3. UNIX for Dummies Questions & Answers

To do directory tree search

Hello Everyone, I need to find the file / directory with the maximum timestamp in a directory tree having many files / directories. Could you please help. Thanks, H squared (3 Replies)
Discussion started by: H squared
3 Replies

4. Programming

Binary Search Tree Search problem

I am writing code for a binary search tree search and when I compile it i am getting strange errors such as, " /tmp/ccJ4X8Xu.o: In function `btree::btree()': project1.cpp:(.text+0x0): multiple definition of `btree::btree()' " What does that mean exactly? tree.h #ifndef TREE_H #define... (1 Reply)
Discussion started by: meredith1990
1 Replies

5. Programming

Binary Tree

I have just been researching this topic and I was wondering what type of application might a binary tree be used for. For instance what type of application would be a good showcase for a binary tree that I could write as an example? (5 Replies)
Discussion started by: sepoto
5 Replies

6. Shell Programming and Scripting

How to search for string and return binary result?

Hi, I have a problem that I am sure someone will know the answer to. Currently I have a script which returns a binary output if it finds a certain search string (in this case relating to a DRBD cluster) as follows: searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate" && echo... (3 Replies)
Discussion started by: almightybunghol
3 Replies

7. UNIX for Dummies Questions & Answers

Binary Tree Creation Using fork()

Hi, I am working on a program and kind of a stuck,nt getting it done. "The program should take one command line arguments: number of hierarchy level. The hierarchy of your program should of that level and each node have two child processes." Can anyone give me the C code using fork() of this... (1 Reply)
Discussion started by: learneros
1 Replies

8. Shell Programming and Scripting

Create a binary tree

I need to create a binary tree like structure of directories using shell script... does anyone know of any algorithm for this ? i tried doing a recursive algorithm function CreateDir { level=$1 dirname=$2 mkdir $dirname/sub1/ mkdir $dirname/sub2/ let level=level-1 ... (2 Replies)
Discussion started by: macvijay1985
2 Replies

9. Shell Programming and Scripting

Creating breadth traversal binary tree

I almost have the entire script written. however the problem is how would i assign the global variable to terminate the process from the bottom up to ensure the child terminates so the parent can. ex. I am proccess 1 I am proccess 2 etc Here is the code $ cat tree.c ... (3 Replies)
Discussion started by: slurpeyatari
3 Replies

10. Programming

Directory tree search???

Hi all, I've got a problem, what function do i use to list the contents of all the directory tree (simular to "find")? Any other suggestions? Thank you all (3 Replies)
Discussion started by: solvman
3 Replies
Login or Register to Ask a Question