|
| 1 | +A tree is a data structure where a node can have zero or more children. Each node contains a value, and similar to graphs each node can have a connection between other |
| 2 | +nodes called an edge. A tree is a type of graph but not all graphs are trees. |
| 3 | + |
| 4 | +The top-most node is called the root. The DOM, or document object model, is a tree data structure. |
| 5 | + |
| 6 | +A node without children is called a leaf node. |
| 7 | + |
| 8 | +The height of the tree is the distance between the farthest leaf node and the root node. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## Binary Trees |
| 13 | + |
| 14 | +Binary trees are a special type of tree in which each node can only have a maximum of two children: a left child and a right child. |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Binary Search Tree |
| 19 | + |
| 20 | +```js |
| 21 | +// Node class |
| 22 | +class Node |
| 23 | +{ |
| 24 | + constructor(data) |
| 25 | + { |
| 26 | + this.data = data; |
| 27 | + this.left = null; |
| 28 | + this.right = null; |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +As in the above code snippet we define a node class having three property data, left and right, Left and right are pointers to the left and right node in a Binary Search Tree. Data is initialized with data which is passed when object for this node is created and left and right is set to null. |
| 34 | + |
| 35 | +Now let’s see an example of a Binary Search Tree class. |
| 36 | + |
| 37 | +```js |
| 38 | +// Binary Search tree class |
| 39 | +class BinarySearchTree |
| 40 | +{ |
| 41 | + constructor() |
| 42 | + { |
| 43 | + // root of a binary search tree |
| 44 | + this.root = null; |
| 45 | + } |
| 46 | + |
| 47 | + // function to be implemented |
| 48 | + // insert(data) |
| 49 | + // remove(data) |
| 50 | + |
| 51 | + |
| 52 | + // Helper function |
| 53 | + // findMinNode() |
| 54 | + // getRootNode() |
| 55 | + // inorder(node) |
| 56 | + // preorder(node) |
| 57 | + // postorder(node) |
| 58 | + // search(node, data) |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +The above example shows a framework of a Binary Search tree class, which contains a private variable root which holds the root of a tree, it is initialized to null. |
| 63 | + |
| 64 | +Now lets implement each of this function: |
| 65 | + |
| 66 | +**1. insert(data)** – It inserts a new node in a tree with a value data |
| 67 | + |
| 68 | +```js |
| 69 | +// helper method which creates a new node to |
| 70 | +// be inserted and calls insertNode |
| 71 | +insert(data) |
| 72 | +{ |
| 73 | + // Creating a node and initialising |
| 74 | + // with data |
| 75 | + var newNode = new Node(data); |
| 76 | + |
| 77 | + // root is null then node will |
| 78 | + // be added to the tree and made root. |
| 79 | + if(this.root === null) |
| 80 | + this.root = newNode; |
| 81 | + else |
| 82 | + |
| 83 | + // find the correct position in the |
| 84 | + // tree and add the node |
| 85 | + this.insertNode(this.root, newNode); |
| 86 | +} |
| 87 | + |
| 88 | +// Method to insert a node in a tree |
| 89 | +// it moves over the tree to find the location |
| 90 | +// to insert a node with a given data |
| 91 | +insertNode(node, newNode) |
| 92 | +{ |
| 93 | + // if the data is less than the node |
| 94 | + // data move left of the tree |
| 95 | + if(newNode.data < node.data) |
| 96 | + { |
| 97 | + // if left is null insert node here |
| 98 | + if(node.left === null) |
| 99 | + node.left = newNode; |
| 100 | + else |
| 101 | + |
| 102 | + // if left is not null recur until |
| 103 | + // null is found |
| 104 | + this.insertNode(node.left, newNode); |
| 105 | + } |
| 106 | + |
| 107 | + // if the data is more than the node |
| 108 | + // data move right of the tree |
| 109 | + else |
| 110 | + { |
| 111 | + // if right is null insert node here |
| 112 | + if(node.right === null) |
| 113 | + node.right = newNode; |
| 114 | + else |
| 115 | + |
| 116 | + // if right is not null recur until |
| 117 | + // null is found |
| 118 | + this.insertNode(node.right,newNode); |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
0 commit comments