data structures and algorithms introduction to binary and binary search trees

Post on 03-Jan-2016

14 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Data Structures and Algorithms Introduction to Binary and Binary Search Trees Prepared by: S. Kondakci. Tree. - PowerPoint PPT Presentation

TRANSCRIPT

Data Structures and Algorithms

Introduction to Binary and Binary Search Trees

Prepared by: S. Kondakci

Tree

A tree is a recursively structured set of branches, where each branch consists of a collection of N nodes, one of which is the root and N-1 edges. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root, and zero or more non-empty (sub) trees T1, T2, …, Tk each of whose roots are connected by a directed edge from r.

Tree terminology

• The root of each subtree is said to be a child of r and r is said to be the parent of each subtree root.

• Leaves: nodes with no children (also known as external nodes)

• Internal Nodes: nodes with children

• Siblings: nodes with the same parent

Tree terminology (continued)

• A path from node n1 to nk is defined as a sequence of nodes n1, n2, …, nk such that ni is the parent of ni+1 for 1<= i <= k.

• The length of this path is the number of edges on the path namely k-1.

• The length of the path from a node to itself is 0.• There is exactly one path from the root to each

node.

Tree terminology (continued)

• Depth: the length of the unique path from the root to a node.– The depth of a tree is equal to the depth of its

deepest leaf.

• Height: the length of the longest path from a node to a leaf.– All leaves have a height of 0

Implementation of Binary trees

• A binary tree is a tree in which no node can have more than two children.

• Each node has an element, a reference to a left child and a reference to a right child.

struct TreeNode{ int element; TreeNode *left_child; TreeNode *right_child;};

Picture of a binary tree

a

b c

d e

g h i

l

f

j k

General Tree traversals

• A binary tree is defined recursively: it consists of a root, a left subtree, and a right subtree

• To traverse (or walk) the binary tree is to visit each node in the binary tree exactly once

• Tree traversals are naturally recursive• Since a binary tree has three “parts,” there are six

possible ways to traverse the binary tree:

– root, left, right– left, root, right– left, right, root

– root, right, left– right, root, left– right, left, root

Traversing the Tree

• There are three common methods for traversing a binary tree and processing the value of each node: – inorder– preorder– postorder

• Each of these methods is best implemented as a recursive function.

Inorder Traversal (LVR)

1. The node’s left subtree is traversed.

2. The node’s data is processed.

3. The node’s right subtree is traversed.

Preorder Traversal (VLR)

1. The node’s data is processed.

2. The node’s left subtree is traversed.

3. The node’s right subtree is traversed.

Postorder Traversal (LRV)

1. The node’s left subtree is traversed.

2. The node’s right subtree is traversed.

3. The node’s data is processed.

Inorder traversal (LVR)

• In inorder, the root is visited in the middle• Here’s an inorder traversal to print out all

the elements in the binary tree:

void inorderPrint(BinaryTree *bt) { if (bt == null) return; inorderPrint(bt -> leftChild); printf(“%d \n”,bt -> element); inorderPrint(bt -> rightChild);}

Preorder traversal (VLR)

• In preorder, the root is visited first• Here’s a C code implementing preorder

traversal to print out all the elements in a binary tree:

void preorderPrint(BinaryTree *bt) { if (bt == null) return; printf(“%d \n”,bt -> element); preorderPrint(bt -> leftChild); preorderPrint(bt -> rightChild);}

Postorder traversal (LRV)

• In postorder, the root is visited last• Here’s a postorder traversal to print out all

the elements in the binary tree:

void postorderPrint(BinaryTree bt) { if (bt == null) return; postorderPrint(bt.leftChild); postorderPrint(bt.rightChild); printf(“%d \n”,bt -> element);

}

Tree traversals Illustration using “flags”

• The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node, as follows:

• To traverse the tree, collect the flags:

Preorder (VLR)

Inorder (LVR)

Postorder (LRV)

A

B C

D E F G

A

B C

D E F G

A

B C

D E F G

A B D E C F G D B E A F C G D E B F G C A

Inorder Search Through Binary Tree (LVR)

14 34

33 23 35 55

89

12

75

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14 75

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14 75 23

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14 75 23 12

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14 75 23 12

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14 75 23 12

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14 75 23 12 35

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14 75 23 12 35 34

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14 75 23 12 35 34

Inorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 14 75 23 12 35 34 55

