struct Node{
char val;
Node *l = NULL, *r = NULL, *p = NULL;
};
Node* build(Node *p /*...根据实际情况而定*/)
{
Node *root = new Node;
root->val = data;
root->p = p;
root->l = build(root /*...根据实际情况而定*/);
root->r = build(root /*...根据实际情况而定*/);
return root;
}
- 递归实现
void pre_order(Node* root)
{
if(root != NULL){
cout<<root->val;
pre_order(root->l);
pre_order(root->r);
}
}
void in_order(Node* root)
{
if(root != NULL){
in_order(root->l);
cout<<root->val;
in_order(root->r);
}
}
void post_order(Node* root)
{
if(root != NULL){
post_order(root->l);
post_order(root->r);
cout<<root->val;
}
}
- 非递归实现
void pre_order2(Node* root)
{
Node *pre = NULL;
Node *node = root;
while(node != NULL){
if(pre == node->p){
cout<<node->val;
if(node->l != NULL){
pre = node;
node = node->l;
}
else{
if(node->r != NULL){
pre = node;
node = node->r;
}
else{
pre = node;
node = node->p;
}
}
}
else if(pre == node->l){
if(node->r != NULL){
pre = node;
node = node->r;
}
else{
pre = node;
node = node->p;
}
}
else if(pre == node->r){
pre = node;
node = node->p;
}
}
}
void in_order2(Node* root)
{
Node *pre = NULL;
Node *node = root;
while(node != NULL){
if(pre == node->p){
if(node->l != NULL){
pre = node;
node = node->l;
}
else{
cout<<node->val;
if(node->r != NULL){
pre = node;
node = node->r;
}
else{
pre = node;
node = node->p;
}
}
}
else if(pre == node->l){
cout<<node->val;
if(node->r != NULL){
pre = node;
node = node->r;
}
else{
pre = node;
node = node->p;
}
}
else if(pre == node->r){
pre = node;
node = node->p;
}
}
}
void post_order2(Node* root)
{
Node *pre = NULL;
Node *node = root;
while(node != NULL){
if(pre == node->p){
if(node->l != NULL){
pre = node;
node = node->l;
}
else{
if(node->r != NULL){
pre = node;
node = node->r;
}
else{
cout<<node->val;
pre = node;
node = node->p;
}
}
}
else if(pre == node->l){
if(node->r != NULL){
pre = node;
node = node->r;
}
else{
cout<<node->val;
pre = node;
node = node->p;
}
}
else if(pre == node->r){
cout<<node->val;
pre = node;
node = node->p;
}
}
}
- 递归实现
Node* tree_search(Node *x, char k)
{
if(x == NULL || k == x.val)
return x;
if(k < x.val)
return tree_search(x->l, k);
else
return tree_search(x->r, k);
}
- 非递归实现
Node* tree_search2(Node *x, char k)
{
while(x != NULL && k != x->val){
if(k < x->val)
x = x->l;
else
x = x->r;
}
return x;
}
- 最小关键字元素
Node* tree_minimum(Node *x)
{
while(x->l != NULL)
x = x->l;
return x;
}
- 最大关键字元素
Node* tree_maximum(Node *x)
{
while(x->r != NULL)
x = x->r;
return x;
}
- 后继
Node* tree_successor(Node *x)
{
if(x->r != NULL)
return tree_minimum(x->r);
Node *y = x->p;
while(y != NULL && x == y->r){
x = y;
y = y->p;
}
return y;
}
- 前驱
Node* tree_predecessor(Node *x)
{
if(x->l != NULL)
return tree_maximum(x->l);
Node *y = x->p;
while(y != NULL && x == y->l){
x = y;
y = y->p;
}
return y;
}
- 插入
Node* tree_insert(Node *root, Node *z) //插入结点z
{
Node *x = root;
Node *y = NULL;
while(x != NULL){
y = x;
if(z->val < x->val)
x = x->l;
else
x = x->r;
}
z->p = y;
if(y == NULL){
//树为空,此时z为根节点
}
else if(z->val < y->val)
y->l = z;
else
y->r = z;
}
- 删除
待续。。。