二叉树的相关程序

                            二叉树的相关程序

前序建立
void precreate(Binary &root, string &str, int &i)
{
if (str.length() == 0)
{
return;
}
else if (str[i] == '#')
{
i++;
root = NULL;
}
else
{
root = new BtNode();
root->val = str[i];
i++;
precreate(root->left, str, i);
precreate(root->right, str, i);
}
}

遍历(前中后)
前序非递归
void prebianli1(Binary &root)
{
if (root == NULL)
return;
stack<Binary> s;
Binary temp = root;
s.push(root);
while (!s.empty())
{
cout << s.top()->val;
temp = s.top();
s.pop();
if (temp->right != NULL)
s.push(temp->right);
if (temp->left != NULL)
s.push(temp->left);
}
}
中序非递归
void midbianli1(Binary &root)
{
if (root == NULL)
return; 
stack<Binary> s;
Binary temp = root;
while (!s.empty()|| temp!=NULL)
{
while (temp != NULL)
{
s.push(temp);
temp = temp->left;
}
if (!s.empty())
{
cout << s.top()->val;
temp = s.top();
s.pop();
temp = temp->right;
}
}
}
后序非递归
void lastbianli1(Binary &root)
{
if (root == NULL)
return;
Binary temp = root;
int tag = 0;
stack<Binary> s;
stack<int> s1;
do
{
while (temp!= NULL)
{
s.push(temp);
s1.push(0);
temp = temp->left;
}
if (!s.empty())
{
tag = s1.top();
s1.pop();
temp = s.top();
s.pop();
if (tag == 0)
{
s.push(temp);
s1.push(1);
temp =temp->right;
}
else
{
cout << temp->val;
temp = NULL;
}
}
} while (!(s.empty()));
}
层次遍历
void levelbianli1(Binary root)
{
queue<Binary> q;
Binary temp = root;
q.push(root);
while (!q.empty())
{
temp = q.front();
cout << temp->val;
if (temp->left != NULL)
q.push(temp->left);
if (temp->right != NULL)
q.push(temp->right);
q.pop();
}
}


求二叉树的高度
int deep(Binary root)
{
if (root == NULL)
return 0;
else
return(max(deep(root->left), deep(root->right)) + 1);
}


求树的宽度
int width(Binary &root)
{
int count=0, sum=0, last=0, rear=0, front=0;
Binary temp = root;
vector<Binary> q;
q.push_back(temp);
while (front <= rear)
{
temp = q[front++];
count++;
if (temp->left!=NULL)
{
q.push_back(temp->left);
++rear;
}
if (temp->right != NULL)
{
q.push_back(temp->right);
++rear;
}
if (front > last)
{
last = rear;
if (count > sum)
{
sum = count;
}
count = 0;
}
}
return sum;
}


、、、、、、、、、先序中序&中序后序建立树
void pre_mid_create(Binary &root,char *str1, char *str2, int len)//str1前 str2中
{
if (len == 0)
return;
char ch = str1[0];
int index = 0;
while (ch != str2[index])
{
index++;
}
root = new BtNode();
root->val = ch;
pre_mid_create(root->left, str1+1, str2, index);
pre_mid_create(root->right, str1 + index + 1, str2 + index + 1, len - index - 1);
}




void mid_back_create(Binary &root, char *str1, char *str2, int len)//str1中 str2后
{
if (len == 0)
{
root = NULL;
return;
}
char ch = str2[len - 1];
int index = 0;
while (ch != str1[index])
{
index++;
}
root = new BtNode();
root->val = ch;
mid_back_create(root->left, str1, str2, index);
mid_back_create(root->right, str1 + index + 1, str2 + index, len - index-1);
}


、、、、、、、、、、其他问题
判断平衡树
bool balance_tree(Binary &root)
{
if (root == NULL)//2直到最后一个空节点位置
return true;
int left = high(root->left);
int right = high(root->right);
if (abs(left - right) > 1)//3有一个节点不满足都不是平衡二叉树
return false;
return balance_tree(root->left) && balance_tree(root->right);//1每一个子树都要平衡
}
构造镜像树
void reserver_tree(Binary &root)//每个节点在自己父节点下左右交换
{
if (root == NULL)
return;
stack<Binary> s;
Binary temp = root;
s.push(root);
while (!s.empty())
{
temp = s.top();
s.pop();
Binary t = temp->left;
temp->left = temp->right;
temp->right = t;//1.这个节点的左右子树交换
if (temp->left)
s.push(temp->left);//2.这个节点左子树等待交换
if (temp->right)
s.push(temp->right);//3.这个节点右子树等待交换
}
}
void reserver_tree1(Binary &root)(递归写法)
{
if (root == NULL)
return;
reserver_tree(root->left);
reserver_tree(root->right);//从下网上依次交换
Binary temp = root->left;
root->left = root->right;
root->right = temp;
}

猜你喜欢

转载自blog.csdn.net/qq_41784469/article/details/80890648
今日推荐