AVL Tree Implementation in C++

[9104 views]




What is AVL Tree?

A Self-balancing Binary Search Tree (BST) where difference between heights of left and right subtrees cannot be more than 1 for all nodes is called an AVL Tree.


AVL Tree implementation in C++

Below is a simple implementation of AVL tree in C++ with in-line explanation in comments.

Full Code:

#include <iostream> #include <ctime> #include <iomanip> // ************************************************** // Adelson-Velskii and Landis dynamically balanced // tree class. class AVLTree { // - - - - - - - - - - - - - - - - - - - - - - - - // Private Node subclass for the nodes of the tree. private: class Node { // - - - - - - - - - - - - - - - - - - - - - - // Private data for the node of the tree. private: // The actual data being stored. int data; // The height of this node from the deepest // point. int height; // A pointer to the left subtree. Node *left_child; // A pointer to the node's parent. Node *parent; // A pointer to the right subtree. Node *right_child; // - - - - - - - - - - - - - - - - - - - - - - // Public methods to process the node. public: // Constructor initializing the data. Node(int val); // Calculate the balance point. int getBalance(); // Get the actual data. int getData(); // Get the height. int getHeight(); // Get the left subtree. Node *getLeftChild(); // Get the node's parent. Node *getParent(); // Get the right subtree. Node *getRightChild(); // Remove the node's parent. void removeParent(); // Set the left subtree. Node *setLeftChild(Node *newLeft); // Set the right subtree. Node *setRightChild(Node *newRight); // Set the node's height. int updateHeight(); }; // Node // - - - - - - - - - - - - - - - - - - - - - - - - // Private data for the tree. private: // A pointer to the top node of the tree. Node *root; // - - - - - - - - - - - - - - - - - - - - - - - - // Private methods to process the tree. private: // Balance the subtree. void balanceAtNode(Node *n); // Find the node containing the data. Node *findNode(int val); // Print the subtree. void printSubtree(Node *subtree, int depth, int offset, bool first); // Rotate the subtree left. void rotateLeft(Node *n); // Rotate the subtree left. void rotateRight(Node *n); // Set the root. void setRoot(Node *n); // Figure out the default spacing for element. int spacing(int offset); // - - - - - - - - - - - - - - - - - - - - - - - - // Public methods to process the tree. public: // Constructor to create an empty tree. AVLTree(); // Constructor to populate the tree with one node. AVLTree(int val); // Get the tree's height. int getHeight(); // Insert the value into the tree. bool insert(int val); // Print the tree. void print(); // Remove the value from the tree. bool remove(int val); }; // AVLTree // ************************************************** // AVLTree's Node subclass methods. // -------------------------------------------------- // Constructor initializing the data. AVLTree::Node::Node(int val) { data = val; height = 0; parent = nullptr; left_child = nullptr; right_child = nullptr; } // Node // -------------------------------------------------- // Calculate the balance point. Negative, when the // right side is deeper. Zero, when both sides are // the same. Positive, when the left side is deeper. int AVLTree::Node::getBalance() { // If we don't have a left subtree, check the // right. int result; if (left_child == nullptr) // If neither subtree exists, return zero. if (right_child == nullptr) result = 0; // Otherwise, the right subtree exists so make // it's height negative and subtract one. else result = -right_child->height-1; // Otherwise, we have a left subtree so if we // don't have a right one, return the left's // height plus one. else if (right_child == nullptr) result = left_child->height+1; // Otherwise, both subtrees exist so subtract // them to return the difference. else result = left_child->height-right_child->height; return result; } // getBalance // -------------------------------------------------- // Get the actual data. int AVLTree::Node::getData() { return data; } // getData // -------------------------------------------------- // Get the height. int AVLTree::Node::getHeight() { return height; } // getHeight // -------------------------------------------------- // Get the left subtree. AVLTree::Node *AVLTree::Node::getLeftChild() { return left_child; } // getLeftChild // -------------------------------------------------- // Get the node's parent. AVLTree::Node *AVLTree::Node::getParent() { return parent; } // getParent // -------------------------------------------------- // Get the right subtree. AVLTree::Node *AVLTree::Node::getRightChild() { return right_child; } // getRightChild // -------------------------------------------------- // Remove the node's parent. void AVLTree::Node::removeParent() { parent = nullptr; } // removeParent // -------------------------------------------------- // Set the left subtree. AVLTree::Node *AVLTree::Node::setLeftChild( Node *newLeft) { // If there is a left node, set it's parent to // be us. In any event, make it our left subtree // and update the height. if (newLeft != nullptr) newLeft->parent = this; left_child = newLeft; updateHeight(); return left_child; } // setLeftChild // -------------------------------------------------- // Set the right subtree. AVLTree::Node *AVLTree::Node::setRightChild( Node *newRight) { // If there is a right node, set it's parent to // be us. In any event, make it our right subtree // and update the height. if (newRight != nullptr) newRight->parent = this; right_child = newRight; updateHeight(); return right_child; } // setRightChild // -------------------------------------------------- // Set the node's height. int AVLTree::Node::updateHeight() { // If we don't have a left subtree, check the // right. if (left_child == nullptr) // If we don't have either subtree, the height // is zero. if (right_child == nullptr) height = 0; // Otherwise, we only have the right subtree so // our height is one more than it's height. else height = right_child->height+1; // Otherwise, the left subtree exists so if we // don't have a right one, our height is one // more than it's height. else if (right_child == nullptr) height = left_child->height+1; // Otherwise, we have both subtree's so if the // left's height is greater than the right, our // height is one more than it. else if (left_child->height > right_child->height) height = left_child->height+1; // Otherwise, the right subtree's height will be // used so our height is one more than it. else height = right_child->height+1; return height; } // updateHeight // ************************************************** // AVLTree class methods. // -------------------------------------------------- // Constructor to create an empty tree. AVLTree::AVLTree() { root = nullptr; } // AVLTree // -------------------------------------------------- // Constructor to populate the tree with one node. AVLTree::AVLTree(int val) { root = new Node(val); } // AVLTree // -------------------------------------------------- // Balance the subtree. void AVLTree::balanceAtNode(Node *n) { // Get the current balance and if it is bad // enough on the left side, rotate it right // adjusting the subtree left, if required. int bal = n->getBalance(); if (bal > 1) { if (n->getLeftChild()->getBalance() < 0) rotateLeft(n->getLeftChild()); rotateRight(n); // Otherwise, if the balance is bad enough on // the right side, rotate it left adjusting the // subtree right, if required. } else if (bal < -1) { if (n->getRightChild()->getBalance() > 0) rotateRight(n->getRightChild()); rotateLeft(n); } // if } // balanceAtNode // -------------------------------------------------- // Find the node containing the data. AVLTree::Node *AVLTree::findNode(int val) { // While nodes remain, if we found the right // node, exit the loop. If the value we want // is less than the current, check the left // subtree, otherwise, the right. Node *temp = root; while (temp != nullptr) { if (val == temp->getData()) break; else if (val < temp->getData()) temp = temp->getLeftChild(); else temp = temp->getRightChild(); } // while return temp; } // findNode // -------------------------------------------------- // Get the tree's height. int AVLTree::getHeight() { return root->getHeight(); } // getHeight // -------------------------------------------------- // Insert the value into the tree. // Returns: // true: If addition is successful // false: If item already exists // bool AVLTree::insert(int val) { // If the tree is empty, add the new node as the // root. if (root == nullptr) root = new Node(val); // Otherwise, we need to find the insertion point. else { // Starting at the tree root search for the // insertion point, until we have added the // new node. Node *added_node = nullptr; Node *temp = root; while (temp != nullptr && added_node == nullptr) { // If the value is less than the current // node's value, go left. If there isn't a // left subtree, insert the node, otherwise, // it is next to check. i</div> <table> <tr> <td><div> <a href="#" id="upvote"><img alt="Solution Worked" src="/resources/images/upvote.jpg" width="80"> 7 Upvotes</a> </div></td> <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td> <td><div> <a href="#" id="downvote"><img alt="Solution Didn't Worked" src="/resources/images/downvote.jpg" width="80"> 13 Downvotes</a> </div></td> <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td> </tr> </table> <br> <div class="card-footer text-muted" align="right"> Updated on 25 FEBRUARY, 2021 by <a href="/author/1">Shaddy</a> </div> <br> <!-- EndOfContent-ATech --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6110005078124306" data-ad-slot="2597659780" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <br> <br> <br> <div class="modal-body" id="getCode" style="border: 1px solid black;" > <h3>AI Search Engine:</h3><br> <h4 style="text-align: center;"><script async src="https://cse.google.com/cse.js?cx=0349454d8f798210d"></script> <div class="gcse-search"></div></h4> </div> <br> <div class="col-md-16 "> <h4 style="margin-bottom:1em;">Comments </h4> <div id="comment_form"> <div> <input type="text" name="name" id="name" value="" placeholder="Name" required="required"> </div> <div> <input type="email" name="email" id="email" value="" placeholder="Email" required="required"> </div> <div> <textarea rows="5" name="comment" id="comment" placeholder="Comment" required="required"></textarea> </div> <div class="g-recaptcha" data-sitekey="6LchJakjAAAAAKyp7Jj4preXm7sjNTEAh0Sj1qNE"></div> <div> <input id="addCommentAjax" type="submit" name="submit" value="Add Comment"> </div> <span id="loadingIcon"></span> </div> <div class="card paper"> <summary style="padding:1em;"><span id="commentSizeAjax">1</span> comment</summary> <ul class="list-group"> <li class="list-group-item "> <!-- <span class="circle"> <img src="imagestoragelocation" alt="user"> </span> --> <span class="title" style="margin-left:0px;"> Daniel <time> 28 APRIL 2021 8:13:9</time> <p>Do not keep track of parent!!!!!!!!!!!!!</p> </span> <ul class="actions" href="#"> <!-- <li><a class="reply" href="#">Reply</a></li>--> </ul> </li> </ul> <div id="latestComment"> </div> </div> <br> <br> </div> <br> <br> <div id="fixedElement"> <br><br> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6110005078124306" data-ad-slot="2597659780" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <!-- Sidebar Widgets Column --> <div class="col-md-3"> <br> <!-- SidebarTop-ATech --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6110005078124306" data-ad-slot="8433559958" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <br><br> <!-- Search Widget --> <div class="card my-4"> <h5 class="card-header">Search Anything:</h5> <div class="card-body"> <form method="get" action="/search" id="search_form" onsubmit="search()"> <div class="input-group"> <input id="q" type="text" class="form-control" placeholder="Search anything..." name="q"> <span class="input-group-btn"> <button class="btn btn-secondary" id="searchBtn" type="submit">Search</button> </span> </div> </form> </div> </div> <script defer> var input = document.getElementById("q"); function search(){ var input = document.getElementById("q"); var action_src = "/search/" + input.value.replace(/[^a-zA-Z\d ]/g, "") ; var search_form = document.getElementById('search_form'); search_form.action = action_src ; } function productSearch(){ var input = document.getElementById("q2"); var action_src = "/search/" + input.value.replace(/[^a-zA-Z\d ]/g, "") ; var product_form = document.getElementById('product_form'); product_form.action = action_src ; } </script> <br> <div class="card my-4"> <h5 class="card-header" style="color:#fc5f48">Sponsored Deals ends in <span id="time"></span></h5> <div class="card-body"> <!-- SidebarTop-ATech --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6110005078124306" data-ad-slot="8433559958" data-ad-format="auto" data-full-width-responsive="true"></ins> <script defer> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <br> <hr> <br> <ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-gu-c+w-3l+7t" data-ad-client="ca-pub-6110005078124306" data-ad-slot="3805214224"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="card my-4"> <h5 class="card-header">E-Books & Study Notes:</h5> <br> <a href="/restricted/payment/file/algorithm-flowchart-notes.pdf?pr=11110&dld=4BE"><img src="/resources/images/algorithm_flowchart_notes.jpg"></a> <br><br> <hr> <h5 style="text-align:center;"><a id="pythonBasics" href="/restricted/payment/file/Java-Interview-Questions-atechdaily.pdf?dld=23D"><button class="btn btn-success btn-lg">Get Most Frequently Asked Java Interview Questions</button></a></h5> <br><br> <hr> <h5 style="text-align:center;"><a id="pythonBasics" href="/restricted/payment/file/python-basics.pdf?pr=11110&dld=5BD"><button class="btn btn-success btn-lg">Get Simplest Python Programming Notes</button></a></h5> <br> </div> </div> <div class="card my-4"> <h5 class="card-header">Technical Quizzes Specially For You:</h5> <div class="card-body"> <ul style="padding: 0px;"> <li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/quiz/algorithms-pseudocode">Algorithms</a></li><br> <li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/quiz/Java">Java</a></li><br> <li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/quiz/Python">Python</a></li><br> </ul> <br><br> </div> </div> <br> <div class="card my-4"> <h5 class="card-header">Online Free Tools:</h5> <div class="card-body"> <ul style="padding: 0px;"> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/java-ide-online.html">Java IDE Online</a></li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/Python-ide-online.html">Python IDE Online</a></li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/json-formatter">JSON Formatter/Minifier Online</a></li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/case-converter">Case Converter</a></li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/reverse-string">Reverse String</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/htmlencode">HTML Encoder</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/htmldecode">HTML Decoder</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/urlencode">URL Encoder</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/urldecode">URL Decoder</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/decimal-to-binary">Decimal To Binary</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/binary-to-decimal">Binary To Decimal</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/decimal-to-octal">Decimal To Octal</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/binary-to-octal">Binary To Octal</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/decimal-to-hexa">Decimal To Hexadecimal</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/hexa-to-decimal">Hexadecimal To Decimal</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/hexa-to-binary">Hexadecimal To Binary</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/octal-to-decimal">Octal To Decimal</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/octal-to-hex">Octal to Hexadecimal</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/octal-to-binary">Octal to Binary</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/calculate-string-length">Calculate String Length</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/remove-spaces">Remove Spaces</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/remove-line-breaks">Remove Line Breaks</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/remove-empty-lines">Remove Empty Lines</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/remove-duplicate-lines">Remove Duplicate Lines</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/word-counter">Word Counter</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/replace-space-with-hyphen">Replace Space with hyphen</a> </li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/check-armstrong-number">Check Armstrong number</a></li><br></h5> <h5><li class="fa fa-caret-right" style="padding: 5px;">&nbsp;<a href="/tools/sentence-to-url">Text to URL</a></li><br></h5> </ul> </div> </div> <!-- Deal timer script --> <script defer> function countdown(minutes) { var seconds = 60; var mins = minutes function tick() { //This script expects an element with an ID = "counter". You can change that to what ever you want. var counter = document.getElementById("time"); var current_minutes = mins-1 seconds--; counter.innerHTML = current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds); if( seconds > 0 ) { setTimeout(tick, 1000); } else { if(mins > 1){ countdown(mins-1); } } } tick(); } countdown(9); </script> <div class="card my-4"> <h5 class="card-header"> <strong>Search Tags</strong> </h5> <div class="card-body"> <ul style="padding:0px;"> <h4>C++ code for AVL tree</h4> </ul> </div> </div> <div class="card my-4"> <h5 class="card-header"> <strong>You Might Also Like</strong> </h5> <div class="card-body"> <ul style="padding:0px;"> <li class="fa fa-caret-right" style="padding:5px;">&nbsp;<a href="/posts/c-best-practices-cheatsheet-for-developers">C# Best Practices CheatSheet for Developers</a> </li> <li class="fa fa-caret-right" style="padding:5px;">&nbsp;<a href="/posts/dijkstras-algorithm-in-cpp">Dijkstra's Algorithm in C++</a> </li> <li class="fa fa-caret-right" style="padding:5px;">&nbsp;<a href="/posts/Add-a-column-with-a-default-value-to-a-table-in-Oracle-SQL">Add a column with a default value to a table in Oracle SQL</a> </li> <li class="fa fa-caret-right" style="padding:5px;">&nbsp;<a href="/posts/algorithm-for-factorial-number">Algorithm for Finding Factorial of a Number</a> </li> <li class="fa fa-caret-right" style="padding:5px;">&nbsp;<a href="/posts/stooge-sort-algorithm-in-python">Stooge sort Algorithm in Python</a> </li> </ul> </div> </div> <div id="fixedElement"> <br><br> <!-- divmd2-atech --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6110005078124306" data-ad-slot="8440945536" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <!-- Sidebar Widgets Column End--> </div> <!-- /.row End--> </div> <!-- /.container End--> <br> <!-- Footer --> <footer class="py-5 bg-dark" > <div class="container"> <p class=" m-0 text-center text-white"><a href="/privacy-policy">Privacy Policy</a> & <a href="/terms-of-service">Terms Of Condition</a> & <a href="/Affliate-Disclosure.html">Affliate Disclosure</a>&nbsp;&nbsp;&nbsp;Copyright &copy; ATechDaily 2020-25</p> </div> <!-- /.container --> </footer> <!-- Bootstrap and jquery script link --> <script> $(document).ready(function() { setTimeout(function() { $.getScript("https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"); if (localStorage.getItem('cookieconsent')===null) { document.body.innerHTML += '\ <div class="cookieconsent" style="position:fixed;padding:20px;left:0;bottom:0;background-color:#000;color:#FFF;text-align:center;width:100%;z-index:99999;">\ This site uses cookies. By continuing to use this website, you agree to their use. \ <a href="#" style="color:#CCCCCC;"><b>I Understand</b></a>\ </div>\ '; document.querySelector('.cookieconsent a').onclick = function(e) { e.preventDefault(); document.querySelector('.cookieconsent').style.display = 'none'; localStorage.setItem('cookieconsent', true); }; } }, 3000); }); </script> <script> var upvoteClicked="0"; var downvoteClicked="0"; var addCommentClicked="0"; var nodeDiv = document.createElement("div"); var node = document.createElement("ins"); node.style = 'display:block; text-align:center;'; node.setAttribute('class','adsbygoogle'); node.setAttribute('data-ad-client','ca-pub-6110005078124306'); node.setAttribute('data-ad-slot','4825133188'); node.setAttribute('data-ad-format','fluid'); node.setAttribute('data-full-width-responsive','fluid'); node.setAttribute('data-ad-layout','in-article'); var scriptNode=document.createElement("script"); scriptNode.text= '(adsbygoogle = window.adsbygoogle || []).push({});'; nodeDiv.appendChild(node); nodeDiv.appendChild(scriptNode); var parentElement=document.getElementById('postdesc'); var el = document.querySelectorAll('#postdesc p'); for(var i = 1; i < el.length; i++) { if(i%3==0){ parentElement.insertBefore(nodeDiv.cloneNode(true),el[i]); } } $(document).ready(function() { var upvoteHtml='<img alt="Solution Worked" src="/resources/images/upvote.jpg" width="80">'; var downvoteHtml='<img alt="Solution Worked" src="/resources/images/downvote.jpg" width="80">'; $('#upvote').click(function(e){ e.preventDefault(); if(upvoteClicked === "0") { $.ajax({ type:'POST', url:'/ajaxUpvote/286', success: function(result){ $('#upvote').html(upvoteHtml+" "+result+" Upvotes"); }, error: function(result) { console.log(result); } }); upvoteClicked='1'; } }); $('#downvote').click(function(e){ e.preventDefault(); if(downvoteClicked === "0") { $.ajax({ type:'POST', url:'/ajaxDownvote/286', success: function(result){ $('#downvote').html(downvoteHtml+" "+result+" Downvotes"); } }) downvoteClicked="1"; } }); $('#addCommentAjax').click(function(event){ var name=$('#name').val(); var comment=$('textarea').val(); var email=$('#email').val(); var intials =name.charAt(0); var profileImage = $('#profileImage').text(intials); event.preventDefault(); if(name!="" && comment!="" && email!="" && validateEmail(email) && addCommentClicked==="0"){ $.ajax({ type:'POST', url:'/ajax/posts/286/addComment', data:{name:name,email:email,comment:comment,'g-recaptcha-response':grecaptcha.getResponse()}, dataType: 'json', beforeSend:function(){ $('#loadingIcon').html('<span style="font-size:30px; text-align:center;"><i class="fa fa-spinner fa-spin"></i> &nbsp;Commenting</span>'); }, success: function(comments){ $('#loadingIcon').html(""); var newHtml="<ul class='list-group'><li class='list-group-item '><span class='title' style='margin-left:0px;'>"+comments.created_by+" <time>"+comments.created_at+"</time> <p>"+comments.description +"</p></span></li></ul>"; $('#latestComment').html(newHtml); $("#addCommentAjax").attr("disabled", true); } }); addCommentClicked="1"; document.getElementById("#addCommentAjax").disabled = true; } else if(name=="" || comment==""){ alert("Name or Comment can't be blank") } else { alert("Please Enter Valid Email Address"); } }); function validateEmail(email) { var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); } function setCookie(name,value,days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days*24*60*60*1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; } function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } }); </script> <!-- <div class="modal fade" id="donateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <div class="modal-header"> <h4 class="modal-title" id="myModalLabel">Improve Your Search with AI:</h4> <button type="button" class="close" data-dismiss="modal" aria-label="Close" id="donateClosemodal"><span aria-hidden="true">&times;</span></button> </div> <div class="modal-body" id="getCode" > Let's deep drive your search by using below <b>AI Search Tool</b> and get more accurate information:<br> <h4 style="text-align: center;"><script async src="https://cse.google.com/cse.js?cx=0349454d8f798210d"></script> <div class="gcse-search"></div></h4> </div> </div> </div> </div> <script> window.addEventListener("scroll", openModal); function openModal() { var cardFooterElement = $('.card-footer').offset(); if (window.pageYOffset > cardFooterElement.top-100){ window.removeEventListener("scroll", openModal); $('#donateModal').modal('toggle')}; }; $('#donateClosemodal').click(function() { $('#donateModal').modal('toggle'); }); </script> --> </body> </html>