二叉树基础

第一题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef struct Node{
	int data;
	Node *left;
	Node *right;
}Node;
void createtree(Node **t,int n,int all[],int index)
{
	if(index>=n)
	{
		(*t)=NULL;
		return;
	}
	if(index>(n-1)/2)
	{
		//printf("第%d个结点的值为%d\n",index,all[index]);
		*t=(Node *)malloc(sizeof(Node));
		(*t)->data=all[index];
		(*t)->left=NULL;
		(*t)->right=NULL;
		return;
	}
	*t=(Node *)malloc(sizeof(Node));
	(*t)->data=all[index];
	//printf("第%d个结点的值为%d\n",index,all[index]);
	createtree(&((*t)->left),n,all,index*2+1);
	createtree(&((*t)->right),n,all,index*2+2);
}
void preshow(Node *t)
{
	if(t==NULL)
	return;
	printf("%d ",t->data);
	preshow(t->left);
	preshow(t->right);
}
void inshow(Node *t)
{
	if(t==NULL)
	return;
	inshow(t->left);
	printf("%d ",t->data);
	inshow(t->right);
}
void pastshow(Node *t)
{
	if(t==NULL)
	return;
	pastshow(t->left);
	pastshow(t->right);
	printf("%d ",t->data);
}
int main()
{
	Node *t;
	int i,n;
	scanf("%d",&n);
	int all[n];
	for(i=0;i<n;i++)
	{
		scanf("%d",&all[i]);
	}
	int res=log(n)/log(2);
	printf("%d\n",res);
	createtree(&t,n,all,0);
	preshow(t);
	printf("\n");
	inshow(t);
	printf("\n");
	pastshow(t);
	return 0;
}

```c
在这里插入代码片
```第二题#include <iostream>
#include <string>
#include <string.h>

using namespace std;

struct entry
{
    string item;
    string num;
};
struct binary_node
{
    entry data;
    binary_node *left;
    binary_node *right;
    binary_node();
    binary_node(entry x);
};
binary_node::binary_node()
{
    left=NULL;right=NULL;
}
binary_node::binary_node(entry x)
{
    data=x;left=NULL;right=NULL;
}

void visit(entry x)
{
    cout << x.item << " ";
}
class binary_tree
{
public:
    binary_tree();
    bool empty_();
    int size_() const;
    int height() const;
    void insert_(entry x);
    bool search_insert(entry &x);
    bool tree_search(entry target);
    bool remove_(entry &target);
    void preorder(void (*visit)(entry ));
	void inorder(void (*visit)(entry ));
	void postorder(void (*visit)(entry ));
protected:
    void recursive_preorder(binary_node *sub_root, void (*visit)(entry ));
	void recursive_inorder(binary_node *sub_root, void (*visit)(entry ));
	void recursive_postorder(binary_node *sub_root, void (*visit)(entry ));
private:
	bool recursive_search_insert(binary_node *&sub_root,entry &x);
	binary_node* recursive_tree_search (binary_node *&sub_root,entry target);
    bool remove_root(binary_node *&sub_root);
    bool search_and_destroy(binary_node *&sub_root,entry &target);
    int count_;
    binary_node *root;
};
binary_tree::binary_tree()
{
    count_=0;root=NULL;
}
bool binary_tree::remove_root(binary_node *&sub_root)
{
	binary_node *&ssub_root=sub_root;
    if (sub_root==NULL)
        return false;
    binary_node *to_del=ssub_root;
    if (to_del->left==NULL)
		ssub_root=sub_root->right;
    else if (to_del->right==NULL) sub_root=sub_root->left;
    else
    {
        to_del=sub_root->left;
        binary_node *parent=sub_root;
        while (to_del->right!=NULL)
        {
            parent=to_del;
            to_del=to_del->right;
        }
        sub_root->data=to_del->data;
        if (parent==sub_root)
            sub_root->left=to_del->left;
        else
            parent->right=to_del->left;
    }
    delete to_del;
    return true;
}
bool binary_tree::search_and_destroy(binary_node *&sub_root,entry &target)
{
    if (sub_root==NULL||target.item==sub_root->data.item)
        return remove_root(sub_root);
    else if (target.item<sub_root->data.item)
        return search_and_destroy(sub_root->left,target);

        return search_and_destroy(sub_root->right,target);
}
bool binary_tree::remove_(entry &target)
{
    if (search_and_destroy(root,target))
    {
        count_--;
        return true;
    }
    return false;
}
binary_node* binary_tree::recursive_tree_search(binary_node *&sub_root,entry target)
{
    if (sub_root==NULL)
    {
        cout << "NULL";
        return sub_root;
    }
    cout << sub_root->data.item << " ";
    if (target.item==sub_root->data.item)
    {
        cout << sub_root->data.num;
        return sub_root;
    }
    else if (sub_root->data.item<target.item)
        return recursive_tree_search(sub_root->right,target);

        return recursive_tree_search(sub_root->left,target);
}
bool binary_tree::tree_search(entry target)
{
    binary_node *found=recursive_tree_search(root,target);
    if (found==NULL)
        return false;
    target=found->data;
    return true;
}
bool binary_tree::search_insert(entry &x)
{
    if (recursive_search_insert(root,x))
    {
         count_++;
        return true;
    }
    return false;
}
bool binary_tree::recursive_search_insert(binary_node *&sub_root,entry &x)
{
    if (sub_root==NULL)
    {
        sub_root=new binary_node (x);
        return true;
    }
    else if (x.item<sub_root->data.item)
        return recursive_search_insert(sub_root->left,x);
    else if (x.item>sub_root->data.item)
        return recursive_search_insert(sub_root->right,x);

        return false;
}

