Binary data structure bintree.h

xiaoxiao2021-03-06  98

///

///

// binary tree data structure bintree.h //

///

//

#include

Template Class Bintree;

Template

Class Treenode

{

protected:

Friend Class Bintree ;

Treenode (): lchild (null), rChild (null) {}

TYPE DATA;

Treenode * lchild; // left, right

Treenode * rchild;

}

Template

Class bintree

{

Friend void bintree_pre (bintree & bintreeop); // Friends function

Friend void bintree_ino (bintree & bintreeop);

Friend void bintree_pos (bintree & bintreeop);

Friend void bintree_destroy (bintree & bintreeop);

PUBLIC:

Bintree (): root (null) {}

Void creatree (); // Creating a binary tree, the main process

Void Creattree (Treenode * Child, INT K); // Sub-process

Void Pretree (Treenode * point); // Senior sequence

Void inotree (TREENODE * Point); // Traverse Tree

Void PostRee (Treenode * point); // Traversing binary tree

Void Destroy (TREENODE * Point); // Destroy the binary tree

Bool isempty ();

protected:

TREENODE * root;

}

Template

Void bintree :: creattree ()

{

CreatTree (root, 1);

}

Template

Void Bintree :: Creattree (Treenode * Child, Int K)

{

TREENODE * POINT;

Point = New Treenode ;

Cout << "Enter the node data item:";

CIN >> POINT-> DATA;

Switch (k)

{

Case 1: root = POINT; BREAK;

Case 2: child-> lchild = points;

Case 3: Child-> rchild = Point; Break;

}

CHAR TEMP;

Cout << "The" << Point-> Data << "node has a left subtree Y / any key:";

CIN >> TEMP;

IF (Temp == 'Y' || TEMP == 'Y')

{

CreatTree (Point, 2);

}

Cout << "The << Point-> Data <<" node has a right sub-tree Y / any key: "; cin >> Temp;

IF (Temp == 'Y' || TEMP == 'Y')

{

CreatTree (Point, 3);

}

}

Template

Void bintree :: pretree (Treenode * point)

{

IF (Point! = null)

{

Cout << "<< Point-> Data;

PRETREE (Point-> LCHILD);

PRETREE (Point-> rchild);

}

}

Template

Void Bintree :: inotree (Treenode * point)

{

IF (Point! = null)

{

Inotree (Point-> Lchild);

Cout << "<< Point-> Data;

INOTREE (Point-> rchild);

}

}

Template

Void bintree :: postree (Treenode * point)

{

IF (Point! = null)

{

Postree (Point-> LCHILD);

Postree (Point-> rchild);

Cout << "<< Point-> Data;

}

}

Template

Bool bintree :: isempty ()

{

Return root == NULL;

}

Template

Void Bintree :: destroy (Treenode * point)

{

IF (Point! = null)

{

Destroy (Point-> LCHILD);

DESTROY (POINT-> rchild);

Delete point;

}

}

转载请注明原文地址:https://www.9cbs.com/read-102108.html

New Post(0)