Binary Tree

A binary Tree is a Tree data structure. For making a binary tree, there must be a Root node, and the root node must have at least two child nodes. Every tree node can have a maximum of two children nodes.

Binary Tree

On the above image, the Root node has two child nodes. and each node has its child nodes. For a binary tree, we mention child nodes as left-child and right-child. Child nodes are considered as different levels. Direct children nodes of the Root are at level one. Then their direct children nodes are at level two and so on.

Now let’s create a binary tree using JavaScript…

class Dom {
	constructor(val) {
		this.val = val
		this.left = null
		this.right = null
	}
}

const dom = new Dom('html')
const head = new Dom('head')
const meta = new Dom('meta')
const body = new Dom('body')
const container = new Dom('container')
const footer = new Dom('footer')

dom.left = head
dom.right = body
head.left = meta
body.left = container
body.right = footer

// ** visual structure:
//		    html
//	 	  /		  \
//	   head	 	   	body
//	  /	  		  /	      \
//  meta  	  container	   footer
0 0 votes
Article Rating