bool binary_tree::empty_()
{
    return root==NULL;
}
int binary_tree::size_() const
{
    return count_;
}
int binary_tree::height() const
{
    int count_=size_();
	if(count_==0)return -1;
	int tmp=1,k=0;
	for(k;tmp<=count_;k++) tmp*=2;
	return k-1;
}
void binary_tree::insert_(entry x)
{
    if (empty_())
    {
        root=new binary_node(x);
        count_++;
        return;
    }
    int numbers[2000];
    int tmpcount=size_(),cur=0;
    while (tmpcount>0)
    {
        if (tmpcount%2==0)
            numbers[cur++]=2;
        else
            numbers[cur++]=1;
        tmpcount=(tmpcount-1)/2;
    }
    binary_node *p=root;
    for (int i=cur-1;i>0;i--)
    {
        if (numbers[i]==1) p=p->left;
        if (numbers[i]==2) p=p->right;
    }
    if (numbers[0]==1) p->left=new binary_node(x);
    if (numbers[0]==2) p->right=new binary_node(x);
    count_++;
}
void binary_tree::preorder(void (*visit)(entry ))
{
    recursive_preorder(root,visit);
}
void binary_tree::recursive_preorder(binary_node *sub_root, void (*visit)(entry ))
{
    if (sub_root!=NULL)
    {
        (*visit)(sub_root->data);
        recursive_preorder(sub_root->left,visit);
        recursive_preorder(sub_root->right,visit);
    }
}
void binary_tree::inorder(void (*visit)(entry ))
{
    recursive_inorder(root,visit);
}
void binary_tree::recursive_inorder(binary_node *sub_root, void (*visit)(entry ))
{
    if (sub_root!=NULL)
    {
        recursive_inorder(sub_root->left,visit);
        (*visit)(sub_root->data);
        recursive_inorder(sub_root->right,visit);
    }
}
void binary_tree::postorder(void (*visit)(entry ))
{
    recursive_postorder(root,visit);
}
void binary_tree::recursive_postorder(binary_node *sub_root, void (*visit)(entry ))
{
    if (sub_root!=NULL)
    {
        recursive_postorder(sub_root->left,visit);
        recursive_postorder(sub_root->right,visit);
        (*visit)(sub_root->data);
    }
}

int main()
{
    int n; cin >> n;
    binary_tree thetree;
    entry input;
    for (int i=0;i<n;i++)
    {
        cin >> input.item >> input.num;
        thetree.search_insert(input);
    }
    int num; cin >> num;
    for (int i=0;i<num;i++)
    {
         cin >> input.item;
        thetree.remove_(input);
    }
    entry to_search1,to_search2;
    cin >> to_search1.item;
    thetree.tree_search(to_search1); cout << endl;
    cin >> to_search2.item;
    thetree.tree_search(to_search2);
    return 0;
}

```c
第三题
```#include <bits/stdc++.h>
using namespace std;

typedef int Entry;
struct Binary_node {
	Entry data;
	Binary_node *left;
	Binary_node *right;
	Binary_node( );
	Binary_node(const Entry &x);
};

Binary_node::Binary_node(){
	left = NULL;
	right = NULL;
}

Binary_node::Binary_node(const Entry &x){
	data = x;
	left = NULL;
	right = NULL;
}

class Binary_tree {
public:
	Binary_tree( );
	bool empty( ) const;
	void preorder(void (*visit)(Entry &));
	void inorder(void (*visit)(Entry &));
	void postorder(void (*visit)(Entry &));
	int size( ) const;
	int height( ) const;
	void insert(Entry &);
	Binary_node *get_root();
protected:
	void recursive_preorder(Binary_node *sub_root, void (*visit)(Entry &));
	void recursive_inorder(Binary_node *sub_root, void (*visit)(Entry &));
	void recursive_postorder(Binary_node *sub_root, void (*visit)(Entry &));
	Binary_node *root;
	int count;
};

Binary_tree::Binary_tree( )
{
	root = NULL;
	count = 0;
}

bool Binary_tree:: empty( ) const
{
	return root == NULL;
}

void Binary_tree :: preorder(void (*visit)(Entry &))
{
	recursive_preorder(root, visit);
}

void Binary_tree :: recursive_preorder(Binary_node *sub_root, void (*visit)(Entry &))
{
	if (sub_root != NULL) {
		(*visit)(sub_root->data);
		recursive_preorder(sub_root->left, visit);
		recursive_preorder(sub_root->right, visit);
	}
}

