Sei sulla pagina 1di 45

Data Structures

Binary Trees

University of Trunojoyo Madura

Introduction
Why use Binary Trees ?
An ordered array
Fast searching binary search
Slow insertion find the exact position and then move all
the objects with greater keys up one space in the array to
make room for it. The same problem arise in deletion
process

Linked List
Quick insertion and deletion by changing few references
Slow searching must start at the beginning of the list and
visit each node to find the match element

Trees
Trees
quick insertion and deletion
quick searching of an ordered array

Tree consists of
Nodes
[connected by] Edges

A tree is an instance of a more general


category a graph

Trees

There is one node in the top row of a tree, with


lines connecting to more nodes on the second
row, even more on the third, and so on trees
are small on the top and large on the bottom

Tree Terminology

Path : Sequence of nodes (source to destination node) is called a


path
Root : The node at the top of the tree. There is only one root in a
tree. For a collection of nodes and edges to be defined as a tree,
there must be one (and only one!) path from the root to any other
node.

Tree Terminology

Parent : Any node (except the root) has exactly one edge running
upward to another node. The node above it is called the parent of
the node
Children : Any node may have one or more lines running
downward to other nodes. These nodes below a given node are
called its children
Leaf : A node that has no children. There can be only one root in a
tree, but there can be many leaves.
Visiting : A node is visited when program control arrives at the
node, usually for the purpose of carrying out some operation on the
node
Traversing : visit all the nodes in some specified order

Binary Trees

Every node in a tree can have at most two children


The two children of each node in a binary tree are called the left
child and the right child
A binary search tree : A nodes left child must have a key less
than its parent, and a nodes right child must have a key greater
than or equal to its parent.

Binary Trees
Code
Class for node objects, consists of data, and
references for two children (left child and right
child)
class Node
{
int data;
Node leftChild;
Node rightChild;
}

Binary Trees
Code
Class for Tree. It has only one field: a
Node variable that holds the root. It
doesnt need fields for the other nodes
because they are all accessed from the
root.
The Tree class has a number of methods.
They are used for finding, inserting, and
deleting nodes

Binary Trees
Code
class Tree {
private Node root;
public Node find(int key)
{
}
public void insert(int key)
{
}
public void delete(int key)
{
}
}

Binary Trees
Code
Main insert three nodes and find certain node
public static void main(String[] args)
{
Tree theTree = new Tree ();
theTree.insert(50);
theTree.insert(25);
theTree.insert(75);
Node found = theTree.find(25);
if(found != null)
System.out.println(Found the node); else
System.out.println(Could not find node);
}

Finding Node
Applet

Finding Node
public Node find(int key)
{
Node current = root;
// start at root
while(current.data!= key)
// while no match,
{
if(key < current.data)
// 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
}

Inserting Node
Applet

Inserting Node
public void insert(int key)
{
Node newNode = new Node();
// make new node
newNode.data = key;
// insert data
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.data) // go left?
{
current = current.leftChild;
if(current == null)
// if end of the line,
{
// insert on left
parent.leftChild = newNode;
return;
}
} // end if go left

Inserting Node
else
{

// or go right?

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

Traversing the
Tree
Preorder
Inorder
Postorder
Applet

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

inorder
private void inOrder(Node localRoot)
{
if(localRoot != null)
{
inOrder(localRoot.leftChild);
System.out.print(localRoot.data+ );
inOrder(localRoot.rightChild);
}
}

Inorder

Infix
: A*(B+C) inorder traverse will generate
A*B+C we have to add parentheses
Prefix
: *A+BC
Postfix
: ABC+*

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

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

Minimum and
Maximum Value
Minimum find left child until the node
has no left child
Maximum conversely

Deleting a Node
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.

Deleting a Node
[1]
Change the child field in the nodes
parent to null, applet

Deleting a Node
[1]
public boolean delete(int key)
{
// (assumes non-empty list)
Node current = root;
Node parent = root;
boolean isLeftChild = true;
while(current.data!= key)
// search for node
{
parent = current;
if(key < current.iData)
// go left?
{
isLeftChild = true;
current = current.leftChild;
} else
// or go right?
{
isLeftChild = false;
current = current.rightChild;
}
if(current == null)
// end of the line,
return false; // didnt find it
}

// end while

Deleting a Node
[1]
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;
}

Deleting a Node
[2]
Connecting the nodes parent directly to
the nodes child, applet

Deleting a Node
[2]
// if no right child, replace with left subtree
else if(current.rightChild==null)
if(current == root)
root = current.leftChild;
else if(isLeftChild)
// left child of parent
parent.leftChild = current.leftChild;
else
// right child of parent
parent.rightChild = current.leftChild;
// if no left child, replace with right subtree
else if(current.leftChild==null)
if(current == root)
root = current.rightChild;
else if(isLeftChild)
// left child of parent
parent.leftChild = current.rightChild;
else
// right child of parent
parent.rightChild = current.rightChild;

Deleting a Node
[3]

Deleting a Node
[3]
Replace the node with its inorder
successor

Deleting a Node
[3]
Finding Successor the smallest of the
set of nodes that are larger than the
original node

Deleting a Node
[3]
private node getSuccessor(node delNode)
{
Node successorParent = delNode;
Node successor = delNode;
Node current = delNode.rightChild;
while(current != null)
{
successorParent = successor;
successor = current;
current = current.leftChild;
}

// go to right child
// until no more
// left children,

// 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;
}

Deleting a Node
[3]
else // two children, so replace with inorder successor
{
// get successor of node to delete (current)
Node successor = getSuccessor(current);
// connect parent of current to successor instead
if(current == root)
root = successor;
else if(isLeftChild)
parent.leftChild = successor;
else
parent.rightChild = successor;
// connect successor to currents left child
successor.leftChild = current.leftChild;
} // end else two children
// (successor cannot have a left child)
return true;
} //

Huffman Code
Each character in a normal
uncompressed text file is represented in
the computer by :
One byte ASCII
Two bytes unicode

Every character requires the same


number of bits

Huffman Code
ASCII Code

Huffman Code
Compression is important
For text, the most common approach is to reduce the
number of bits that represent the most-used characters.
E.g. number of bits for e will be different with z, since
z is rarely used in words
No code can be the prefix of any other code.
E.g. code for e is 01 and x is 01011000, then the
code 01011000 is ambiguous (either for e or for x)

Huffman Code
E.g.
SUSIE SAYS IT IS EASY.

Huffman Code

Decoding with
Huffman Tree

The characters in the message


appear in the tree as leaf nodes
The number outside each circle is
the frequency
The numbers outside non-leaf
nodes are the sums of the
frequencies of their children
If you see a 0 bit, you go left to the
next node, and if you see a 1 bit,
you go right

Create Huffman
Tree
1. Make a Node object (as seen in tree.java) for each
character used in the message. For our Susie
example that would be nine nodes. Each node has
two data items: the character and that characters
frequency in the message.
2. Make a tree object for each of these nodes. The node
becomes the root of the tree.
3. Insert these trees in a priority queue. They are ordered
by frequency, with the smallest frequency having the
highest priority. That is, when you remove a tree, its
always the one with the least-used character.

Create Huffman
Tree
1. Remove two trees from the priority queue, and make
them into children of a new node. The new node has a
frequency that is the sum of the childrens frequencies;
its character field can be left blank.
2. Insert this new three-node tree back into the priority
queue.
3. Keep repeating steps 1 and 2. The trees will get larger
and larger, and there will be fewer and fewer of them.
When there is only one tree left in the queue, it is the
Huffman tree and youre done.

Create Huffman
Tree

Create Huffman
Tree

12 Juni 2015
1.
2.

Recursif untuk menara hanoi dan hitungan pangkat suatu


bilangan (xn)
Insertion sort vs shell short

3.

Buat Binary Tree

4.

Bandingkan jumlah shifting untuk insertion dan shell short


Create binary tree
Find node
Insert node
Del node
Inorder traversal
Finding Maximum of binary search tree

Buat Huffman Code

Potrebbero piacerti anche