创建二叉树&创建多叉树并层次化打印

创建二叉树:

#include<bits/stdc++.h>
using namespace std;

/*typedef struct TreeNode{
    int value;
    struct TreeNode* left;
    struct TreeNode* right;

}TreeNode;*/

class TreeNode{
    public:
        int value;
        TreeNode* left;
        TreeNode* right;
        TreeNode(int x) : value(x), left(NULL), right(NULL) {}

};

void createTree(TreeNode* &node,int a[], int len,int index){
    if(index >= len){
        return;
    }

    node = new TreeNode(a[index]);

    //node->value = a[index];
    node->left = NULL;
    node->right = NULL;

    createTree(node->left, a, len, 2 * index + 1);
    createTree(node->right, a, len, 2 * index + 2);

}

void print(TreeNode* node){
    if( node == NULL){
        return;
    }
    //中序遍历二叉搜索树,左,根,右。
    print(node->left);
    cout<<node->value;
    print(node->right);
}

TreeNode* findCommon(TreeNode* root, TreeNode* p, TreeNode* q) {
    if( (p->value-root->value)*(q->value-root->value) <= 0 ){
        return root;
    }

    return findCommon( p->value > root->value ? root->right : root->left, p, q);
}

TreeNode* findCommon(TreeNode* root, int p, int q) {
    if( (p-root->value)*(q-root->value) <= 0 ){
        return root;
    }

    return findCommon( p > root->value ? root->right : root->left, p, q);
}

int main()
{
    int a[7] = {4,2,6,1,3,5,7};
    /*int a[1024];
    int i = 0;
    int num;
    while(cin>>num){
        a[i++] = num;
    }*/

    TreeNode* head;
    //createTree(node,a,i,0);
    createTree( head,a,7,0 );
    print(head);


    TreeNode* common = findCommon( head, 5, 7);
    cout<<common->value;

	return 0;
}


 

创建多叉树:

#include<bits/stdc++.h>
using namespace std;

class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};

void putChild(Node *node, Node *parent) {

    parent->children.push_back(node);

}

void putChildren(vector<Node *> nodes, Node *parent) {

    for (int i = 0; i < nodes.size(); ++i) {
        putChild(nodes[i], parent);
    }

}

void dfs(Node* root , vector<int>& vec, string str){
        if(root == NULL){
            return;
        }
        vec.push_back(root->val);
        cout<<root->val<<endl;//打印

        if( !root->children.empty() ){
            str.push_back('-'); //打印
            for( auto node : root->children ){
                cout<<str;//打印
                dfs(node, vec, str);
            }
        }
}

vector<int> preorder(Node* root) {

    vector<int> vec;
    string str;//打印
    dfs(root,vec,str);

    return vec;
}

int main()
{
    Node* root = new Node(100);

    vector<Node*> vec1;
    Node* node = new Node(1);
    vec1.push_back(node);
    int i;
    for( i=2; i<5; i++){
        Node* node1 = new Node(i);
        vec1.push_back(node1);
    }

    vector<Node*> vec2;
    for( i=5; i<8; i++){
        Node* node2 = new Node(i);
        vec2.push_back(node2);
    }

    putChildren(vec1,root);
    putChildren(vec2,node);


    preorder(root);


    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值