void Binary_tree :: inorder(void (*visit)(Entry &))
{
	recursive_inorder(root, visit);
}
void Binary_tree :: recursive_inorder(Binary_node *sub_root, void (*visit)(Entry &))
{
	if (sub_root != NULL) {
		recursive_inorder(sub_root->left, visit);
		(*visit)(sub_root->data);
		recursive_inorder(sub_root->right, visit);
	}
}

void Binary_tree::postorder(void (*visit)(Entry &))
{
	recursive_postorder(root, visit);
}

void Binary_tree :: recursive_postorder(Binary_node *sub_root, void (*visit)(Entry &))
{
	if (sub_root != NULL) {
		recursive_postorder(sub_root->left, visit);
		recursive_postorder(sub_root->right, visit);
		(*visit)(sub_root->data);
	}
}

int Binary_tree :: size( ) const
{
	return count;
}
int Binary_tree :: height( ) const
{
	int count=size();
	if(count==0)return -1;
	int tmp=1;
	int k;
	for( k=0; tmp<=count; k++) tmp*=2;
	return k-1;
}

void Binary_tree :: insert(Entry &x)
{
	if(empty()){
		root=new Binary_node(x);
		count++;
		return;
	}
    stack<int> numbers;
    int tmpcount=size();
    while(tmpcount>0){
	    if(tmpcount%2==0){
		   numbers.push(2);
		}
	    else{
		   numbers.push(1);
		}
	    tmpcount=(tmpcount-1)/2;
	}
	Binary_node *current=root;
	while (numbers.size()>1) {

        	if(numbers.top()==1)current=current->left;
		if(numbers.top()==2)current=current->right;
       	numbers.pop();
	}
	if(numbers.top()==1)current->left=new Binary_node(x);
	if(numbers.top()==2)current->right=new Binary_node(x);
	count++;
}

Binary_node* Binary_tree::get_root()
{
    return root;
}


void print(Entry &x)
{
    cout<<x<<" ";
}

bool search_road(Binary_node* root,int sum)
{
    if(root==NULL)return false;
    if(root->left==NULL&&root->right==NULL)
        return sum == root->data;
    if(root->right==NULL)
    {
        sum -=root->data;
        return search_road(root->left,sum);
    }
    if(root->left==NULL)
    {
        sum -=root->data;
        return search_road(root->right,sum);
    }
    sum-=root->data;
    return search_road(root->right,sum)||search_road(root->left,sum);
}

int main()
{
    int T,t,k;
    Binary_tree Tree;
    cin>>T;
    while(T>0)
    {
        cin>>t;
        Tree.insert(t);
        T--;
    }
    cin>>k;
    cout<<Tree.height()<<" ";
    Binary_node* root=Tree.get_root();
    if(search_road(root,k))
        cout<<"true";
    else cout<<"false";
    return 0;
}
第四题`
#include <bits/stdc++.h>
using namespace std;

typedef string Entry;
struct Binary_node {
	Entry data;
	Binary_node *left;
	Binary_node *right;
	Binary_node( );
	Binary_node(const Entry &x);
};

Binary_node::Binary_node(){
	left = NULL;
	right = NULL;
}

Binary_node::Binary_node(const Entry &x){
	data = x;
	left = NULL;
	right = NULL;
}

class Binary_tree {
public:
	Binary_tree( );
	bool empty( ) const;
	void preorder(void (*visit)(Entry &));
	void inorder(void (*visit)(Entry &));
	void postorder(void (*visit)(Entry &));
	int size( ) const;
	int height( ) const;
	void insert(Entry &);
	Binary_node *get_root();
protected:
	void recursive_preorder(Binary_node *sub_root, void (*visit)(Entry &));
	void recursive_inorder(Binary_node *sub_root, void (*visit)(Entry &));
	void recursive_postorder(Binary_node *sub_root, void (*visit)(Entry &));
	Binary_node *root;
	int count;
};

Binary_tree::Binary_tree( )
{
	root = NULL;
	count = 0;
}

bool Binary_tree:: empty( ) const
{
	return root == NULL;
}

void Binary_tree :: preorder(void (*visit)(Entry &))
{
	recursive_preorder(root, visit);
}

void Binary_tree :: recursive_preorder(Binary_node *sub_root, void (*visit)(Entry &))
{
	if (sub_root != NULL) {
		(*visit)(sub_root->data);
		recursive_preorder(sub_root->left, visit);
		recursive_preorder(sub_root->right, visit);
	}
}

void Binary_tree :: inorder(void (*visit)(Entry &))
{
	recursive_inorder(root, visit);
}
void Binary_tree :: recursive_inorder(Binary_node *sub_root, void (*visit)(Entry &))
{
	if (sub_root != NULL) {
		recursive_inorder(sub_root->left, visit);
		(*visit)(sub_root->data);
		recursive_inorder(sub_root->right, visit);
	}
}

void Binary_tree::postorder(void (*visit)(Entry &))
{
	recursive_postorder(root, visit);
}

void Binary_tree :: recursive_postorder(Binary_node *sub_root, void (*visit)(Entry &))
{
	if (sub_root != NULL) {
		recursive_postorder(sub_root->left, visit);
		recursive_postorder(sub_root->right, visit);
		(*visit)(sub_root->data);
	}
}

int Binary_tree :: size( ) const
{
	return count;
}
int Binary_tree :: height( ) const
{
	int count=size();
	if(count==0)return -1;
	int tmp=1;
	int k;
	for( k=0; tmp<=count; k++) tmp*=2;
	return k-1;
}

void Binary_tree :: insert(Entry &x)
{
	if(empty()){
		root=new Binary_node(x);
		count++;
		return;
	}
    stack<int> numbers;
    int tmpcount=size();
    while(tmpcount>0){
	    if(tmpcount%2==0){
		   numbers.push(2);
		}
	    else{
		   numbers.push(1);
		}
	    tmpcount=(tmpcount-1)/2;
	}
	Binary_node *current=root;
	while (numbers.size()>1)
    {
        if(numbers.top()==1)current=current->left;
		if(numbers.top()==2)current=current->right;
       	numbers.pop();
	}
	if(numbers.top()==1)current->left=new Binary_node(x);
	if(numbers.top()==2)current->right=new Binary_node(x);
	count++;
}

Binary_node* Binary_tree::get_root()
{
    return root;
}


void print(Entry &x)
{
    cout<<x<<" ";
}

bool isequal(Binary_node* left1,Binary_node* right1)
{
    if(left1==NULL||right1==NULL)return left1==right1;
    if(left1->data==right1->data)
        return isequal(left1->left,right1->right)&&isequal(left1->right,right1->left);
    return false;
}

bool judge(Binary_node* root)
{
    return isequal(root->left,root->right);
}

int main()
{
    int T;
    Binary_tree Tree;
    cin>>T;
    string s;
    while(T>0)
    {
        cin>>s;
        Tree.insert(s);
        T--;
    }
    cout<<Tree.height()<<" ";
    Binary_node* root=Tree.get_root();
    if(judge(root))
        cout<<"true";
    else cout<<"false";
    return 0;
}`
第五题

