-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble_tree.c
64 lines (59 loc) · 1.04 KB
/
double_tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node* lchild;
struct node* rchild;
};
struct node *newnode(int data)
{
struct node *node=(struct node *)malloc(sizeof(struct node ));
node->info=data;
node->lchild=NULL;
node->rchild=NULL;
return (node);
}
void double_tree(struct node *root)
{
struct node *temp;
if(root==NULL)
return ;
double_tree(root->lchild);
double_tree(root->rchild);
temp=root->lchild;
root->lchild=newnode(root->info);
root->lchild->lchild=temp;
printf("\n");
}
void inorder(struct node *root)
{
if(root==NULL)
return ;
inorder(root->lchild);
printf("%d\n",root->info);
inorder(root->rchild);
}
void preorder(struct node *root)
{
if(root==NULL)
return;
printf("%d\t",root->info);
preorder(root->lchild);
preorder(root->rchild);
}
int main()
{
struct node *node=newnode(4);
node->lchild=newnode(2);
node->rchild=newnode(5);
node->lchild->lchild=newnode(1);
node->lchild->rchild=newnode(3);
inorder(node);
double_tree(node);
inorder(node);
double_tree(node);
preorder(node);
getch();
return 0;
}