Preorder Search Through Binary Tree (VLR)

14 34

33 23 35 55

89

12

75

12

Preorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

12 14

Preorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

12 14 33

Preorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

12 14 33 89

Preorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

12 14 33 89 23

Preorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

12 14 33 89 23 75

Preorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

12 14 33 89 23 75 34 35

Preorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

12 14 33 89 23 75 34 35 55

Postorder Search Through Binary Tree (LRV)

14 34

33 23 35 55

89

12

75

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14 35

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14 35

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14 35

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14 35

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14 35 55

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14 35 55 34

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

89 33 75 23 14 35 55 34 12

Postorder Search Through Binary Tree

14 34

33 23 35 55

89

12

75

Other traversals

• The other traversals are the reverse of these three standard ones– That is, the right subtree is traversed before the left

subtree is traversed

• Reverse preorder: root, right subtree, left subtree• Reverse inorder: right subtree, root, left subtree• Reverse postorder: right subtree, left subtree, root

Arithmetic Expression Tree

• Binary tree for an arithmetic expression– internal nodes: operators– leaves: operands

• Example: arithmetic expression tree for the expression (2 (5 - 1) + (3 2))

2

5 1

3 2

Print Arithmetic Expressions

• inorder traversal:– print “(“ before traversing

left subtree– print operand or operator

when visiting node– print “)“ after traversing right

subtree

void printTree(t) //binary operands only

if (t.left != null)print("(");

printTree (t.left);print(t.element );if (t.right != null)

printTree (t.right);print (")");

2

5 1

3 2((2 (5 - 1)) + (3 2))

Evaluate Arithmetic Expressions

• postorder traversal– Recursively evaluate

subtrees– Apply the operator

after subtrees are evaluated

int evaluate (t)//binary operators only

if (t.left == null) //external node return t.element;else //internal node x = evaluate (t.left); y = evaluate (t.right); let o be the operator t.element z = apply o to x and y return z;

2

5 1

3 2

Binary Trees: Recursive Definition

ROOT OF TREE T

T1 T2

SUBTREES

*left_child *right_child

Binary Search Trees

6

92

41 8

Binary Search Tree

• A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property:– Let u, v, and w be three

nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w)

• External nodes do not store items

• An inorder traversal of a binary search trees visits the keys in increasing order

6

92

41 8

Samller keys

Greater keys

Binary Search Tree Operations

• insertElement(k)

• findElement(k)

• removeElement(k)

Searching in a Binary Search Tree

• To search for a key k, we trace a downward path starting at the root

• The next node visited depends on the outcome of the comparison of k with the key of the current node

• If we reach a leaf, the key is not found and we return a null position

• Example: find(4)

Algorithm find (k, v)if T.isExternal (v)

return Position(null)if k key(v)

return find(k, T.leftChild(v))else if k key(v)

return Position(v)else { k key(v) }

return find(k, T.rightChild(v))

6

92

41 8

Insertion

• To perform operation insertElement(k, o), we search for key k

• Assume k is not already in the tree, and let w be the leaf reached by the search

• We insert k at node w and expand w into an internal node

• Example: insert 5

6

92

41 8

6

92

41 8

5

w

w

Deletion of External Nodes• To perform operation

removeElement(k), we search for key k

• Assume key k is in the tree, and let let v be the node storing k

• If node v has a leaf child w, we remove v and w from the tree with operation removeAboveExternal(w): also reconnects the broken tree!

• Example: remove 4

6

92

41 8

5

vw

6

92

51 8

Deletion of Internal Nodes

• We consider the case where the key k to be removed is stored at a node v whose children are both internal– we find the internal node w that

follows v in an inorder traversal

– we copy key(w) into node v

– we remove node w and its left child z (which must be a leaf) by means of operation removeAboveExternal(z)

• Example: remove 3

3

1

8

6 9

5

v

w

z

2

5

1

8

6 9

v

2

Binary Search Trees in C

• We will use two struct definitions as ADT:– The BinaryNode simply defines individual

nodes in the tree.– The BinarySearchTree maintains a pointer

to the root of the binary search tree.

Binary Search Trees in C

element, *left, *right

Functions

The Binary Tree Node struct BinaryNode

{

int element;

BinaryNode *left;

BinaryNode *right;

};

*left *right

el

Binary Search Trees in C

*root

find/insert/remove functions

The Binary Search Tree ADT

struct BinarySearchTree