```c
#include <iostream>
#include <stdbool.h>
#include <string.h>

using namespace std;

typedef int Stack_Entry, Node_entry;
enum Error_code { success, notPresent, duplicate_error };

struct Node
{
	Node_entry entry;
	Node* next;
	Node();
	Node(Node_entry item, Node* add_on = NULL);
};

Node::Node()
{
	next = NULL;
}

Node::Node(Node_entry item, Node* add_on)
{
	entry = item;
	next = add_on;
}

class Stack
{
public:
	Stack();
	bool emptyS()const;
	void push(const Stack_Entry& item);
	void pop();
	void top(Stack_Entry& item);
	void clearS();
	int size();
protected:
	Node* top_node;
	int countS;
};

Stack::Stack()
{
	top_node = NULL;
	countS = 0;
}

void Stack::push(const Stack_Entry& item)
{
	Node* new_top = new Node(item, top_node);
	countS++;
	if (new_top != NULL)
		top_node = new_top;
}

void Stack::pop()
{
	Node* old_top = top_node;
	if (top_node != NULL)
	{
		top_node = old_top->next;
		countS--;
		delete old_top;
	}
}

void Stack::top(Stack_Entry& item)
{
	if (top_node != NULL)
	{
		item = top_node->entry;
	}
}

bool Stack::emptyS()const
{
	bool outcome = true;
	if (top_node != NULL)
		outcome = false;
	return outcome;
}

int Stack::size()
{
	return countS;
}

void Stack::clearS()
{
	while (top_node != NULL)
	{
		Node* p = top_node;
		top_node = top_node->next;
		delete p;
	}
}

class Record
{
public:
	Record();
	Record(int x, int y);
	int the_key()const;
	int the_other()const;
private:
	int key;
	int other;
};
typedef Record List_entry;

Record::Record()
{

}

Record::Record(int x, int y)
{
	key = x;
	other = y;
}

int Record::the_key()const
{
	return key;
}

int Record::the_other()const
{
	return other;
}

class Key
{
	int key;
public:
	Key(int x);
	Key(const Record& r);
	int the_key()const;
};

Key::Key(int x)
{
	key = x;
}

Key::Key(const Record& r)
{
	key = r.the_key();
}

int Key::the_key()const
{
	return key;
}

bool operator==(const Key& x, const Key& y)
{
	return x.the_key() == y.the_key();
}

bool operator > (const Record& x, const Record& y)
{
	return x.the_key() > y.the_key();
}

bool operator < (const Record& x, const Record& y)
{
	return x.the_key() < y.the_key();
}

class Binary_Node
{
public:
	Record data;
	Binary_Node* left;
	Binary_Node* right;
	Binary_Node();
	Binary_Node(const Record& x);
};

Binary_Node::Binary_Node()
{
	left = NULL;
	right = NULL;
}

Binary_Node::Binary_Node(const Record& x)
{
	data = x;
	left = NULL;
	right = NULL;
}

class Binary_Tree
{
public:
	Binary_Tree();
	bool empty()const;
	void preorder(void(*visit)(Record&));
	void inorder(void(*visit)(Record&));
	void postorder(void(*visit)(Record&));
	int size()const;
	int height()const;
	void insert(Record&);
	Binary_Node* return_root();
protected:
	void recursive_pre(Binary_Node* subroot, void(*visit)(Record&));
	void recursive_in(Binary_Node* subroot, void(*visit)(Record&));
	void recursive_post(Binary_Node* subroot, void(*visit)(Record&));
	Binary_Node* root;
	int count;
};

Binary_Tree::Binary_Tree()
{
	root = NULL;
	count = 0;
}

bool Binary_Tree::empty()const
{
	return root == NULL;
}

void Binary_Tree::preorder(void(*visit)(Record&))
{
	recursive_pre(root, visit);
}

void Binary_Tree::recursive_pre(Binary_Node* subroot, void(*visit)(Record&))
{
	if (subroot != NULL)
	{
		(*visit)(subroot->data);
		recursive_pre(subroot->left, visit);
		recursive_pre(subroot->right, visit);
	}
}

void Binary_Tree::inorder(void(*visit)(Record&))
{
	recursive_in(root, visit);
}

void Binary_Tree::recursive_in(Binary_Node* subroot, void(*visit)(Record&))
{
	if (subroot != NULL)
	{
		recursive_in(subroot->left, visit);
		(*visit)(subroot->data);
		recursive_in(subroot->right, visit);
	}
}

void Binary_Tree::postorder(void(*visit)(Record&))
{
	recursive_post(root, visit);
}

void Binary_Tree::recursive_post(Binary_Node* subroot, void(*visit)(Record&))
{
	if (subroot != NULL)
	{
		recursive_post(subroot->left, visit);
		recursive_post(subroot->right, visit);
		(*visit)(subroot->data);
	}
}

Binary_Node* Binary_Tree::return_root()
{
	return root;
}

int Binary_Tree::size()const
{
	return count;
}

int Binary_Tree::height()const
{
	int count = size();
	if (count == 0)
		return -1;
	int tmp = 1;
	int k = 0;
	for (k = 0; tmp <= count; k++)
		tmp *= 2;
	return k - 1;
}

void Binary_Tree::insert(Record& x)
{
	if (empty())
	{
		root = new Binary_Node(x);
		count++;
		return;
	}
	Stack numbers;
	int item = 0;
	int tmpcount = size();
	while (tmpcount > 0)
	{
		if (tmpcount % 2 == 0)
		{
			numbers.push(2);
		}
		else
			numbers.push(1);
		tmpcount = (tmpcount - 1) / 2;
	}
	Binary_Node* current = root;
	while (numbers.size() > 1)
	{
		numbers.top(item);
		if (item == 1)
			current = current->left;
		if (item == 2)
			current = current->right;
		numbers.pop();
	}
	numbers.top(item);
	if (item == 1)
		current->left = new Binary_Node(x);
	if (item == 2)
		current->right = new Binary_Node(x);
	count++;
}

class Search_Tree :public Binary_Tree
{
public:
	Error_code insert(const Record& new_data);
	Error_code remove(const Record& target);
	Error_code tree_search(Record& target)const;
private:
	Binary_Node* searchNode(Binary_Node* subroot, const Record& target)const;
	Error_code search_insert(Binary_Node*& subroot, const Record& new_data);
	Error_code remove_root(Binary_Node*& subroot);
	Error_code search_destory(Binary_Node*& subroot, const Record& target);
};

Error_code Search_Tree::tree_search(Record& target)const
{
	Error_code result = success;
	Binary_Node* found = searchNode(root, target);
	if (found == NULL)
		result = notPresent;
	else
		target = found->data;
	return result;
}

Binary_Node* Search_Tree::searchNode(Binary_Node* subroot, const Record& target)const
{
	if (subroot == NULL || subroot->data == target)
		return subroot;
	else if (subroot->data < target)
	{
		cout << subroot->data.the_key() << " ";
		return searchNode(subroot->right, target);
	}
	else
	{
		cout << subroot->data.the_key() << " ";
		return searchNode(subroot->left, target);
	}
}

Error_code Search_Tree::insert(const Record& new_data)
{
	Error_code result = search_insert(root, new_data);
	if (result == success)
		count++;
	return result;
}

Error_code Search_Tree::search_insert(Binary_Node*& subroot, const Record& new_data)
{
	if (subroot == NULL)
	{
		subroot = new Binary_Node(new_data);
		return success;
	}
	else if (new_data < subroot->data)
		return search_insert(subroot->left, new_data);
	else if (new_data > subroot->data)
		return search_insert(subroot->right, new_data);
	else
		return duplicate_error;
}

Error_code Search_Tree::remove_root(Binary_Node*& subroot)
{
	if (subroot == NULL)
		return notPresent;
	Binary_Node* to_delete = subroot;
	if (subroot->right == NULL)
		subroot = subroot->left;
	else if (subroot->left == NULL)
		subroot = subroot->right;
	else
	{
		to_delete = subroot->left;
		Binary_Node* parent = subroot;
		while (to_delete->right != NULL)
		{
			parent = to_delete;
			to_delete = to_delete->right;
		}
		subroot->data = to_delete->data;
		if (parent == subroot)
			subroot->left = to_delete->left;
		else
			parent->right = to_delete->left;
	}
	delete to_delete;
	return success;
}

Error_code Search_Tree::remove(const Record& target)
{
	Error_code result = search_destory(root, target);
	if (result == success)
		count--;
	return result;
}

Error_code Search_Tree::search_destory(Binary_Node*& subroot, const Record& target)
{
	if (subroot == NULL || subroot->data == target)
		return remove_root(subroot);
	else if (target < subroot->data)
		return search_destory(subroot->left, target);
	else
		return search_destory(subroot->right, target);
}

Binary_Node* Solution(Binary_Node* root,Record& ptr1,Record& ptr2)
{
	if (root == NULL)
		return NULL;
	else if (root->data > ptr1 && root->data > ptr2)
	{
		Solution(root->left, ptr1, ptr2);
	}
	else if (root->data < ptr1 && root->data < ptr2)
	{
		Solution(root->right, ptr1, ptr2);
	}
	else
		return root;
}

int main(void)
{
	Search_Tree MyTree;
	int I_num = 0, D_num = 0;
	cin >> I_num;
	while (I_num--)
	{
		int x;
		cin >> x;
		Record temp(x, x);
		MyTree.insert(temp);
	}
	int x, y;
	cin >> x >> y;
	Record ptr1(x, x),ptr2(y,y);
	Binary_Node* root=MyTree.return_root();	
	Binary_Node* ans = Solution(root, ptr1, ptr2);
	cout << ans->data.the_key();
	return 0;
}

第六题`
#include
#include <stdbool.h>
#include <string.h>

using namespace std;

typedef int Stack_Entry, Node_entry;
const int max_list = 30;
enum Error_code { fail,success, notPresent, duplicate_error, overflow, rangeError, underflow};

struct Node
{
Node_entry entry;
Node* next;
Node();
Node(Node_entry item, Node* add_on = NULL);
};

Node::Node()
{
next = NULL;
}

Node::Node(Node_entry item, Node* add_on)
{
entry = item;
next = add_on;
}

class Stack
{
public:
Stack();
bool emptyS()const;
void push(const Stack_Entry& item);
void pop();
void top(Stack_Entry& item);
void clearS();
int size();
protected:
Node* top_node;
int countS;
};

Stack::Stack()
{
top_node = NULL;
countS = 0;
}

void Stack::push(const Stack_Entry& item)
{
Node* new_top = new Node(item, top_node);
countS++;
if (new_top != NULL)
top_node = new_top;
}

void Stack::pop()
{
Node* old_top = top_node;
if (top_node != NULL)
{
top_node = old_top->next;
countS–;
delete old_top;
}
}

void Stack::top(Stack_Entry& item)
{
if (top_node != NULL)
{
item = top_node->entry;
}
}

bool Stack::emptyS()const
{
bool outcome = true;
if (top_node != NULL)
outcome = false;
return outcome;
}

