Binary Tree operation

Source: Internet
Author: User

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <assert. h>
 
Struct tnode
{
Int data;
Struct tnode * lchild;
Struct tnode * rchild;
} Tnode;
Typedef struct tnode * TNode;
// This program is useless.
TNode newNode (int data)
{
TNode node = (TNode) malloc (sizeof (struct tnode ));
Node-> data = data;
Node-> lchild = NULL;
Node-> rchild = NULL;
Return node;
}
 
 
TNode createTree (TNode root)
{
Int data = 0;
Scanf ("% d", & data );
If (data =-1) return NULL;
Root = (TNode) malloc (sizeof (struct tnode ));
Root-> data = data;
Root-> lchild = NULL;
Root-> rchild = NULL;
Root-> lchild = createTree (root-> lchild); // if no value is assigned to root-> lchild, the left subtree is not created, and root-> lchild is still NULL.
Root-> rchild = createTree (root-> rchild );
Return root;
}
 
Void preorderTraverse (TNode root)
{
If (root! = NULL)
{
Printf ("% d", root-> data );
PreorderTraverse (root-> lchild );
PreorderTraverse (root-> rchild );
}
}
 
Int treeDepth (TNode root)
{
If (root = NULL)
Return 0;
Int nleft = treeDepth (root-> lchild );
Int nright = treeDepth (root-> rchild );
Return (nleft> nright )? (Nleft + 1): (nright + 1 );
}
 
Void treeFree (TNode root)
{
If (root! = NULL)
{
TreeFree (root-> lchild );
TreeFree (root-> rchild );
Printf ("% d", root-> data );
Free (root );
}
}
 
Void test ()
{
TNode root = NULL;
Root = createTree (root );
PreorderTraverse (root );
Printf ("\ n ");
Int depth = treeDepth (root );
Printf ("depth = % d \ n", depth );
TreeFree (root );
}
 
Int main ()
{
Test ();
Return 0;
}
/*****************************\
1
2 6
-1 5 4-1
-1-1-1-1
\*****************************/

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

Struct tnode
{
Int data;
Struct tnode * lchild;
Struct tnode * rchild;
} Tnode;
Typedef struct tnode * TNode;
// This program is useless.
TNode newNode (int data)
{
TNode node = (TNode) malloc (sizeof (struct tnode ));
Node-> data = data;
Node-> lchild = NULL;
Node-> rchild = NULL;
Return node;
}


TNode createTree (TNode root)
{
Int data = 0;
Scanf ("% d", & data );
If (data =-1) return NULL;
Root = (TNode) malloc (sizeof (struct tnode ));
Root-> data = data;
Root-> lchild = NULL;
Root-> rchild = NULL;
Root-> lchild = createTree (root-> lchild); // if no value is assigned to root-> lchild, the left subtree is not created, and root-> lchild is still NULL.
Root-> rchild = createTree (root-> rchild );
Return root;
}

Void preorderTraverse (TNode root)
{
If (root! = NULL)
{
Printf ("% d", root-> data );
PreorderTraverse (root-> lchild );
PreorderTraverse (root-> rchild );
}
}

Int treeDepth (TNode root)
{
If (root = NULL)
Return 0;
Int nleft = treeDepth (root-> lchild );
Int nright = treeDepth (root-> rchild );
Return (nleft> nright )? (Nleft + 1): (nright + 1 );
}

Void treeFree (TNode root)
{
If (root! = NULL)
{
TreeFree (root-> lchild );
TreeFree (root-> rchild );
Printf ("% d", root-> data );
Free (root );
}
}

Void test ()
{
TNode root = NULL;
Root = createTree (root );
PreorderTraverse (root );
Printf ("\ n ");
Int depth = treeDepth (root );
Printf ("depth = % d \ n", depth );
TreeFree (root );
}

Int main ()
{
Test ();
Return 0;
}
/*****************************\
1
2 6
-1 5 4-1
-1-1-1-1
\*****************************/

 

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.