{

BinaryNode *root;

const int NOT_FOUND;

};void insert (const int x);void remove (const int x);

*left *right

el*root

Binary Search Tree Methods

• bool find(): Search operation returns true if a value is found in the tree.

• The Find operation returns a pointer to the node in a tree T that has item X, or NULL if there is no such node.

• void insert(int X): The Insert operation inserts a new node (with item X) into the tree T.

• void remove(int X):The Remove operation removes the node (with item X) from the tree T.

Searching a Binary TreeSearchNode returns true if a value is found in the tree, or false otherwise. bool searchNode(int num){

TreeNode *nodePtr = root;

while (nodePtr){

if (nodePtr->value == num)return true;

else if (num < nodePtr->value)nodePtr = nodePtr->left;

elsenodePtr = nodePtr->right;

}return false;

}

The Find Operation

// Returns the element at the node

const int &getElement (BinaryNode *t)

{

return (t == NULL ? NOT_FOUND : t element);

}

// Start the search at the root node

const int &find (const int & x)

{

return elementAt (find (x, root));

}

element

*t

*root

The Find Operation…

BinaryNode *find (const int &x, BinaryNode *t)

{

if ( t == NULL )

return NULL;

else if ( x < t element )

return find ( x, t left );

else if ( x > t element )

return find ( x, t right );

else

return t;

/* Match; return a poointer to the node */

}

*root

6

2

5

9

7 10

Suppose I try to find thenode with 7 in it. First godown the right subtree, thengo down the left subtree.

Search Left: The FindMin Operation…

BinaryNode *findMin (BinaryNode *t)

{

if ( t == NULL )

return NULL;

else if ( t left == NULL )

return t;

return findMin (t left);

}

*root

6

2

5

9

7 10This function returns a pointer to the node

containing the element with smallest value in the tree. It does so by following the leftbranch of the tree.

Search Right:The FindMax Operation…

BinaryNode *findMax (BinaryNode *t)

{

if ( t == NULL )

return NULL;

else if ( t right == NULL )

return t;

return findMax (t right);

}

*root

6

2

5

9

7 10

This function returns a pointer to the node containing thelargest element in the tree. It does so by following the rightside of the tree.

The Insert Operation• Insertion is conceptually simple. To insert X into a

tree T, proceed down the tree as you would with a find. If X is found, do nothing. Otherwise insert X at the last spot on the path that has been traversed.

suppose I want toinsert a 1 into this tree…

*root

6

2

5

9

7 101

The Insert Operationvoid insert (const int &x, BinaryNode *&t)

{

if (t == NULL)

t = new BinaryNode (x, NULL, NULL);

else if (x < t element)

insert(x, t left);

else if( x > t element )

insert(x, t right);

else

; // Duplicate entry; do nothing

}

*root

6

2

5

9

7 10

t left

1

NULLNULL

t

Note the pointer t is passedusing call by reference. In this casethis means tleft will be changed to t.

t=malloc(sizeof(BinaryNode));

t->x=val;t->left=t->right =NULL

The Removal Operation

• If the node to be removed is a leaf, it can be deleted immediately.

• If the node has one child, the node can be deleted after its parent adjusts a link to bypass the deleted node. *root

6

2

5

9

7 10

What if the node with 2 is deleted?

Removal…

*root

6

2

5

9

7 10

*root

6

2

5

9

7 10

t

t right

Set t = t right

Removal…

• If the node to be removed has two children, the general strategy is to replace the data of this node with the smallest data of the right subtree.

• Then the node with the smallest data is now removed (this case is easy since this node cannot have two children).

Removal…

*root

6

2

5

9

7 101

3

4

Remove the node with 2 again…

*root

6

3

5

9

7 101

3

4

The Removal Operation void remove (const int &x, BinaryNode *&t) {

if ( t == NULL ) return; // Item doesn’t exist; do nothing

if ( x < t element ) remove( x, t left );

else if( x > t element ) remove( x, t right );

else if( t left != NULL && t right != NULL ) // Two kids

{

t element = findMin( t right ) element;

remove( t element, t right );

}

else // One kid7

{

BinaryNode *oldNode = t;

t = ( t left != NULL ) ? t left : t right;

free (oldNode);

}

}

Binary Search Tree Operations

• Creating a Node: We will demonstrate binary tree operations

• The basis of our binary tree node is the following struct declaration:

