Skip to content

implement search in AVL tree #1302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions DataStructures/Lists/DoublyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ else if (current == null)
current.previous = newLink; // 1 <--> newLink <--> 2(current) <--> 3
}
}

public static void removeDuplicates(DoublyLinkedList l ) {
Link linkOne = l.head ;
while(linkOne.next != null) { // list is present
Link linkTwo = linkOne.next; // second link for comparison
while(linkTwo.next!= null) {
if(linkOne.value == linkTwo.value) // if there are duplicates values then
l.delete(linkTwo.value); // delete the link
linkTwo = linkTwo.next ; // go to next link
}
linkOne = linkOne.next; // go to link link to iterate the whole list again
}
}

/**
* Returns true if list is empty
Expand Down
23 changes: 23 additions & 0 deletions DataStructures/Trees/AVLTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,29 @@ private void reheight(Node node) {
}
}

public boolean search(int key) {
Node result = searchHelper(this.root,key);
if(result != null)
return true ;

return false ;
}

private Node searchHelper(Node root, int key)
{
//root is null or key is present at root
if (root==null || root.key==key)
return root;

// key is greater than root's key
if (root.key > key)
return searchHelper(root.left, key); // call the function on the node's left child

// key is less than root's key then
//call the function on the node's right child as it is greater
return searchHelper(root.right, key);
}

public static void main(String[] args) {
AVLTree tree = new AVLTree();

Expand Down