int Stack::size()
{
return countS;
}

void Stack::clearS()
{
while (top_node != NULL)
{
Node* p = top_node;
top_node = top_node->next;
delete p;
}
}

template
class List
{
public:
List();
int size() const;
bool full() const;
bool empty() const;
void clear();
void traverse(void (*visit)(List_entry&));
Error_code retrieve(int position, List_entry& x) const;
Error_code replace(int position, const List_entry& x);
Error_code remove(int position, List_entry& x);
Error_code insert(int position, const List_entry& x);
protected:
int count;
List_entry entry[max_list];
};

template
List<List_entry> ::List()
{
count = 0;
}

template
int List<List_entry> ::size() const
{
return count;
}

template
bool List<List_entry> ::full() const
{
return(count == max_list);
}

template
bool List<List_entry> ::empty() const
{
return count == 0;
}

template
void List<List_entry> ::clear()
{
count = 0;
}

template
Error_code List<List_entry> ::insert(int position, const List_entry& x)
{
if (full())return overflow;
if (position < 0 || position > count)return rangeError;
for (int i = count - 1; i >= position; i–) entry[i + 1] = entry[i];
entry[position] = x;
count++;
return success;
}

template
Error_code List<List_entry> ::remove(int position, List_entry& x)
{
if (empty())return underflow;
if (position < 0 || position >= count)return rangeError;
x = entry[position];
for (int i = position; i < count - 1; i++)entry[i] = entry[i + 1];
count–;
return success;
}

template
Error_code List<List_entry> ::replace(int position, const List_entry& x)
{
if (position < 0 || position >= count)return rangeError;
entry[position] = x;
return success;
}

