You are on page 1of 44

Binary Trees

Tree

Why use Binary Tree


It combines the advantages of two other structures (an ordered array and a linked list)
Searching is quickly (an ordered array) binary search Insert and delete quickly ( a linked list)

A general tree but non-binary tree

Term applied in binary tree

Sequence of nodes is called a path

Non-tree

Term of tree
Parent: any node (except the root) has exactly one edge running upward to another node. Child: any node may have one or more lines running downward to other nodes. Leaf: a node that has no children Subtree: any node may be considered to be the root of a subtree, which consists of its children, and its childrens children, and so on

Traversing: to visit all the nodes in some specified order. For example, you might visit all the nodes in order of ascending key value Levels: how many generations the nodes is from the root. It assumed, the root is level 0, then its children will be level 1, and so on. Keys: one data field in an object

Binary Tree
If every node in a tree can have at most two children Two children of each node in binary tree are called left child and right child The kind of binary tree that will be discussed is binary search tree

Binary search tree


A nodes left child must have a key less that its parent, and a nodes right child must have a key greater than or equal to its parent

An unbalanced tree (with an unbalanced subtree)

Representing the Tree in Java


The node Class
These objects contain the data representing the object being stored.
class Node { int iData; double fData; node leftChild; node rightChild;

// // // //

data used as key value other data this nodes left child this nodes right child

public void displayNode(); }

The Tree class


The object that holds all the nodes It has only one field: a Node variable that holds the root. There is a number methods, such as: finding, inserting, and deleting nodes, traverse and displaying the tree.

class Tree { private Node root; public void find (int key) public void insert(int id, double dd) public void delete (int id) // various other methods }

Finding a Node
Finding node 57 Starts at the root. If key is less, then it goes to the left right. Otherwise, it goes to the right side

Finding a Node in Java code


public Node find(int key) // find node with given key { node current = root; // start at root
while (current.iData !=key) // while no match

{ if (key<current.iData) // go left current = current.leftChild; else current = current.rightChild; // or go right? if (current == null) // if no child, return null; // didnt find it } return current; // found it }

Tree Efficiency
The time required to find anode depends on how many levels down it is situated. This is O(log2 N) time

Inserting a Node
To insert a node, we must first find the place to insert it. Example: to insert a new node 45

Inserting in Java Code


public void insert(int id, double dd) { Node newNode=new Node(); // make a 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) // (exists 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 the left

Inserting in java code (contd.)


else { current = current.rightChild; if(current==null) // if end of the line { parent.rightChild = newNode; return; } } // end else go right } } }

Traversing the Tree


There are three simple ways to traverse a tree:
Preorder Inorder Postorder

InOrder Traversal
1. Call itself to traverse the nodes left subtree 2. Visit the node 3. Call itself to traverse the nodes right subtree
private void inOrder(node localRoot) { if(localRoot !=null) { inOrder(localRoot.leftChild); System.out.print(localRoot.iData + ); inOrder(localRoot.rightChild); } } This method is initially called with the root as an argument: inOrder(root)

The inOrder() method applied to a three-node tree

Traversing a tree inorder

PreOrder Traversal
1. Visit the node 2. Call itself to traverse the nodes left subtree 3. Call itself to traverse the nodes right subtree

PostOrder Traversal
1. Call itself to traverse the nodes left subtree 2. Call itself to traverse the nodes right subtree 3. Visit the node

Finding Minimum & Maximum value of a Tree For the minimum, go to the left child of the root; then go to the left child of that child and so on

Finding a minimum value in Java code


public Node minimum() // return node with minimum key value { Node current, last; current=root; // start at root while(current !=null) // until the bottom { last=current; current=current.leftChild; // go to left child } return last; }

Finding a maximum value in Java code


public Node maximum() // return node with minimum key value { Node current, last; current=root; // start at root while(current !=null) // until the bottom { last=current; current=current.rightChild; // go to left child } return last; }

Deleting a Node
It is started by finding the node that want to delete. There are three cases:
1. The node to be deleted is a leaf (has no children) 2. The node to be deleted has one child 3. The node to be deleted has two children

Case 1: The Node to be Deleted has No Children To delete a leaf node, it just change the appropriate child field in the nodes parent to point to null, instead of to the node.

Delete a node with no children in Java code


public boolean delete(int key) { Node current = root; Node parent = root; boolean isLeftChild = true; while(current.iData != key) { parent=current; if (key<current.iData) { isLeftChild=true; current=current.leftChild; } else

// or go right isLeftChild=false; current=current.rightChild;

} if (current==null) return false } // found node to delete continues }

Delete a node with no childreen in Java code (contd..)


// delete() continued // if no children, simply delete it if (current.leftChild==null && current.rightChild==null) { if (current == root) // if root, root = null; // tree is empty else if (isLeftChild) parent.leftChild=null; //disconnect else //from parent parent.rightChild=null; }

Case 2: The Node to be deleted has one child


The node has only two connections: to its parent and to its only child. The process involves changing the appropriate reference in the parent(leftChild or rightChild) to point to the deleted nodes child.

Delete a node with one child in Java code


// delete continued // if no right child, replace with left subtree else if (current.rightChild==null) if (current==root) root=current.leftChild; else if(isLeftChild) parent.leftChild=current.leftChild; else parent.rightChild=current.leftchild; //if no left child, replace right subtree Else if(current.leftChild==null) if(current==root) root=current.rightChild; else if(isLeftChild) parent.leftChild=current.rightChild; else parent.rightChild=current.rightChild;

Case 3: The Node to be deleted has two children

To delete a node with two children, replace the node with its inorder successor

Finding the Successor


First, go to the original nodes right, which have a key larger than the node. Then it goes to this rights left child and to this left childs left child, and so on.

Find the successor in Java code


// return node with next highest value after delNode //goes to right child, then right childs left descendants private node getSuccessor(node delNode) { Node successorParent=delNode; Node successor=delNode; Node current = delNode.rightChild;//go to right child while(current !=null) // until no more left children { successorParent=successor; successor=current; current = current.leftChild; // go to left child }

if (successor != delNode.rightChild)// if successor not { // right child, make connections successorParent.leftChild=successor.rightChild; successor.rightChild=delNode.rightChild; } return successor; }

Successor is right child of delNode


1. Parent.rightChild=successor; 2. Successor.leftChild=current.leftChild;

Successor is Left Descendant of right of delNode


1. 2. 3. 4. successorParent .leftChild=successor.rightChild; successor.rightChild=delNode.rightChild; parent.rightChild=successor; Successor.leftChild=current.leftChild;

// if successor is not right child, make connections if (successor !=delNode.rightChild) { successorParent.leftChild=successor.rightChild; successor.rightChild=delNode.rightChild; }

Tree Represented as Arrays


Left child = 2*index +1 Right child = 2*index + 2 Its parent = (index-1)/2

You might also like