Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- class Node {
- constructor(data) {
- this.data = data;
- this.left = null;
- this.right = null;
- }
- }
- class BST {
- constructor() {
- this.root = null;
- }
- insert(data) {
- const newNode = new Node(data);
- if(this.root === null) {
- this.root = newNode;
- } else {
- this._insertNode(this.root, newNode);
- }
- }
- _insertNode(node, newNode) {
- // console.log('node', node);
- if(node.data > newNode.data) {
- if(node.left !== null) {
- return this._insertNode(node.left, newNode);
- } else {
- node.left = newNode
- }
- } else {
- if (node.right !== null) {
- return this._insertNode(node.right, newNode)
- } else {
- node.right = newNode
- }
- }
- }
- search(node, data) {
- if(node === null) {
- return 0;
- }
- if (node.data === data) {
- return 1;
- }
- if(node.data > data) {
- return this.search(node.left, data);
- } else {
- return this.search(node.right, data);
- }
- }
- }
- function main(value){
- const bst1 = new BST();
- bst1.insert(1);
- bst1.insert(3);
- bst1.insert(2);
- const bst2 = new BST();
- bst2.insert(3);
- bst2.insert(2);
- bst2.insert(1);
- const bst3 = new BST();
- bst3.insert(3);
- bst3.insert(1);
- bst3.insert(2);
- const bst4 = new BST();
- bst4.insert(2);
- bst4.insert(1);
- bst4.insert(3);
- const bst5 = new BST();
- bst5.insert(1);
- bst5.insert(2);
- bst5.insert(3);
- const result = bst1.search(bst1.root, value) +
- bst2.search(bst2.root, value) +
- bst3.search(bst3.root, value) +
- bst4.search(bst4.root, value) +
- bst5.search(bst5.root, value);
- return result;
- }
- console.log(main(3));
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">class Node {
- constructor(data) {
- this.data = data;
- this.left = null;
- this.right = null;
- }
- }
- class BST {
- constructor() {
- this.root = null;
- }
- insert(data) {
- const newNode = new Node(data);
- if(this.root === null) {
- this.root = newNode;
- } else {
- this._insertNode(this.root, newNode);
- }
- }
- _insertNode(node, newNode) {
- // console.log('node', node);
- if(node.data > newNode.data) {
- if(node.left !== null) {
- return this._insertNode(node.left, newNode);
- } else {
- node.left = newNode
- }
- } else {
- if (node.right !== null) {
- return this._insertNode(node.right, newNode)
- } else {
- node.right = newNode
- }
- }
- }
- search(node, data) {
- if(node === null) {
- return 0;
- }
- if (node.data === data) {
- return 1;
- }
- if(node.data > data) {
- return this.search(node.left, data);
- } else {
- return this.search(node.right, data);
- }
- }
- }
- function main(value){
- const bst1 = new BST();
- bst1.insert(1);
- bst1.insert(3);
- bst1.insert(2);
- const bst2 = new BST();
- bst2.insert(3);
- bst2.insert(2);
- bst2.insert(1);
- const bst3 = new BST();
- bst3.insert(3);
- bst3.insert(1);
- bst3.insert(2);
- const bst4 = new BST();
- bst4.insert(2);
- bst4.insert(1);
- bst4.insert(3);
- const bst5 = new BST();
- bst5.insert(1);
- bst5.insert(2);
- bst5.insert(3);
- const result = bst1.search(bst1.root, value) +
- bst2.search(bst2.root, value) +
- bst3.search(bst3.root, value) +
- bst4.search(bst4.root, value) +
- bst5.search(bst5.root, value);
- return result;
- }
- console.log(main(3));
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement