Title: Tree
1Tree
2Basic characteristic
3- Top node root
- Left and right subtree
- Node 1 is a parent of node 2,5,6.
- Node 2 is a parent of node 3,4
- Node 3,4 are children of node 2.
- Node 3 and 4 are siblings.
- Node 1 is an ancestor of node 7.
4- Node that does not have any branch going out
leaf. In the picture, the leaves are 3, 4, 5 and
7. - Notice there is only 1 path from a root to a
node. - depth of node n is the path length from root to
n. - Example depth of node 7 is 2.
- Height of n is the maximum path length from n to
leaf. - Example height of node 7 is 0.
- Height of node 1 is 2.
5- Height of a tree
- If a tree only has root, we let its height be 0.
For empty tree, we let its height be -1. - height of any tree height of its heighest
subtree 1. - level of a node
- A child of a node has 1 more level than its
parent. - A root has its level 0.
6If a node can have any number of branches?
- We can never be sure of the number of branches?
- Preparing many branches in advance is a waste of
space. - One solution! -gt
7How to arrange nodes for printing
- preorder
- Pick the root first
- Then pick subtree
- Within subtree, pick its root first, and so on.
-
8Preorder result is 1,2,3,4,5,6,7
First to pick
4th, this subtree
Second, this subtree
Inside, it is also preorder.
Inside, we also view it with preorder.
Third
9- postorder
- View all subtrees, then the root.
- Inside each subtree, use postorder.
- The postorder result of our example is
3,4,2,5,7,6,1. - inorder
- Left subtree comes first, then root, then right
subtree. - Inside each subtree, use inorder.
- The inorder result of our example is (we do not
count a subtree that has 5 because we do not know
which side it will be on) 3,2,4,1,6,7
10breadth-first search
- The first 3 methods we have seen are
tree-searching using depth-first search - When looking at each subtree, we must finish with
the whole of that subtree before we can carry on.
- breadth-first inspects a tree level by level,
starting from root. - Our example tree then gives the following result.
-gt 1,2,5,6,3,4,7
11breadth-first implementation
- When we look at a node, we record the children of
that node into a queue created for storing next
level nodes (next level queue). - Example if we have
12breadth-first implementation(2)
- When we look at the root, we record itsildren
into next level queue. Therefore, at this stage,
next level queue has and . - this level queue next level queue clear next
level queue - Inspect this level queue.
- Find Now we insert the children of into next
level queue - At this stage, next level queue a,
- Continue inspecting this level queue,
- we find Now we insert the children of - into
next level queue - At this stage, next level queue a,,d,e
- No more member in this level queue, therefore let
this level queue next level queue. - At this stage, this level queue a,,d,e
- Now starting with this level queue again. And so
on.
13Binary Tree
- Each node can have at most 2 branches.
- It is possible to have one branch at every level,
meaning it is a linked list (skewed tree).
14full binary tree or perfectly balanced tree
- A complete triangle, no missing leaf.
15complete tree
- Full up to the level before the highest level.
- At the highest level, leaves must fill from left
to right.
Full is complete, but complete is not full.
16complete tree(2)
- If we put members of a complete binary tree into
an array, breadth-first - Member at the i_th position will have its left
child at the 2i1 th position. - And its right child will be at 2i2 th position.
- A parent of i is at the (i-1)/2 th position (no
decimal)
A N E T O X
Example left child of E must have index
221 5. It is X. The parent of E must have
index (2-1)/2 0
17two-tree
- Must be empty tree or
- All non-leaf must have 2 branches.
18Binary tree Theory
- Let
- leaves(t) be the number of leaves in the tree t.
- n(t) be the number of nodes of tree t.
- height(t) be the height of t.
- leftsubtree(t) be the left subtree of t.
- rightsubtree(t) be the right subtree of t.
- max(a,b) be a maximum value from a and b.
19For a non empty binary tree, we have the
following relations
If t is a two-tree, then
Then t is definitely a two-tree.
If
If t is a full tree,
20If
, then t is a full tree
21Proving
base case is when our tree has only its root.
Which is true.
inductive case is when our tree has height h. Let
We must prove that when t has height h1, then
22We know that
We can substitute n(t) into our equation.
23Fact about External Path length
- Let t be a binary tree that has root. External
Path length, E(t), is the sum of the depth of all
leaves. - If t is a binary tree that has leaves(t) k
(more than 0), then
24Binary Search Tree
- Is an empty tree or a tree that
- Every member in leftsubtree(t) has smaller value
than the member at the root. - Every member in rightsubtree(t) has larger value
than the member at the root. - Both leftsubtree(t) and rightsubtree(t) are also
binary search trees.
25- Finding a member is easy. Let us try to find 4.
When we look at each node, we know immediately
which side of the tree that we should carry on
looking. - Therefore the searching time only depends on the
height of our tree. The height is a log of n(t).
26- class BinaryNode
-
- // Constructors
- BinaryNode( Comparable theElement )
-
- this( theElement, null, null )
-
- BinaryNode( Comparable theElement,
BinaryNode lt, BinaryNode rt ) -
- element theElement
- left lt
- right rt
-
- // Friendly data accessible by other
package routines - Comparable element // The data in
the node - BinaryNode left // Left child
- BinaryNode right // Right child
27- // BinarySearchTree class
- //
- // CONSTRUCTION with no initializer
- //
- // PUBLIC
OPERATIONS - // void insert( x ) --gt Insert x
- // void remove( x ) --gt Remove x
- // Comparable find( x ) --gt Return item
that matches x - // Comparable findMin( ) --gt Return smallest
item - // Comparable findMax( ) --gt Return largest
item - // boolean isEmpty( ) --gt Return true if
empty else false - // void makeEmpty( ) --gt Remove all
items - // void printTree( ) --gt Print tree in
sorted order
private BinaryNode root
28- public class BinarySearchTree
-
- private BinaryNode root
- /
- Construct the tree.
- /
- public BinarySearchTree( )
-
- root null
-
- /
- Insert into the tree duplicates are
ignored. - _at_param x the item to insert.
- /
- public void insert( Comparable x )
-
- root insert( x, root )
29- /
- Remove from the tree. Nothing is done
if x is not found. - _at_param x the item to remove.
- /
- public void remove( Comparable x )
-
- root remove( x, root )
-
- /
- Find the smallest item in the tree.
- _at_return smallest item or null if
empty. - /
- public Comparable findMin( )
-
- return elementAt( findMin( root ) )
-
30- /
- Find the largest item in the tree.
- _at_return the largest item of null if
empty. - /
- public Comparable findMax( )
-
- return elementAt( findMax( root ) )
-
- /
- Find an item in the tree.
- _at_param x the item to search for.
- _at_return the matching item or null if
not found. - /
- public Comparable find( Comparable x )
-
- return elementAt( find( x, root ) )
-
31- /
- Make the tree logically empty.
- /
- public void makeEmpty( )
-
- root null
-
- /
- Test if the tree is logically empty.
- _at_return true if empty, false
otherwise. - /
- public boolean isEmpty( )
-
- return root null
-
32- /
- Print the tree contents in sorted
order. - /
- public void printTree( )
-
- if( isEmpty( ) )
- System.out.println( "Empty tree"
) - else
- printTree( root )
-
- /
- Internal method to get element field.
- _at_param t the node.
- _at_return the element field or null if t
is null. - /
- private Comparable elementAt( BinaryNode
t ) -
- return t null ? null t.element
33- /
- Internal method to insert into a
subtree. - _at_param x the item to insert.
- _at_param t the node that roots the tree.
- _at_return the new root.
- /
- private BinaryNode insert( Comparable x,
BinaryNode t ) -
- / 1/ if( t null )
- / 2/ t new BinaryNode( x, null, null
) - / 3/ else if( x.compareTo( t.element ) lt 0
) - / 4/ t.left insert( x, t.left )
- / 5/ else if( x.compareTo( t.element ) gt 0
) - / 6/ t.right insert( x, t.right )
- / 7/ else
- / 8/ // Duplicate do nothing
- / 9/ return t
-
34- /
- Internal method to remove from a
subtree. - _at_param x the item to remove.
- _at_param t the node that roots the tree.
- _at_return the new root.
- /
- private BinaryNode remove( Comparable x,
BinaryNode t ) -
- if( t null )
- return t // Item not found do
nothing - if( x.compareTo( t.element ) lt 0 )
- t.left remove( x, t.left )
- else if( x.compareTo( t.element ) gt 0
) - t.right remove( x, t.right )
- else if( t.left ! null t.right !
null ) // Two children -
- t.element findMin( t.right
).element - t.right remove( t.element,
t.right ) -
35Replacing a deleted element with the smallest
value of its right subtree.
36Replacing a deleted node with its subtree. This
happens when the node only has one subtree.
37- /
- Internal method to find the smallest
item in a subtree. - _at_param t the node that roots the tree.
- _at_return node containing the smallest
item. - /
- private BinaryNode findMin( BinaryNode t
) -
- if( t null )
- return null
- else if( t.left null )
- return t
- return findMin( t.left )
-
38- /
- Internal method to find the largest
item in a subtree. - _at_param t the node that roots the tree.
- _at_return node containing the largest
item. - /
- private BinaryNode findMax( BinaryNode t
) -
- if( t ! null )
- while( t.right ! null )
- t t.right
- return t
-
39- /
- Internal method to find an item in a
subtree. - _at_param x is item to search for.
- _at_param t the node that roots the tree.
- _at_return node containing the matched
item. - /
- private BinaryNode find( Comparable x,
BinaryNode t ) -
- if( t null )
- return null
- if( x.compareTo( t.element ) lt 0 )
- return find( x, t.left )
- else if( x.compareTo( t.element ) gt 0
) - return find( x, t.right )
- else
- return t // Match
-
40- /
- Internal method to print a subtree in
sorted order. - _at_param t the node that roots the tree.
- /
- private void printTree( BinaryNode t )
-
- if( t ! null )
-
- printTree( t.left )
- System.out.println( t.element )
- printTree( t.right )
-
-
41- // Test program
- public static void main( String args
) -
- BinarySearchTree t new
BinarySearchTree( ) - final int NUMS 4000
- final int GAP 37
- System.out.println( "Checking... (no
more output means success)" ) - for( int i GAP i ! 0 i ( i
GAP ) NUMS ) - t.insert( new MyInteger( i ) )
- for( int i 1 i lt NUMS i 2 )
- t.remove( new MyInteger( i ) )
42- if( NUMS lt 40 )
- t.printTree( )
- if( ((MyInteger)(t.findMin(
))).intValue( ) ! 2 - ((MyInteger)(t.findMax(
))).intValue( ) ! NUMS - 2 ) - System.out.println( "FindMin or
FindMax error!" ) - for( int i 2 i lt NUMS i2 )
- if( ((MyInteger)(t.find( new
MyInteger( i ) ))).intValue( ) ! i ) - System.out.println( "Find
error1!" ) - for( int i 1 i lt NUMS i2 )
-
- if( t.find( new MyInteger( i ) )
! null ) - System.out.println( "Find
error2!" ) -
-
-