Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Binary Tree

2 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

3 Binary Trees – A Recursive Definition 1. An empty structure is a binary tree 2. If T 1 and T 2 are binary trees, then the structure whose root has as its children the roots of T 1 and T 2 is also a binary tree 3. Only structures generated by rules 1 and 2 are binary trees

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 BST – Linked List Vstion template 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 BST { ….//methods protected: BSTNode * root; …//methods }

14 Recursive version of search? Remember when to stop!

15 Binary Search Trees (cont’d)

16 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 ppt "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."

Similar presentations


Ads by Google