What is Binary Tree in Data Structures ?

Answer : Binary tree is a special type of data structure…

What is Binary Tree in Data Structures ?

  • Binary tree is a special type of data structure. In binary tree, every node can have a maximum of 2 children, which are known as Left child and Right Child.
  • It is a method of placing and locating the records in a database, especially when all the data is known to be in random access memory (RAM).
  • binary tree has the benefits of both an ordered array and a linked list as search is as quick as in a sorted array and insertion or deletion operation are as fast as in linked list.
What is Binary Tree in Data Structures

Representation of Binary Tree using Array

  • Binary tree using array represents a node which is numbered sequentially level by level from left to right. Even empty nodes are numbered.

Sample Code

[pastacode lang=”javascript” manual=”%2F%2F%20JAVA%20implementation%20of%20tree%20using%20array%20%0A%2F%2F%20numbering%20starting%20from%200%20to%20n-1.%20%0Aimport%20java.util.*%3B%0Aimport%20java.lang.*%3B%0Aimport%20java.io.*%3B%0A%0Aclass%20bTree%20%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20btree_Array%20obj%20%3D%20new%20btree_Array()%3B%0A%20%20%20%20%20%20%20%20obj.Root(%22P%22)%3B%0A%20%20%20%20%20%20%20%20%2F%2F%20obj.set_Left(%22Q%22%2C%200)%3B%20%0A%20%20%20%20%20%20%20%20obj.set_Right(%22R%22%2C%200)%3B%0A%20%20%20%20%20%20%20%20obj.set_Left(%22S%22%2C%201)%3B%0A%20%20%20%20%20%20%20%20obj.set_Right(%22T%22%2C%201)%3B%0A%20%20%20%20%20%20%20%20obj.set_Left(%22U%22%2C%202)%3B%0A%20%20%20%20%20%20%20%20obj.print_Tree()%3B%0A%20%20%20%20%7D%0A%7D%0A%0Aclass%20btree_Array%20%7B%0A%20%20%20%20static%20int%20root%20%3D%200%3B%0A%20%20%20%20static%20String%5B%5D%20s%20%3D%20new%20String%5B10%5D%3B%0A%0A%20%20%20%20%2F*create%20root*%2F%0A%20%20%20%20public%20void%20Root(String%20key)%20%7B%0A%20%20%20%20%20%20%20%20s%5B0%5D%20%3D%20key%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F*create%20left%20son%20of%20root*%2F%0A%20%20%20%20public%20void%20set_Left(String%20key%2C%20int%20root)%20%7B%0A%20%20%20%20%20%20%20%20int%20t%20%3D%20(root%20*%202)%20%2B%201%3B%0A%0A%20%20%20%20%20%20%20%20if%20(s%5Broot%5D%20%3D%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.printf(%22Can’t%20set%20child%20at%20%25d%2C%20no%20parent%20found%5Cn%22%2C%20t)%3B%0A%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20s%5Bt%5D%20%3D%20key%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F*create%20right%20son%20of%20root*%2F%0A%20%20%20%20public%20void%20set_Right(String%20key%2C%20int%20root)%20%7B%0A%20%20%20%20%20%20%20%20int%20t%20%3D%20(root%20*%202)%20%2B%202%3B%0A%0A%20%20%20%20%20%20%20%20if%20(s%5Broot%5D%20%3D%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.printf(%22Can’t%20set%20child%20at%20%25d%2C%20no%20parent%20found%5Cn%22%2C%20t)%3B%0A%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20s%5Bt%5D%20%3D%20key%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20void%20print_Tree()%20%7B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%2010%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(s%5Bi%5D%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(s%5Bi%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(%22-%22)%3B%0A%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”javascript” manual=”Can’t%20set%20child%20at%203%2C%20no%20parent%20found%0ACan’t%20set%20child%20at%204%2C%20no%20parent%20found%0AP-R–U—-” message=”” highlight=”” provider=”manual”/]
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like