template
Error_code List<List_entry> ::retrieve(int position, List_entry& x) const
{
if (position < 0 || position >= count)return rangeError;
x = entry[position];
return success;
}

template
void List<List_entry> ::traverse(void (*visit)(List_entry&))
{
for (int i = 0; i < count; i++)(*visit)(entry[i]);
}

class Record
{
public:
Record();
Record(string x, string y);
string the_key()const;
string the_other()const;
private:
string key;
string other;
};
typedef Record List_entry;

Record::Record()
{

}

Record::Record(string x, string y)
{
key = x;
other = y;
}

string Record::the_key()const
{
return key;
}

string Record::the_other()const
{
return other;
}

class Key
{
string key;
public:
Key(string x);
Key(const Record& r);
string the_key()const;
};

Key::Key(string x)
{
key = x;
}

Key::Key(const Record& r)
{
key = r.the_key();
}

string Key::the_key()const
{
return key;
}

bool operator==(const Key& x, const Key& y)
{
return x.the_key() == y.the_key();
}

bool operator > (const Record& x, const Record& y)
{
return x.the_key() > y.the_key();
}

bool operator < (const Record& x, const Record& y)
{
return x.the_key() < y.the_key();
}

bool operator <=(const Record& x, const Record& y)
{
return x.the_key() <= y.the_key();
}

bool operator >=(const Record& x, const Record& y)
{
return x.the_key() >=y.the_key();
}

class Binary_Node
{
public:
Record data;
Binary_Node* left;
Binary_Node* right;
Binary_Node();
Binary_Node(const Record& x);
};

Binary_Node::Binary_Node()
{
left = NULL;
right = NULL;
}

Binary_Node::Binary_Node(const Record& x)
{
data = x;
left = NULL;
right = NULL;
}

class Binary_Tree
{
public:
Binary_Tree();
bool empty()const;
void preorder(void(*visit)(Record&));
void inorder(void(*visit)(Record&));
void postorder(void(visit)(Record&));
int size()const;
int height()const;
void insert(Record&);
protected:
void recursive_pre(Binary_Node
subroot, void(visit)(Record&));
void recursive_in(Binary_Node
subroot, void(visit)(Record&));
void recursive_post(Binary_Node
subroot, void(visit)(Record&));
Binary_Node
root;
int count;
};

Binary_Tree::Binary_Tree()
{
root = NULL;
count = 0;
}

bool Binary_Tree::empty()const
{
return root == NULL;
}

void Binary_Tree::preorder(void(*visit)(Record&))
{
recursive_pre(root, visit);
}

void Binary_Tree::recursive_pre(Binary_Node* subroot, void(*visit)(Record&))
{
if (subroot != NULL)
{
(*visit)(subroot->data);
recursive_pre(subroot->left, visit);
recursive_pre(subroot->right, visit);
}
}

void Binary_Tree::inorder(void(*visit)(Record&))
{
recursive_in(root, visit);
}

void Binary_Tree::recursive_in(Binary_Node* subroot, void(*visit)(Record&))
{
if (subroot != NULL)
{
recursive_in(subroot->left, visit);
(*visit)(subroot->data);
recursive_in(subroot->right, visit);
}
}

void Binary_Tree::postorder(void(*visit)(Record&))
{
recursive_post(root, visit);
}

void Binary_Tree::recursive_post(Binary_Node* subroot, void(*visit)(Record&))
{
if (subroot != NULL)
{
recursive_post(subroot->left, visit);
recursive_post(subroot->right, visit);
(*visit)(subroot->data);
}
}

int Binary_Tree::size()const
{
return count;
}

int Binary_Tree::height()const
{
int count = size();
if (count == 0)
return -1;
int tmp = 1;
int k = 0;
for (k = 0; tmp <= count; k++)
tmp *= 2;
return k - 1;
}

void Binary_Tree::insert(Record& x)
{
if (empty())
{
root = new Binary_Node(x);
count++;
return;
}
Stack numbers;
int item = 0;
int tmpcount = size();
while (tmpcount > 0)
{
if (tmpcount % 2 == 0)
{
numbers.push(2);
}
else
numbers.push(1);
tmpcount = (tmpcount - 1) / 2;
}
Binary_Node* current = root;
while (numbers.size() > 1)
{
numbers.top(item);
if (item == 1)
current = current->left;
if (item == 2)
current = current->right;
numbers.pop();
}
numbers.top(item);
if (item == 1)
current->left = new Binary_Node(x);
if (item == 2)
current->right = new Binary_Node(x);
count++;
}

class Search_Tree :public Binary_Tree
{
public:
Error_code insert(const Record& new_data);
Error_code remove(const Record& target);
Error_code tree_search(Record& target)const;
private:
Binary_Node* searchNode(Binary_Node* subroot, const Record& target)const;
Error_code search_insert(Binary_Node*& subroot, const Record& new_data);
Error_code remove_root(Binary_Node*& subroot);
Error_code search_destory(Binary_Node*& subroot, const Record& target);
};

