1 trees - part ii © dave bockus. 2 building a binary search tree h i b c e d f m k a input: i h b m...
DESCRIPTION
3 Code for BST Insertion Using Recursion BinaryNode Insert(Comparable x, BinaryNode t) { if (t == null) Null ptr, Create new Node. t = new BinaryNode(X, null, null); else if (x.compareTo(t.element < 0)If element is < then descend left t.left = Insert(x, t.left); else if (x.compareTo(t.element > 0)If element is > then descend right t.right = Insert(x, t.right); else ; Equal i.e. Duplicate - do nothing return t; }TRANSCRIPT
1
Trees - Part II
© Dave Bockus
2
Building a Binary Search Tree
h
i
b
c
e
d
f
m
k
a
Input: i h b m e c f a d k
3
Code for BST InsertionUsing Recursion
BinaryNode Insert(Comparable x, BinaryNode t) {if (t == null) Null ptr, Create new Node. t = new BinaryNode(X, null, null);
else if (x.compareTo(t.element < 0) If element is < then descend left t.left = Insert(x, t.left); else if (x.compareTo(t.element > 0) If element is > then descend right t.right = Insert(x, t.right); else ; Equal i.e. Duplicate - do nothing return t;}
4
BST Insertion Using Iteration - page 308 Lafore public void insert(int id, double dd) { Node newNode = new Node(); // make new node newNode.iData = id; // insert data newNode.dData = dd; if(root==null) // no node in root root = newNode; else { // root occupied Node current = root; // start at root Node parent; while(true) { // (exits internally) parent = current; if(id < current.iData){ // go left? current = current.leftChild; if(current == null) { // if end of the line parent.leftChild = newNode; // insert on left return; } } // end if go left else { // or go right? current = current.rightChild; if(current == null){ // if end of the line parent.rightChild = newNode; // insert on right return; } } // end else go right } // end while } // end else not root } // end insert()
5
Modified BST Insertion Using Iteration public Node insert(int id) //Data is modified outside of { // insert via returned ptr Node newNode = new Node(); // make new node newNode.iData = id; // insert key data if(root==null) // no node in root root = newNode; return root; else { // root occupied Node current = root; // start at root Node parent; while(true) { // (exits internally) parent = current; if (id == current.iData) return current; if(id < current.iData){ // go left? current = current.leftChild; if(current == null) { // if end of the line parent.leftChild = newNode; // insert on left return parent.leftChild; } } // end if go left else { // or go right? current = current.rightChild; if(current == null){ // if end of the line parent.rightChild = newNode; // insert on right return parent.rightChild; } } // end else go right } // end while } // end else not root } // end insert()
6
Building an AVL Tree
J
M
B
GE
Input: M J B F E G
Single Right
J
B M
FDouble LeftB
E
F
Double RightF
JE
G MB
7
Deletions in a Binary Tree
• 3 Cases - if we delete node pointed to by ptr– Leaf Node
• Just Delete it– Node with 1 null sub-tree.
• Ptr’s Parent points to non-null sub-tree.– Node with 2 non-null sub-trees.
• Find successor• Copy successor into node pointed to by ptr.• Delete successor - simple case i.e. null left sub-tree
8
Deletion Case 1 - A leaf node
h
i
b
c
e
d
f
m
k
aLeaf nodes can be deleted
without any problems
9
h
i
b
c
e
d
f
m
k
a
Deletion Case 2 - 1 non-null sub-tree
Parent points to non-null subtree
ptr
Node pointed to by ptr is deleted
10
h
i
b
c
e
d
f
m
k
a
Deletion Case 2 - 2 non-null sub-trees
c
ptr
Find the successor
Replace data of ptr with data of qtr
Deleting node qtr is a simple case
qtr
c
11
Code to Delete from a BSTBinaryNode remove(Comparable x, BinaryNode t) {
if (t == null) Null ptr, node not found return t;if (x.compareTo(t.element) < 0) Descend left branch t.left = remove(x, t.left);else if(x.compareTo(t.element) > 0) Descend right branch t.right = remove(x, t.right);else if (t.left != null && t.right != null) { t.element = successor(t).element; Copy successor into current t.right = remove(t.element, t.right); node and recursively remove} the successor.else if (t.left != null) Simple case: found x pointed to by t = t.left; t, so point around it to the non- else null sub-tree. If t is a leaf then t = t.right; t.left is null so we point around toreturn t; t.right which is null also, this
} null is returned.