You are on page 1of 10

Characteristics of Tree

1. Nodes connected by edges


2. Can be classified by maximum number of children
3. Trees do not have cycles or loops

Full and complete tree

If number of nodes = 7
H eight(h) = log 2 7
Complexity = O( log 2 7 )

Thus, the general complexity is O( log 2 m ) .

Why is the height of the binary tree log2n?


Lets suppose we have a sorted binary tree
Then the number of nodes is given by,
Number of nodes(N) = 20 + 21 + 22 +

23 +...+ 2height


= 2height+1
-1
Thus, the above equation can be written as

N = 2height+1
-1
Taking log on both sides we get,

N = 2height+1
-1

N + 1 = 2height +1

Taking log on both sides,


log2(N + 1) = log2(2height
+ 1
)
log2(N
+ 1) = (height + 1) * log22
log2(N
+ 1) - 1 = height * 1
height = log2(N
+ 1) - 1
Full Tree
All the leaves of the tree must
be at same height and every
non-leaf node must have
exactly n children.

Complete Tree
The non-leaf node must have
a child node to the left.

Uses of Trees:
1. Tree Traversal
2. Represents a recursive data structure

2
Explanation for Tree Traversal:
1. Preorder:
Example:
PreOrder(TreeNode n)
process n.value(visit the rest)
preorder(n.left);
preorder(n.right);

Class TreeNode {
TreeNode left;
TreeNode right;
V value;
}
Example of pre-order:
Q
|
|-------------|--------|
| |
Z M
|
|-------------|-------------|
| |
X P

Output:
Q Z M X P

3
2. In-order
Example:
inOrder(TreeNode n)
inOrder(n.left);
process n.value(visit the rest)
inOrder(n.right);

Class TreeNode {
TreeNode left;
TreeNode right;
V value;
}

Example of In-order:
12
|
|-------------|--------|
| |
9 15
|-------------|
| |
7 10

Output:
7 9 10 12 15

4
3. Postorder
Example:
PostOrder(TreeNode n)
PostOrder(n.left);
PostOrder(n.right);
process n.value(visit the rest)

Class TreeNode {
TreeNode left;
TreeNode right;
V value;
}

Example of Post-order:
M
|
|-------------|--------|
No node |
Q
|
|
|--------|--------|
| |
X P

Output:
X P Q M

4. Level-order

5
More complicated, requires extra data structure such as queue/list to
create the necessary order

Traverse (left child)


Traverse (right child)
Visit node

Example of In-order:
A
|
|-------------|--------|
| |
B C
|-------------|
| |
D E

Output:
A B C D E

Decision Trees
A tree whose node represents decision points, and whose children
represent the options available.

Binary Search Tree:


A tree whose elements are organized to facilitate finding a particular
element when needed.
Example:
9
|
|
|-----------------------------------|
<9 >=9

The complexity of binary search tree is 0(log2n)


6
To search for a particular value in a BST:
- Start at the root
- Compare target to element at current node
- Move left from the current node if target is less than element in the
current node
- Move right from the current node if target is greater than element in
the current node

12 (13 > 12)


|
|
|-------------------------------------------------|-------------------------------------------------|
| |
5 (13<17) 17
| |
|------------------------| |------------------------|
4 6 13 (13 found)
Contains 13? True.
Build a tree from adding elements
From the list 1,2,3,4,5,16,19
1 <-------Degenerate tree
|------- |
| 2
null |------- |
| 3
null |------- |
| 4
null |------- |
| 5
null |------- |
| 16
null |------- |
| 19
null

7
AddingElements
- Adding elements is similar to finding as element
- New elements are added as leaf nodes
- Start at the root, follow path directed by existing elements until you find no
child in the desired direction
- Then add new element

RemovinganElement
- After removing an element the resulting tree must still be valid
- Three distinct situation must be considered before removing an element
- The node to remove is a leaf
- The node to remove has one child
- The node to remove has two child

Case 1:

6
|------- |------- |
| |
3 9
|
|------- |
2 4

Remove(9)

Resulting tree:
6
|------- |------- |
| |
3 null
|
|------- |
2 4

8
Case 2:

6
|------- |------- |
| |
3 null
|
|
2

Remove(3)

6
|------- |------- |
| |
4 null


Example 2:
9
|
|
|----------------|----------------|
7 12
|
|----------------|----------------|
11 13

9
BalancingBSTs

A right rotation:

10

You might also like