struct TreeNode{

int value;TreeNode *left;TreeNode *right;

};

BinaryTree.hstruct BinaryTree { struct TreeNode *root;

const int NOT_FOUND;

};

struct TreeNode {

int value;TreeNode *left;TreeNode *right;

};

TreeNode *root;void destroySubTree(TreeNode *);void deleteNode(int, TreeNode *&);void makeDeletion(TreeNode *&);void displayInOrder(TreeNode *);void displayPreOrder(TreeNode *);void displayPostOrder(TreeNode *);

BinaryTree.h (continued)

/* Initialize BinaryTree and make root = NULL; */void insertNode(int);bool searchNode(int);void remove(int);void showNodesInOrder(void)

{ displayInOrder(root); }void showNodesPreOrder()

{ displayPreOrder(root); }void showNodesPostOrder()

{ displayPostOrder(root); }

Binary Search Tree Operations

• Inserting a Node:First, a new node is allocated and its value member is initialized with the new value.

• The left and right child pointers are set to NULL, because all nodes must be inserted as leaf nodes.

• Next, we determine if the tree is empty. If so, we simply make root point to it, and there is nothing else to be done. But, if there are nodes in the tree, we must find the new node's proper insertion point.

Binary Search Tree Operations

• If the new value is less than the root node's value, we know it will be inserted somewhere in the left subtree. Otherwise, the value will be inserted into the right subtree.

• We simply traverse the subtree, comparing each node along the way with the new node's value, and deciding if we should continue to the left or the right.

• When we reach a child pointer that is set to NULL, we have found out insertion point.

The insertNode() Function

