BST - if (x > t.element) { temp = recInsert(x, t.right);...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
//BST running time insert delete=O(log N) //AVL running time Insert=delete=O(log N) public void insert(int theElement) { if (root == null) { root = new BinaryTreeNode(theElement); return; } BinaryTreeNode x = root; boolean done = false; while (! done) { if (x.element == theElement) return; if (x.element < theElement) { if (x.right == null) { x.right = new BinaryTreeNode(theElement); done = true; } else { x = x.right; } } else { if (x.left == null) { x.left = new BinaryTreeNode(theElement); done = true; } else { x = x.left;} } } } public void recInsert(int theElement) { root = recInsert(theElement, root); } private BinaryTreeNode recInsert(int x, BinaryTreeNode t) { BinaryTreeNode temp; if (t == null) return new BinaryTreeNode(x); if (x == t.element) return t;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if (x > t.element) { temp = recInsert(x, t.right); t.right = temp; } else { temp = recInsert(x, t.left); t.left = temp; } return t; } public void delete(int theElement) { root = delete(theElement, root); } private BinaryTreeNode findMin(BinaryTreeNode t) { if (t != null) { while (t.left != null) { t = t.left; } } return t; } private BinaryTreeNode delete(int x, BinaryTreeNode t) { if (t == null) return t; if (x < t.element) t.left = delete(x, t.left); else if (x > t.element) t.right = delete(x, t.right); else if (t.left == null) t = t.right; else if (t.right == null) t = t.left; else { t.element = findMin(t.right).element; t.right = delete(t.element, t.right); } return t; }...
View Full Document

This note was uploaded on 02/18/2011 for the course 22C 031 taught by Professor Kasturi during the Spring '11 term at University of Iowa.

Page1 / 2

BST - if (x > t.element) { temp = recInsert(x, t.right);...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online