Binary Tree

advertisement
Binary Tree
Binary Trees – An Informal Definition
• A binary tree is a tree in which no node can
have more than two children
• Each node has 0, 1, or 2 children
Binary Trees – A Recursive Definition
1. An empty structure is a binary tree
2. If T1 and T2 are binary trees, then the
structure whose root has as its children the
roots of T1 and T2 is also a binary tree
3. Only structures generated by rules 1 and 2
are binary trees
Trees vs. Binary Trees
• No node in a binary tree may have more than 2 children, whereas
there is no limit on the number of children of a node in a tree
Types of Binary Trees
• A binary tree in which each node has exactly 0 or 2 children is called a
full binary tree – there are no degree 1 nodes
• A complete binary tree is a tree which is completely filled, with the
possible exception of the bottom level, which is filled from left to right
Properties of BT
• Min/Max number of nodes in a binary tree
whose height is h
• Min/Max height in a binary tree with n nodes
• Min/Max number of leaves/internal nodes/in a
binary tree whose height is h
Properties of Binary Trees (cont’d)
• Minimum number of nodes ?
– Minimum number of nodes in a binary tree whose
height is h
At least one node at each level
 Minimum number of nodes is h + 1
Properties of Binary Trees (cont’d)
• Maximum number of nodes ?
– Maximum number of nodes in a binary tree
whose height is h
A complete binary tree
2^(h+1) -1
Properties of Binary Trees (cont’d)
• Maximum height ?
– Maximum height in a binary tree with n nodes
• Minimum height ?
– Minimum height in a binary tree with n nodes
Properties of Binary Trees (cont’d)
• Maximum number of external nodes (or leaves)?
– Maximum number of leaves in a binary tree with
height h
• Minimum number of internal nodes?
– Maximum number of internal nodes in a binary tree
with height h
Binary Search Trees (BST)
• A binary tree with the following property:
For each node of the tree, all values stored in its left subtree (the tree whose
root is the left child) are less than the value stored in the node, and all values
stored in the right subtree are greater than the value stored in the node
Binary Search Trees (cont’d)
• A BST is a binary tree of nodes ordered in the
following way:
1.Each node contains one key (also unique)
2.The keys in the left subtree are < than the key in
its parent node
3.The keys in the right subtree are > than the key in
its parent node
4.Duplicate node keys are not allowed
BST – Linked List Vstion
template<class T>
class BSTNode {
public:
BSTNode() {
left = right = 0;
}
BSTNode(const T& el, BSTNode *l = 0, BSTNode *r = 0) {
key = el; left = l; right = r;
}
T key;
BSTNode *left, *right;
};
template<class T>
class BST {
….//methods
protected:
BSTNode<T>* root;
…//methods
}
Recursive version of search? Remember when to stop!
Binary Search Trees (cont’d)
Lab 5 discussion
Find the length of a linked list: (recursive func)
int LinkedList::length(){
return lengthFrom(head);
}
int LinkedList::lengthFrom(Node *n){
if (n==0) return 0;
else
return 1+lengthFrom(n->next);
Download