Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Trees. A Binary tree is a tree in which every node has none, one, or at most, two children A Proper Binary Tree has two children or no children.

Similar presentations


Presentation on theme: "Binary Trees. A Binary tree is a tree in which every node has none, one, or at most, two children A Proper Binary Tree has two children or no children."— Presentation transcript:

1 Binary Trees

2 A Binary tree is a tree in which every node has none, one, or at most, two children A Proper Binary Tree has two children or no children To make every binary tree into a proper binary tree, we add empty nodes as leaves

3 Properties of a Proper Binary Tree Level = all nodes at the same depth Level d has at most 2 d nodes Level 1 0 2 Max Nodes 2 1 4 Number of external nodes: at least h+1 and at most 2 h Number of internal nodes: at least h and at most 2 h – 1 Total no. nodes: at least 2h + 1 and at most 2 h+1 – 1 Number of external nodes = number of internal nodes + 1 Height(h) 1 2 0

4 Binary Tree: Array-Based Implementation Binary trees can be stored as an implicit data structure in an array, and if the tree is a complete binary tree, this method wastes no space. In this compact arrangement, if a node has an index i, its children are found at indices 2i+1 and 2i+2, while its parent (if any) is found at index floor((i-1)/2) (assuming the root has index zero).

5 store node #0 in array location 0, node #1 in array location 1, etc. i0123456... t [i ]OMTCEPU... CEPU TM O 21 3456 0 Binary Tree: Array-Based Implementation

6 But, unless each level of the tree is full so that there are no "dangling limbs," there can be much wasted space in the array. ECMUT O P

7 Binary Tree: Node-Based Implementation left element parent right left r NULL right NULL s parent NULL t parent NULL T

8 Node for a Binary Tree struct Node { char element; Node *left; Node *right; Node *parent; Node() : element( char() ){// default constructor parent=right=left=NULL;} Node* sibling() const {// get the sibling return(this==parent->left ? parent->right : parent- >left); } };

9 Data Members of a Binary Tree template class LinkedBinaryTree{ private: Node *theRoot; int sz; };

10 Constructor of a Binary Tree template class LinkedBinaryTree{ public: LinkedBinaryTree() {// constructor theRoot = new Node; sz = 1; }; }

11 Operations of the LinkedBinaryTree  Generic Methods: bool isEmpty() intsize() iterchildren( pos ) iterelements() iterpositions() Update methods: void swapElements( w, x ) void replaceElement( pos, e ) void expandExternal( pos ) Void removeAboveExternal( pos )

12 Algorithm Analysis on the Operations of the BinaryTree isEmpty() O(1) size() O(1) children( pos ) O(1) elements() O(n) positions() O(n) swapElements( w, x ) O(1) replaceElement( pos, e ) O(1) expandExternal( pos ) O(1) removeAboveExternal( pos ) O(1)

13 Operations of the LinkedBinaryTree Accessor methods: posroot() posparent( pos ) boolisInternal( pos ) boolisExternal( pos ) boolisRoot( pos ) posleftChild ( pos ) posrightChild( pos ) possibling( pos )

14 Creating a LinkedBinaryTree Object LinkedBinaryTree T; Creates the tree with an empty external node. T.expandExternal(T.root()); Adds two new, empty nodes as children. A T.replaceElement( T.root(),'A'); Inserts the element.

15 Instantiating a LinkedBinaryTree Object LinkedBinaryTree T; Creates the tree with an empty external node. theRoot //Default constructor LinkedBinaryTree(){ theRoot = new Node; sz = 1; }

16 Adding an Element to a LinkedBinaryTree Object – Step 1 Adds two new, empty nodes as children. theRoot void expandExternal( const Position& v ){ expandExternal(nodePtr(v)); } void expandExternal( Node *n ){ n->left = new Node; n->left-parent = n; n->right = new Node; n->right->parent = n; sz += 2; }

17 Adding an Element to a LinkedBinaryTree Object – Step 2 A Inserts the element. theRoot void replaceElement( Node *n, const Object& o ){ n->element = o; }

18 Removing an Element from a LinkedBinaryTree Object Node* removeAboveExternal( Node *n ){ Node *p = n->parent; Node *s = n->sibling(); if( isRoot(p) ) setRoot(s); else{ Node *g = p->parent; if( p == g->left ) g->left = s; else g->right = s; s->parent = g; } delete n; delete p; sz -= 2; return s; } A theRoot p sn s

19 Removing an Element from a LinkedBinaryTree Object Node* removeAboveExternal( Node *n ){ Node *p = n->parent; Node *s = n->sibling(); if( isRoot(p) ) setRoot(s); else{ Node *g = p->parent; if( p == g->left ) g->left = s; else g->right = s; s->parent = g; } delete n; delete p; sz -= 2; return s; } theRoot B A C D p ns g B A C s g


Download ppt "Binary Trees. A Binary tree is a tree in which every node has none, one, or at most, two children A Proper Binary Tree has two children or no children."

Similar presentations


Ads by Google