Tree 1
Tree 1
Chapter 4 of textbook 1
Why we need to learn Tree structure
• Efficient Data Organization
– Examples: File directories, organizational charts
• Representation of Hierarchical Data
– Examples: family trees, decision trees
• Fast Search and Retrieval
– BST, AVL Tree
• Foundational Knowledge for Advanced Concepts
– Graph, priority queues.
Formal Definition
A tree is a collection of nodes.
There is a starting node known as root node.
Every node other than the root has a parent node.
Nodes may have any number of children.
Nodes with no children are known as leaves
Root directory
EE220 CSE260 TCOM500
EE220, CSE260, TCOM 500
EE220: Lecture Notes, HW Lec HW
Using pointers
A node has a pointer to all its children
Since a node may have many children, the child pointers
have a linked list. A
B C D
E F
H
Deletion of a child B:
Children of B are first made children of the A
parent of B
Node B is deleted. B C D
E F
B C D
E
F
A
C D
E
F
Deletion of the root:
One of the children becomes the new root:
Other children of old root become the children of the new root
B C D
E F G
B D
G
E F
Tree Traversal (Tree Search)
Many algorithms involve walking through a tree,
and performing some computation at each node
B C D
E F G
Pre-order:
First visit a node, then with its children (visit means
performing some computation at this node)
A→B→E→F→C→G→D
DFS: Post-order
A
B C D
E F G
Post-order:
First visit its children, then return to the node. (visit means
performing some computation at this node)
E→F→B→G→C→D→A
BFS
A
B C D
E F G
BFS:
Visit all nodes at the current level before moving to the next.
A → B→C→D→E→F-G
Binary Trees
4→2→5→1→6→3→7
Quiz
Please give the order of nodes we visit:
DFS Pre-order
DFS Post-order
DFS In-order
BFS (Level-order)
Implementation of a binary tree
node
struct BinaryTreeNode
{
ElementType Element;
BinaryTreeNode * left;
BinaryTreeNode * right;
};
Homework
Time complexity?
Implementation of In-order
Recursive function Non-recursive function
Homework
Implementation of BFS
1 4 10
1 4 10
Another Examples
5 8
4 8 5 11
1 7 11 2 7 6 10 18
3 4 15 20
Search for 10
3 8
Sequence Traveled:
1 4 10 5, 8, 10
Found!
Sequence Traveled:
5, 3, 4
Not found!
Quiz: Non-recursive version of
search
Find Min and Find Max
Find Min: start at the root and go left as long as there is a left child. The
stopping leaf is the smallest element.
Find Max: start at the root and go right as long as there is a right child.
The stopping leaf is the greatest element.
Complexity: O(d)
5
3 8
1 4 10
Travel 5, 3, 1
Return 1;
Travel 5, 8, 10
Return 10;
Quiz: implementation
Provide the recursive version and non-recursive
version of Find Min and Find Max
Find_Min(root): Find_Min(root):
T = root; T = root;
If(T != NULL) If(T != NULL)
while(T→left != NULL) if (T→ left != NULL)
T=T→left; Find_Min(T-left);
return(T); return T;
Insertion
An insertion will be performed at a leaf
node:
– Any empty node is a possible location for
an insertion
Insertion
For example, this node may hold 48, 49,
or 50
Insertion
An insertion at this location must be 35,
36, 37, or 38
Insertion
This empty node may hold values from 71
to 74
Insertion Algorithm
Complexity? O(d)
Quiz: Erase
Blackboard example:
– In the binary search tree generated
previously:
• Erase47
• Erase21
• Erase45
• Erase31
• Erase36
Next week
• Online Assignment 2
– Duration :45 minutes
– Content: Stack, Queue, Tree
• AVL Tree