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

19
Binary Trees

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

Page 1: 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.

Binary Trees

Page 2: 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.

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

Page 3: 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.

Properties of a Proper Binary Tree

Level = all nodes at the same depth Level d has at most 2d nodes

Level

1

0

2

Max Nodes

2

1

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

Height(h)

1

2

0

Page 4: 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.

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

Page 5: 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.

store node #0 in array location 0, node #1 in array location 1, etc.

i 0 1 2 3 4 5 6 . . .

t [i ] O M T C E P U . . .

C E P U

TM

••

O

21

3 4 5 6

0

Binary Tree: Array-Based Implementation

Page 6: 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.

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.

MC

E

U

T

P

O

it[i]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

20 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

40 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

21

41

0

……

E C M U T

O

P

Page 7: 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.

Binary Tree: Node-Based Implementation

leftelement

parentright

leftr

NULLright

NULLs

parentNULL NULL

t

parentNULL

T

Page 8: 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.

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

}

};

Page 9: 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.

Data Members of a Binary Tree

template < typename Object >class LinkedBinaryTree{private: Node *theRoot; int sz;};

Page 10: 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.

Constructor of a Binary Tree

template < typename Object >class LinkedBinaryTree{public: LinkedBinaryTree() { // constructor theRoot = new Node; sz = 1; };}

Page 11: 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.

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 )

Page 12: 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.

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)

Page 13: 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.

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 )

Page 14: 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.

Creating a LinkedBinaryTree Object

LinkedBinaryTree<char> T; Creates the tree with an empty external node.

T.expandExternal(T.root()); Adds two new, empty nodes as children.

AT.replaceElement( T.root(),'A'); Inserts the element.

Page 15: 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.

Instantiating a LinkedBinaryTree Object

LinkedBinaryTree<char> T; Creates the tree with an empty external node.

theRoot

//Default constructorLinkedBinaryTree(){ theRoot = new Node; sz = 1;}

Page 16: 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.

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

Page 17: 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.

Adding an Element to a LinkedBinaryTree Object – Step 2

A Inserts the element.

theRoot

void replaceElement( Node *n, const Object& o ){ n->element = o;}

Page 18: 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.

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

theRoot

s

Page 19: 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.

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

Dp

n s

g

theRoot

B

A

Csg