Binary Tree C implementation

Source: Internet
Author: User

 

# Include <stdio. h>
# Include <stdlib. h>

# Define maxlen 100 // maximum number of nodes
Typedef char data; // element type
Typedef struct CBT
{
Data data;
Struct CBT * left; // left subtree node pointer
Struct CBT * right; // right subtree node pointer
} Cbttype;

cbttype * inittree () // initialize the binary tree root
{
cbttype * node;
If (node = (cbttype *) malloc (sizeof (cbttype)
{< br> printf ("enter a root node data first: \ n ");
scanf ("% s", & node-> data);
node-> left = NULL;
node-> right = NULL;
If (node! = NULL)
{< br> return node;
}else
{< br> return NULL;
}< BR >}< br> return NULL;
}

Cbttype * treefindnode (cbttype * treenode, data)
{
Cbttype * PTR;
If (treenode = NULL)
{
Return NULL;
}
Else
{
If (treenode-> DATA = data)
{
Return treenode;
}
Else
{
If (PTR = treefindnode (treenode-> left, data ))
{
Return PTR;
}
Else if (PTR = treefindnode (treenode-> right, Data ))
{
Return PTR;
} Else
{
Return NULL;
}
}
}
}

Void addtreenode (cbttype * treenode)
{
Cbttype * pnode, * parent;
Data data;
Char menusel;

If (pnode = (cbttype *) malloc (sizeof (cbttype )))
{
Printf ("input binary tree node data: \ n ");
Scanf ("% s", & pnode-> data );
Pnode-> left = NULL;
Pnode-> right = NULL;

Printf ("input parent node data of this node: \ n ");
Scanf ("% s", & data );

Parent = treefindnode (treenode, data );
If (! Parent)
{
Printf ("this parent node is not found! \ N ");
Free (pnode );
Return;
}
Printf ("1. Add the left subtree \ N2. add the right subtree \ n" of the node ");
Do
{
Menusel = getchar ();
Menusel-= '0 ';
If (menusel = 1 | menusel = 2)
{
If (parent = NULL)
{
Printf ("no parent node exists. Please set the parent node first! \ N ");
}
Else
{
Switch (menusel)
{
Case 1:
If (parent-> left)
{
Printf ("Left subtree node is not empty! \ N ");
}
Else
{
Parent-> left = pnode;
}
Break;
Case 2:
If (parent-> right)
{
Printf ("the right subtree node is not empty! \ N ");
}
Else
{
Parent-> right = pnode;
}
Break;
Default:
Printf ("invalid parameter! \ N ");
}
}
}

} While (menusel! = 1 & menusel! = 2 );
}
}

Cbttype * treeleftnode (cbttype * treenode)
{
If (treenode)
{
Return treenode-> left;
} Else
{
Return NULL;
}
}

Cbttype * treerightnode (cbttype * treenode)
{
If (treenode)
{
Return treenode-> right;
} Else
{
Return NULL;
}
}

Int treeisempty (cbttype * treenode)
{
If (treenode)
{
Return 0;
}
Else
{
Return 1;
}
}

int treedepth (cbttype * treenode)
{< br> int depleft, depright;
If (treenode = NULL)
{< br> return 0;
}< br> else
{< br> depleft = treedepth (treenode-> left );
depright = treedepth (treenode-> right);
If (depleft> depright)
{< br> return depleft + 1;
}< br> else
{< br> return depright + 1;
}< BR >}

Void cleartree (cbttype * treenode)
{
If (treenode)
{
Cleartree (treenode-> left );
Cleartree (treenode-> right );
Free (treenode );
Treenode = NULL;
}
}
Void treenodedata (cbttype * P)
{
Printf ("% C", p-> data );
}

Void leveltree (cbttype * treenode, void (* treenodedata) (cbttype * p ))
{
Cbttype * P;
Cbttype * Q [maxlen];
Int head = 0, tail = 0;

If (treenode)
{
Tail = (tail + 1) % maxlen;
Q [tail] = treenode;
}
While (Head! = Tail)
{
Head = (Head + 1) % maxlen;
P = Q [head];
Treenodedata (P );
If (p-> left)
{
Tail = (tail + 1) % maxlen;
Q [tail] = p-> left;
}
If (p-> right)
{
Tail = (tail + 1) % maxlen;
Q [tail] = p-> right;
}
}
}
Void dlrtree (cbttype * treenode, void (* treenodedata) (cbttype * p ))
{
If (treenode)
{
Treenodedata (treenode );
Dlrtree (treenode-> left, treenodedata );
Dlrtree (treenode-> right, treenodedata );
}
}
Void ldrtree (cbttype * treenode, void (* treenodedata) (cbttype * p ))
{
If (treenode)
{
Ldrtree (treenode-> left, treenodedata );
Treenodedata (treenode );
Ldrtree (treenode-> right, treenodedata );
}
}
Void rdltree (cbttype * treenode, void (* treenodedata) (cbttype * p ))
{
If (treenode)
{
Rdltree (treenode-> right, treenodedata );
Treenodedata (treenode );
Rdltree (treenode-> left, treenodedata );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.