1 / 16

Binary Tree

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

Download Presentation

Binary Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 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

  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: • Each node contains one key (also unique) • The keys in the left subtree are < than the key in its parent node • The keys in the right subtree are > than the key in its parent node • Duplicate node keys are not allowed

  13. 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 }

  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); }

More Related