수색…


BST에서 노드 만들기

BST (Binary Search Tree)는 루트 노드에 대한 단일 포인터가있는 계층 적 데이터 구조입니다.

BST의 노드에는 일반적으로 빠른 검색을위한 "항목"(예 : 숫자 또는 이름)이 포함됩니다. 각 노드에는 최대 두 개의 하위 노드가 있습니다 (왼쪽과 오른쪽). 모든 노드는 몇 가지 주요 데이터 필드로 구성됩니다. BST의 모든 노드에서 키가 왼쪽 자식 키보다 커서 오른쪽 자식 키보다 작습니다.

노드의 전형적인 구조 (정수를 저장함)는

struct bst_node {
    int item; 
    bst_node* left;
    bst_node* right;
}; 

BST의 단 하나의 루트 노드 만 있습니다. 루트 노드는

bst_node* root = NULL;
root = (bst_node*) malloc(sizeof(bst_node));

루트의 항목 키를 10으로 설정합니다.

root->item = 10;

이진 검색 트리에 노드 삽입

struct tree{
    int a;
    tree* right;
    tree* left;
};
tree* root=NULL;
void insert(tree*& in, int b){
        if(in){
            if(in->a<b)
                    insert(in->right,b);
            else if(in->a>b)
                    insert(in->left,b);
            else
                cout<<"the value is already in the tree."<<endl;
        }else{
            tree* temp = new tree;
            temp->a=b;
            temp->right=NULL;
            temp->left=NULL;
            in=temp;
        }
}


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow