資料結構 二叉樹基本操作 二叉樹面試題

2021-08-25 05:47:00 字數 3850 閱讀 5152

二叉樹的基本概念就不多說了 **如下

binarytree.c

#pragma once

#include #include #include #include typedef char btdatatype;

typedef struct binarytreenode

btnode;

// a是乙個前序遍歷的陣列

//二叉樹的建立

btnode* binarytreecreate(btdatatype* a, int n, int* pi);

//二叉式銷毀

void binarytreedestory(btnode* root);

//二叉樹節點個數

int binarytreesize(btnode* root);

//二叉樹葉子節點個數

int binarytreeleafsize(btnode* root);

//二叉樹第k個節點個數

int binarytreelevelksize(btnode* root, int k);

//查詢節點

btnode* binarytreefind(btnode* root, btdatatype x);

//完全二叉樹的判斷

int binarytreecomplete(btnode* root);

//二叉樹高度

int binarytreeheight(btnode* root);

// 遍歷 遞迴&非遞迴

//先序

void binarytreeprevorder(btnode* root);

//中序

void binarytreeinorder(btnode* root);

//後序

void binarytreepostorder(btnode* root);

//層序(非遞迴)

void binarytreelevelorder(btnode* root);

//先序(非遞迴)

void binarytreeprevordernonr(btnode* root);

//中序(非遞迴)

void binarytreeinordernonr(btnode* root);

//後序(非遞迴)

void binarytreepostordernonr(btnode* root);

void testbinarytree();

binarytree .c

#include "binarytree.h"

#include "queue.h"

#include "stack.h"

//申請節點

btnode* buybtnode(btdatatype x)

//二叉樹的建立

btnode* binarytreecreate(btdatatype* a, int n, int* pi)

else }

//數的銷毀

//採用遞迴,先銷毀根左子樹節點,在銷毀根右子樹節點,最後銷毀根節點

void binarytreedestory(btnode* root)

//樹的節點數

int binarytreesize(btnode* root)

//樹的葉子節點數

int binarytreeleafsize(btnode* root)

//數的第k層節點數

int binarytreelevelksize(btnode* root, int k)

//查詢節點

btnode* binarytreefind(btnode* root, btdatatype x)

//判斷完全二叉樹

//我們可以採用層次遍歷的思想,當它的left或right為空時,將'#',入隊

//如果為完全二叉樹則當隊頭元素為'#',那麼不斷出隊判斷對頭元素為'#'即可,如果出現

//不是'#'的元素則為非完全二叉樹

int binarytreecomplete(btnode* root)

if (queuefront(&q)->_right)

queuepush(&q, queuefront(&q)->_right);

else

if (queuefront(&q)->_data != '#')

queuepop(&q);

if (queuefront(&q)->_data == '#')

if (queueempty(&q))

return -1;

else

return 1;

} }queuedestory(&q);

return 1;

}//數的高度

//我們可以將題目分解為求節點左邊的高度和求節點右邊的高度,最後只需要將

//求出的結果進行比較,返回最大值就好

int binarytreeheight(btnode* root)

// 遍歷 遞迴&非遞迴

//先序遍歷

void binarytreeprevorder(btnode* root)

//中序遍歷

void binarytreeinorder(btnode* root)

//後序遍歷

void binarytreepostorder(btnode* root)

//層序遍歷

//採用佇列的性質先入先出,將左右一次入隊

void binarytreelevelorder(btnode* root)

queuedestory(&q);

}void testbinarytree()

; char array = ;

// char array = ;

size_t i = 0;

int j = 0;

btnode* tree = binarytreecreate(array, sizeof(array) / sizeof(btdatatype), &i);

printf("先序遍歷順序為: ");

binarytreeprevorder(tree);

printf("\n中序遍歷順序為: ");

binarytreeinorder(tree);

printf("\n後序遍歷順序為: ");

binarytreepostorder(tree);

printf("\n層序遍歷順序為: ");

binarytreelevelorder(tree);

printf("\n");

printf("樹的節點個數為: %d\n",binarytreesize(tree));

printf("樹的高度為: %d\n", binarytreeheight(tree));

if (binarytreefind(tree, 'd') != null)

printf("%c \n", binarytreefind(tree, 'd')->_data);

else

printf("沒有找到!\n");

j = binarytreecomplete(tree);

if (j > 0)

printf("完全二叉樹\n");

else

printf("非完全二叉樹\n");

binarytreedestory(tree);

}

test.c

#include "binarytree.h"

int main()

資料結構 二叉樹 反轉二叉樹

include using namespace std define maxsize 1000 struct binary tree node class queue queue queue void queue push binary tree node btn binary tree node ...

資料結構 樹結構 二叉樹 完全二叉樹 滿二叉樹

樹結構是一種描述非線性層次關係的資料結構。除根結點外,其餘每個結點有且僅有乙個直接前驅。每個結點可以有任意多個直接後繼。英文名詞表示 tree,root,node,leaf,edge,child,subtree 要麼二叉樹沒有根結點,是一棵空樹。要麼二叉樹由根結點,左子樹,右子樹組成,且左子樹和右子...

二叉樹 二叉樹

題目描述 如上所示,由正整數1,2,3 組成了一顆特殊二叉樹。我們已知這個二叉樹的最後乙個結點是n。現在的問題是,結點m所在的子樹中一共包括多少個結點。比如,n 12,m 3那麼上圖中的結點13,14,15以及後面的結點都是不存在的,結點m所在子樹中包括的結點有3,6,7,12,因此結點m的所在子樹...