Error_code Search_Tree::tree_search(Record& target)const
{
Error_code result = success;
Binary_Node* found = searchNode(root, target);
if (found == NULL)
result = notPresent;
else
target = found->data;
return result;
}

Binary_Node* Search_Tree::searchNode(Binary_Node* subroot, const Record& target)const
{
if (subroot == NULL || subroot->data == target)
return subroot;
else if (subroot->data < target)
{
cout << subroot->data.the_key() << " ";
return searchNode(subroot->right, target);
}
else
{
cout << subroot->data.the_key() << " ";
return searchNode(subroot->left, target);
}
}

Error_code Search_Tree::insert(const Record& new_data)
{
Error_code result = search_insert(root, new_data);
if (result == success)
count++;
return result;
}

Error_code Search_Tree::search_insert(Binary_Node*& subroot, const Record& new_data)
{
if (subroot == NULL)
{
subroot = new Binary_Node(new_data);
return success;
}
else if (new_data < subroot->data)
return search_insert(subroot->left, new_data);
else if (new_data > subroot->data)
return search_insert(subroot->right, new_data);
else
return duplicate_error;
}

Error_code Search_Tree::remove_root(Binary_Node*& subroot)
{
if (subroot == NULL)
return notPresent;
Binary_Node* to_delete = subroot;
if (subroot->right == NULL)
subroot = subroot->left;
else if (subroot->left == NULL)
subroot = subroot->right;
else
{
to_delete = subroot->left;
Binary_Node* parent = subroot;
while (to_delete->right != NULL)
{
parent = to_delete;
to_delete = to_delete->right;
}
subroot->data = to_delete->data;
if (parent == subroot)
subroot->left = to_delete->left;
else
parent->right = to_delete->left;
}
delete to_delete;
return success;
}

Error_code Search_Tree::remove(const Record& target)
{
Error_code result = search_destory(root, target);
if (result == success)
count–;
return result;
}

Error_code Search_Tree::search_destory(Binary_Node*& subroot, const Record& target)
{
if (subroot == NULL || subroot->data == target)
return remove_root(subroot);
else if (target < subroot->data)
return search_destory(subroot->left, target);
else
return search_destory(subroot->right, target);
}

class Buildable_Tree :public Search_Tree
{
public:
Error_code build_tree(const List& supply);
private:
void build_in(int count, const Record& new_data, List <Binary_Node*>& last_Node);
Binary_Node* find_root(List<Binary_Node*>& last_Node);
void connect_trees(const List<Binary_Node*>& last_Node);
};

Error_code Buildable_Tree::build_tree(const List& supply)
{
Error_code ordered_data = success;
int count = 0;
Record x, last_x;
List<Binary_Node*> last_node;
Binary_Node* none = NULL;
last_node.insert(0,none);
while (supply.retrieve(count, x) == success)
{
if (count > 0 && x <= last_x)
{
ordered_data = fail;
break;
}
build_in(++count, x, last_node);
last_x = x;
}
root = find_root(last_node);
connect_trees(last_node);
return ordered_data;
}

void Buildable_Tree::build_in(int count, const Record& new_data, List <Binary_Node*>& last_Node)
{
int level;
for (level = 1; count % 2 == 0; level++)
count /= 2;
Binary_Node* next_node = new Binary_Node(new_data);
Binary_Node* parent;
last_Node.retrieve(level - 1, next_node->left);
if (last_Node.size() <= level)
last_Node.insert(level, next_node);
else
last_Node.replace(level, next_node);
if (last_Node.retrieve(level + 1, parent) == success&&parent->right==NULL)
parent->right = next_node;
}

Binary_Node* Buildable_Tree::find_root(List<Binary_Node*>& last_Node)
{
Binary_Node* high_node;
last_Node.retrieve(last_Node.size() - 1, high_node);
return high_node;
}

void Buildable_Tree::connect_trees(const List<Binary_Node*>& last_Node)
{
Binary_Node* high_node, * low_node;
int high_level = last_Node.size() - 1,low_level;
while (high_level > 2)
{
last_Node.retrieve(high_level, high_node);
if (high_node->right != NULL)
high_level–;
else
{
low_level = high_level;
do
{
last_Node.retrieve(–low_level, low_node);
} while (low_node != NULL && low_node->data < high_node->data);
high_node->right = low_node;
high_level = low_level;
}
}
}

void print(Record*& )
{
}

int main(void)
{
Buildable_Tree MyTree;
List MyList;
int I_num = 0;
cin >> I_num;
for (int i = 0; i < I_num; i++)
{
string x, y;
cin >> x >> y;
Record temp(x, y);
MyList.insert(i, temp);
}
MyTree.build_tree(MyList);
for (int i = 0; i < 2; i++)
{
string data;
cin >> data;
Record tar(data, data);
if (MyTree.tree_search(tar) == success)
cout << data << " " << tar.the_other() << endl;
else
cout << “NULL” << endl;
}
return 0;
}`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值