Lec 13 TreesI

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

CS214 – Data Structures

Lecture 13: Trees I

Slides by
Mohamed El-Ramly, PhD
Basheer Youssef, PhD
Agenda
1. Introduction to Binary Trees
2. Implementing Binary Trees
3. Searching Binary Search Trees
4. Tree Traversal …1. Breadth-First ….2. Depth-First
5. Insertion
6. ……1. Deletion by Merging …..2. By Copying
7. ……2. Balanced Trees
8. .
9. Heaps / Heap Sort
10. .
2
6.1 Trees
1. Linked Lists are limited, why?

2. How can I organize a hierarchical structure (of a


university for example)
Tree Definitions
1. A tree data structure consists of nodes and
arcs.
2. Every node has one parent but may have one
or more children.
3. Top node is root. Root has no parent.
4. Nodes with no children are called leaves.
Chapter 6: Binary Trees 5
Tree Definitions
1. Each node is reachable from the root with a
unique sequence of arcs called path.
2. The number of arcs in a path is the length of the
path.
3. The level of a node is the length of the path
from the root to the node + 1.
4. The height of a non-empty tree is the maximum
level of a node in the tree.
Tree Definitions
1. The height of an empty tree is zero.
2. The height of a single node tree is 1.
3. A single node tree is the only case when the
root is also a leaf.
4. The level of any node is between 1 and the tree
height.
5. In the extreme case, when every node has one
child, how will the tree look like?
Binary Trees
1. A binary tree is a tree whose nodes have at
most two children per node.
2. A complete binary tree is a binary tree that
satisfies the following conditions:
• Every non-terminal nodes has 2 children
• Leaves are all at the same level
3. In a complete binary tree, level i+1has 2i nodes.
Complete Binary Trees
1. The number of leaves m = 2i-1.
2. non-terminal nodes k = 2i-1 -1.
3. So, a complete binary tree has 2i -1
Binary Search Trees
1. A binary search tree is a binary tree that
satisfies the following conditions:
• Every node n in its left sub-tree has a value
less than the value stored in n.
• Every node n in its right sub-tree has a value
greater than the value stored in n.
6.2 Implementing Binary Trees
1. How can we implement binary trees ?

2. Arrays?
• Advantages?
• Limitations?

3. Linked Structures?
Building Binary Trees w Arrays
Building Binary Trees w Nodes
Building Binary Trees w Nodes
(A few comments on style)

private
Chapter 6: Binary Trees 14
template <class T>
class BSTNode {
private:
T key;
BSTNode* left;
BSTNode* right;
public:
BSTNode () {left = right = 0;}
BSTNode (T& el, BSTNode* l = 0, BSTNode* r =
0){
key = el;
left = l;
right = r;
}
BSTNode* getLeft {return left;}
BSTNode* getRight {return right;}
T getKey {return key;}
template <class T>
class BST {
protected:
BSTNode<T>* root
public:
BST () {root = 0;}
void clear() {root = 0;}
bool isEmpty() {retun root == 0;}
T* search (T& el) {
BSTNode<T>* p = root;
while (p != 0)
if (el == p->getKey())
return &p->getKey();
else if (el < p->getKey())
p = getLeft();
else p = getRight();
return 0;
}
6.3 Searching in a BST
1. Trace searching for 1, 12, 31
2. What is the max num of tests
when searching in this tee?
Complexity of Searching in a BST
1. Measured by the number of comparisons done,
which depends on the nodes encountered on
the path from the root to the node searched for.
2. So, complexity is the length of the path leading
to this node +1;
3. = log n
4. Worst case = n

You might also like