200 likes | 381 Views
Binary Trees. 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 To make every binary tree into a proper binary tree, we add empty nodes as leaves. Properties of a Proper Binary Tree.
E N D
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 • To make every binary tree into a proper binary tree, we add empty nodes as leaves
Properties of a Proper Binary Tree • Level = all nodes at the same depth • Level d has at most 2d nodes Level Max Nodes Height(h) 2 0 1 1 1 2 0 2 4 • Number of external nodes: at least h+1 and at most 2h • Number of internal nodes: at least h and at most 2h – 1 • Total no. nodes: at least 2h + 1 and at most 2h+1 – 1 • Number of external nodes = number of internal nodes + 1
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).
O M T C E P U • • • . . . i 0 1 2 3 4 5 6 t [ i ] O M T C E P U . . . Binary Tree: Array-Based Implementation 0 1 2 3 4 5 6 store node #0 in array location 0, node #1 in array location 1, etc.
i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 t [ i ] 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 20 … 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 … Binary Tree: Array-Based Implementation • 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. E C M U T P O
left left NULL NULL parent NULL parent parent NULL NULL right right element r s t T Binary Tree: Node-Based Implementation
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); } };
Data Members of a Binary Tree template < typename Object > class LinkedBinaryTree{ private: Node *theRoot; int sz; };
Constructor of a Binary Tree template < typename Object > class LinkedBinaryTree{ public: LinkedBinaryTree() { // constructor theRoot = new Node; sz = 1; }; }
Operations of the LinkedBinaryTree • Generic Methods: bool isEmpty() int size() iter children( pos ) iter elements() iter positions() • Update methods: void swapElements( w, x ) void replaceElement( pos, e ) void expandExternal( pos ) Void removeAboveExternal( pos )
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)
Operations of the LinkedBinaryTree • Accessor methods: pos root() pos parent( pos ) bool isInternal( pos ) bool isExternal( pos ) bool isRoot( pos ) pos leftChild ( pos ) pos rightChild( pos ) pos sibling( pos )
Adds two new, empty nodes as children. T.expandExternal(T.root()); Inserts the element. T.replaceElement( T.root(),'A'); A Creating a LinkedBinaryTree Object Creates the tree with an empty external node. LinkedBinaryTree<char> T;
Instantiating a LinkedBinaryTree Object theRoot Creates the tree with an empty external node. LinkedBinaryTree<char> T; //Default constructor LinkedBinaryTree(){ theRoot = new Node; sz = 1; }
Adds two new, empty nodes as children. Adding an Element to a LinkedBinaryTree Object – Step 1 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; }
Inserts the element. A Adding an Element to a LinkedBinaryTree Object – Step 2 theRoot void replaceElement( Node *n, const Object& o ){ n->element = o; }
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 p A n s theRoot s
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 A g B C p D n s theRoot A g B C s