void insertNode(int num){

TreeNode *newNode,// Pointer to a new node *nodePtr;// Pointer to traverse the tree

// Create a new nodenewNode = new TreeNode; // Watch out C++ notation!newNode->value = num;newNode->left = newNode->right = NULL;

if (!root) // Is the tree empty?root = newNode;

else{

nodePtr = root;

The insertNode() Function

while (nodePtr != NULL){

if (num < nodePtr->value){

if (nodePtr->left)nodePtr = nodePtr->left;

else{

nodePtr->left = newNode;break;

}}

The insertNode() Function

else if (num > nodePtr->value){

if (nodePtr->right)nodePtr = nodePtr->right;

else{

nodePtr->right = newNode;break;

}} else{

printf("Duplicate value found in tree.\n“);break;

}}

}}

Let’s Build A Bin Search-Ttree

// This program builds a binary tree with 5 nodes.

#include <stdio.h>#include "BinaryTree.h“

void main(void){

IntBinaryTree tree;

printf("Inserting nodes... “);tree.insertNode(5);tree.insertNode(8);tree.insertNode(3);tree.insertNode(12);tree.insertNode(9);printf("Done.\n“);

}

How the Binary Tree Looks Like

Note: The shape of the tree is determined by the order in which the values are inserted. The root node in the diagram above holds the value 5 because that was the first value inserted.

The displayInOrder() Function

void displayInOrder(TreeNode *nodePtr){

if (nodePtr){

displayInOrder(nodePtr->left);printf(“%d \n”, nodePtr->value);displayInOrder(nodePtr->right);

}}

The displayPreOrder() Function

void IntBinaryTree::displayPreOrder(TreeNode *nodePtr){

if (nodePtr){

printf(“%d \n”, nodePtr->value); displayPreOrder(nodePtr->left);displayPreOrder(nodePtr->right);

}}

The displayPostOrder() Function

void displayPostOrder(TreeNode *nodePtr){

if (nodePtr){

displayPostOrder(nodePtr->left);displayPostOrder(nodePtr->right);printf(“%d \n”, nodePtr->value);

}}

Buil A B-Tree with 5 Nodes#include <stdio.h>#include "BinaryTree.h“void main(void){

IntBinaryTree tree;printf("Inserting nodes.\n“);tree.insertNode(5);tree.insertNode(8);tree.insertNode(3);tree.insertNode(12);tree.insertNode(9);

printf("Inorder traversal:\n“);showNodesInOrder(tree);printf("\nPreorder traversal:\n“);showNodesPreOrder(tree);printf("\nPostorder traversal:\n“);showNodesPostOrder(tree);

}

5 Node B-Tree Output  Inserting nodes.Inorder traversal:358912 Preorder traversal:538129

Postorder traversal:3

91285

Build A B-Tree & Search 3 in it// This program builds a binary tree with 5 nodes.// The SearchNode function determines if the value 3 is in the tree.#include <stdio.h>#include "BinaryTree.h“void main(void){

IntBinaryTree tree;printf("Inserting nodes.\n“);tree.insertNode(5);tree.insertNode(8);tree.insertNode(3);tree.insertNode(12);tree.insertNode(9);

if (tree.searchNode(3))pritnf( "3 is found in the tree.\n“);

elseprinyf( "3 was not found in the tree.\n“);

}

if (tree.searchNode(3))printf( "3 is found in the tree.\n“);

elseprintf("3 was not found in the tree.\n“);

}

Deleting a Node

• We simply find its parent and set the child pointer that links to it to NULL, and then free the node's memory.

• But what if we want to delete a node that has child nodes? We must delete the node while at the same time preserving the subtrees that the node links to.

Deleting a Node

• There are two possible situations when we are deleting a non-leaf node:– A) the node has one child, or– B) the node has two children.

Deleting a NodeA tree in which we are about to delete a node with one subtree.

Deleting a NodeHow to will link the node's subtree with its parent after the delete.

Deleting a NodeThe problem is not as easily solved, however, when the node we are about to delete has two subtrees.

Deleting a Node

• We cannot attach both of the node's subtrees to its parent, so there must be an alternative solution.

• One way is to attach the node's right subtree to the parent, and then find a position in the right subtree to attach the left subtree. The result is shown in the next slide.

Deleting a Node

Deleting a Node

To delete a node from the BinaryTree, call function remove(). The argument is the value of the node that is to be deleted.

void remove(int num){

deleteNode(num, root);}

The remove function calls the deleteNode function. It passes the value of the node to delete, and the root pointer.

The deleteNode()

void deleteNode(int num, TreeNode *&nodePtr){

if (num < nodePtr->value)deleteNode(num, nodePtr->left);

else if (num > nodePtr->value)deleteNode(num, nodePtr->right);

elsemakeDeletion(nodePtr);

} elsemakeDeletion(nodePtr);

Notice the declaration of the nodePtr parameter:

TreeNode *&nodePtr;

nodePtr is not simply a pointer to a TreeNode structure, but a reference to a pointer to a TreeNode structure. Any action performed on nodePtr is actually performed on the argument passed into nodePtr.

The deleteNode() Function

The trailing else statement calls the makeDeletion function, passing nodePtr as its argument.

•The makeDeletion function actually deletes the node from the tree, and must reattach the deleted node’s subtrees.

•Therefore, it must have access to the actual pointer in the binary tree to the node that is being deleted.

•This is why the nodePtr parameter in the deleteNode function is a reference.

The makeDeletion()

void makeDeletion(TreeNode *&nodePtr){

TreeNode *tempNodePtr; // Temporary pointer, used in // reattaching the left subtree.

if (nodePtr == NULL)cout << "Cannot delete empty node.\n";

else if (nodePtr->right == NULL){

tempNodePtr = nodePtr;nodePtr = nodePtr->left; // Reattach the left childfree (tempNodePtr);

}else if (nodePtr->left == NULL){

tempNodePtr = nodePtr;nodePtr = nodePtr->right; // Reattach the right childfree (tempNodePtr);

}

The makeDeletion() (continued)

// If the node has two children.else{

// Move one node the right.tempNodePtr = nodePtr->right;// Go to the end left node.while (tempNodePtr->left)

tempNodePtr = tempNodePtr->left;// Reattach the left subtree.tempNodePtr->left = nodePtr->left;tempNodePtr = nodePtr;// Reattach the right subtree.nodePtr = nodePtr->right;free (tempNodePtr);

}}

Build a binary tree with 5 nodes// The DeleteNode function is used to remove two of them.#include <stdio.h>#include "BinaryTree.h“void main(void){

BinaryTree tree;printf("Inserting nodes.\n“);tree.insertNode(5);tree.insertNode(8);tree.insertNode(3);tree.insertNode(12);tree.insertNode(9);printf("Here are the values in the tree:\n“);tree.showNodesInOrder();

printf("Deleting 8...\n";tree.remove(8);printf("Deleting 12...\n";tree.remove(12);printf("Now, here are the nodes:\n";tree.showNodesInOrder();

}

Program Output

Inserting nodes.Here are the values in the tree:358912Deleting 8...Deleting 12...Now, here are the nodes:359

top related